package com.mercurylake.test.tree;
import java.util.Stack;
public class SimpleTree {
//根节点
private TreeNode root;
private SimpleTree(){
this.root=new TreeNode();
}
//树深度
public int height(){
return height(root);
}
private int height(TreeNode td){
if(td==null)return 0;
else{
return height(td.leftChild)>height(td.rightChild)?height(td.leftChild):height(td.rightChild)+0;
}
}
//节点个数
public int size(){
return size(root);
}
private int size(TreeNode td){
return size(td.leftChild)>size(td.rightChild)?size(td.leftChild):size(td.rightChild)+1;
}
//前序
public void pre(){
pre(root);
}
private void pre(TreeNode td){
if(td==null)return;
else{
System.out.println(td.data);
pre(td.leftChild);
pre(td.rightChild);
}
}
//中序遍历
private void middle(TreeNode td){
if(td==null) return;
else{
middle(td.leftChild);
System.out.println(td);
middle(td.rightChild);
}
}
private void back(TreeNode td){
if(td==null)return;
else{
back(td.leftChild);
back(td.rightChild);
System.out.println(td);
}
}
//非递归中序遍历
private void MiddleByStack(){
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode temp=root;
boolean isleft=true;
//root压栈
stack.push(root);
//如果栈里有值
while(stack.size()>0){
//如果当前节点有左孩子,而且是左孩子并没有遍历到,把所有左孩子的左孩子都压栈
while(temp.leftChild!=null&&isleft){
stack.push(temp.leftChild);
temp=temp.leftChild;
}
//取出一个元素作为当前元素
temp=stack.pop();
//打印当前元素
System.out.println(temp.data);
isleft=true;
//如果右孩子不为空
if(temp.rightChild!=null){
isleft=false;
temp=temp.rightChild;
}
}
}
//非递归中序遍历(借鉴的,仅供参考)
private void midByStack(){
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode temp=root;
stack.push(this.root);
while (stack.size()>0) {
while (temp != null) {
temp = temp.leftChild;
stack.push(temp);
}
//去除加入的null
stack.pop();
if (stack.size() > 0) {
temp = stack.pop();
System.out.println(temp.data);
temp = temp.rightChild;
stack.push(temp);
}
}
}
//非递归
private void mid2(){
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode temp=root;
stack.push(root);
if(stack.size()>0){
//所有左孩子压栈
while(temp!=null){
temp=temp.leftChild;
stack.push(temp);
}
//去除空节点
stack.pop();
//打印并且处理右孩子
if(stack.size()>0){
temp=stack.pop();
System.out.println(temp.data);
temp=temp.rightChild;
stack.push(temp.rightChild);
}
}
}
/**
*
* @param root 树的根节点
* 利用栈模拟递归过程实现循环先序遍历二叉树
* 这种方式具备扩展性,它模拟递归的过程,将左子树点不断的压入栈,直到null,然后处理栈顶节点的右子树
*/
public void preOrderStack_2(TreeNode root){
if(root==null)return;
Stack<TreeNode> s=new Stack<TreeNode>();
while(root!=null||!s.isEmpty()){
while(root!=null){
System.out.println(root.data);
s.push(root);//先访问再入栈
root=root.leftChild;
}
root=s.pop();
root=root.rightChild;//如果是null,出栈并处理右子树
}
}
public void preStack(){
TreeNode temp=root;
Stack<TreeNode> stack=new Stack<TreeNode>();
while(stack.size()>0||temp!=null){
//打印压栈
while(temp!=null){
System.out.println(temp.data);
stack.push(temp);
temp=temp.leftChild;
}
//处理右孩子
temp=stack.pop();
temp=temp.rightChild;
}
}
//插入
public TreeNode insert(String data,TreeNode temp){
if(temp==null)return new TreeNode(data);
int result=data.compareTo(temp.data);
if(result<0){
temp.leftChild=insert(data,temp.leftChild);
if(temp.leftChild.height-temp.rightChild.height==2){
//判断插在哪一边,左边但旋转,右边双旋转
}
}else if(result>0){
temp.rightChild=insert(data,temp.leftChild);
if(temp.rightChild.height-temp.leftChild.height==2)
System.out.println("右单旋转");
else if(temp.rightChild.height-temp.leftChild.height==2)
System.out.println("右双旋转");
}else ;
//改变height
return temp ;
}
public TreeNode leftSingleRoll(TreeNode node){
TreeNode tl=node.leftChild;
node.leftChild=tl.rightChild;
tl.rightChild=node;
return tl;
}
public TreeNode rightSingleRoll(TreeNode node){
TreeNode tl=node.rightChild;
node.rightChild=tl.leftChild;
tl.leftChild=node;
return tl;
}
public TreeNode
leftDoubleRoll(TreeNode node){
TreeNode tl=rightSingleRoll(node.leftChild);
return leftSingleRoll(node);
}
private int cp(String x,String y){
return x.compareTo(y);
}
//内部类:节点
@SuppressWarnings("unused")
private class TreeNode{
private int key;
private TreeNode leftChild;
private TreeNode rightChild;
private int height;
private String data=null;
public TreeNode(){}
public TreeNode(String data){
this.data=data;
this.height=0;
}
public TreeNode(int key,TreeNode leftchild,TreeNode rightChild){
this.key=key;
this.leftChild=leftchild;
this.rightChild=rightChild;
}
}
}
import java.util.Stack;
public class SimpleTree {
//根节点
private TreeNode root;
private SimpleTree(){
this.root=new TreeNode();
}
//树深度
public int height(){
return height(root);
}
private int height(TreeNode td){
if(td==null)return 0;
else{
return height(td.leftChild)>height(td.rightChild)?height(td.leftChild):height(td.rightChild)+0;
}
}
//节点个数
public int size(){
return size(root);
}
private int size(TreeNode td){
return size(td.leftChild)>size(td.rightChild)?size(td.leftChild):size(td.rightChild)+1;
}
//前序
public void pre(){
pre(root);
}
private void pre(TreeNode td){
if(td==null)return;
else{
System.out.println(td.data);
pre(td.leftChild);
pre(td.rightChild);
}
}
//中序遍历
private void middle(TreeNode td){
if(td==null) return;
else{
middle(td.leftChild);
System.out.println(td);
middle(td.rightChild);
}
}
private void back(TreeNode td){
if(td==null)return;
else{
back(td.leftChild);
back(td.rightChild);
System.out.println(td);
}
}
//非递归中序遍历
private void MiddleByStack(){
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode temp=root;
boolean isleft=true;
//root压栈
stack.push(root);
//如果栈里有值
while(stack.size()>0){
//如果当前节点有左孩子,而且是左孩子并没有遍历到,把所有左孩子的左孩子都压栈
while(temp.leftChild!=null&&isleft){
stack.push(temp.leftChild);
temp=temp.leftChild;
}
//取出一个元素作为当前元素
temp=stack.pop();
//打印当前元素
System.out.println(temp.data);
isleft=true;
//如果右孩子不为空
if(temp.rightChild!=null){
isleft=false;
temp=temp.rightChild;
}
}
}
//非递归中序遍历(借鉴的,仅供参考)
private void midByStack(){
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode temp=root;
stack.push(this.root);
while (stack.size()>0) {
while (temp != null) {
temp = temp.leftChild;
stack.push(temp);
}
//去除加入的null
stack.pop();
if (stack.size() > 0) {
temp = stack.pop();
System.out.println(temp.data);
temp = temp.rightChild;
stack.push(temp);
}
}
}
//非递归
private void mid2(){
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode temp=root;
stack.push(root);
if(stack.size()>0){
//所有左孩子压栈
while(temp!=null){
temp=temp.leftChild;
stack.push(temp);
}
//去除空节点
stack.pop();
//打印并且处理右孩子
if(stack.size()>0){
temp=stack.pop();
System.out.println(temp.data);
temp=temp.rightChild;
stack.push(temp.rightChild);
}
}
}
/**
*
* @param root 树的根节点
* 利用栈模拟递归过程实现循环先序遍历二叉树
* 这种方式具备扩展性,它模拟递归的过程,将左子树点不断的压入栈,直到null,然后处理栈顶节点的右子树
*/
public void preOrderStack_2(TreeNode root){
if(root==null)return;
Stack<TreeNode> s=new Stack<TreeNode>();
while(root!=null||!s.isEmpty()){
while(root!=null){
System.out.println(root.data);
s.push(root);//先访问再入栈
root=root.leftChild;
}
root=s.pop();
root=root.rightChild;//如果是null,出栈并处理右子树
}
}
public void preStack(){
TreeNode temp=root;
Stack<TreeNode> stack=new Stack<TreeNode>();
while(stack.size()>0||temp!=null){
//打印压栈
while(temp!=null){
System.out.println(temp.data);
stack.push(temp);
temp=temp.leftChild;
}
//处理右孩子
temp=stack.pop();
temp=temp.rightChild;
}
}
//插入
public TreeNode insert(String data,TreeNode temp){
if(temp==null)return new TreeNode(data);
int result=data.compareTo(temp.data);
if(result<0){
temp.leftChild=insert(data,temp.leftChild);
if(temp.leftChild.height-temp.rightChild.height==2){
//判断插在哪一边,左边但旋转,右边双旋转
}
}else if(result>0){
temp.rightChild=insert(data,temp.leftChild);
if(temp.rightChild.height-temp.leftChild.height==2)
System.out.println("右单旋转");
else if(temp.rightChild.height-temp.leftChild.height==2)
System.out.println("右双旋转");
}else ;
//改变height
return temp ;
}
public TreeNode leftSingleRoll(TreeNode node){
TreeNode tl=node.leftChild;
node.leftChild=tl.rightChild;
tl.rightChild=node;
return tl;
}
public TreeNode rightSingleRoll(TreeNode node){
TreeNode tl=node.rightChild;
node.rightChild=tl.leftChild;
tl.leftChild=node;
return tl;
}
public TreeNode
leftDoubleRoll(TreeNode node){
TreeNode tl=rightSingleRoll(node.leftChild);
return leftSingleRoll(node);
}
private int cp(String x,String y){
return x.compareTo(y);
}
//内部类:节点
@SuppressWarnings("unused")
private class TreeNode{
private int key;
private TreeNode leftChild;
private TreeNode rightChild;
private int height;
private String data=null;
public TreeNode(){}
public TreeNode(String data){
this.data=data;
this.height=0;
}
public TreeNode(int key,TreeNode leftchild,TreeNode rightChild){
this.key=key;
this.leftChild=leftchild;
this.rightChild=rightChild;
}
}
}