public class TestBinaryTree1 { TestNode1 root; TestBinaryTree1 left; TestBinaryTree1 right; static int i; static int left1; static int right1; // final static int i1 = i++; public void addData(int n,String name,int cc,int data1) { System.out.println("输入:= "+n); if(root == null) { root = new TestNode1(); root.setData(n); System.out.println("root= "+root.getData()+" "+(++i)+"次"); System.out.println("-------------------------"); if(name !=null && cc !=0 && data1 != 0) System.out.println(name+" 下"+(cc+1)+"层"+" 上一层是:"+data1); System.out.println("-------------------------"); } else { int data = root.getData(); System.out.println("root的data= "+data); if(n <= data) { if(this.left == null) this.left = new TestBinaryTree1(); System.out.println("因为小于,走左left"); this.left.addData(n,"左left",++left1,data); } else { if(this.right == null) this.right = new TestBinaryTree1(); System.out.println("因为大于,走右right"); this.right.addData(n,"右right",++right1,data); } } } public void sort1() { if(this.left!=null) this.left.sort1(); if(this.right!=null) this.right.sort1(); if(this.root!=null) System.out.print(root.getData()+","); } public void sort2() { if(this.root!=null) System.out.print(root.getData()+","); if(this.left!=null) this.left.sort2(); if(this.right!=null) this.right.sort2(); } public void sort3() { if(this.left!=null) this.left.sort3(); if(this.root!=null) System.out.print(root.getData()+","); if(this.right!=null) this.right.sort3(); } public void sort4() { if(this.right!=null) this.right.sort4(); if(this.root!=null) System.out.print(root.getData()+","); if(this.left!=null) this.left.sort4(); } public static void main(String [] args) { TestBinaryTree1 bt = new TestBinaryTree1(); start1(bt); start2(bt); } private static void clear1() { right1=0;left1=0; } private static void start1(TestBinaryTree1 bt) { bt.addData(1,null,0,0);clear1(); bt.addData(3,null,0,0);clear1(); bt.addData(2,null,0,0);clear1(); bt.addData(1,null,0,0);clear1(); bt.addData(4,null,0,0);clear1(); bt.addData(9,null,0,0);clear1(); bt.addData(-1,null,0,0);clear1(); } private static void start2(TestBinaryTree1 bt) { bt.sort1(); System.out.println(); bt.sort2(); System.out.println(); bt.sort3(); System.out.println(); bt.sort4(); } } class TestNode1 { int data; public static int i; public TestNode1() { ++i; } public int getData() { return data; } public void setData(int data) { this.data = data; } }
/*
输入:= 1
root= 1 1次
-------------------------
-------------------------
输入:= 3
root的data= 1
因为大于,走right
输入:= 3
root= 3 2次
-------------------------
right 1层 上一层是:1
-------------------------
输入:= 2
root的data= 1
因为大于,走right
输入:= 2
root的data= 3
因为小于,走left
输入:= 2
root= 2 3次
-------------------------
left 1层 上一层是:3
-------------------------
输入:= 1
root的data= 1
因为小于,走left
输入:= 1
root= 1 4次
-------------------------
left 1层 上一层是:1
-------------------------
输入:= 4
root的data= 1
因为大于,走right
输入:= 4
root的data= 3
因为大于,走right
输入:= 4
root= 4 5次
-------------------------
right 2层 上一层是:3
-------------------------
输入:= 9
root的data= 1
因为大于,走right
输入:= 9
root的data= 3
因为大于,走right
输入:= 9
root的data= 4
因为大于,走right
输入:= 9
root= 9 6次
-------------------------
right 3层 上一层是:4
-------------------------
输入:= -1
root的data= 1
因为小于,走left
输入:= -1
root的data= 1
因为小于,走left
输入:= -1
root= -1 7次
-------------------------
left 2层 上一层是:1
-------------------------
请按任意键继续. . .
*/