代码完成了增加元素和打印输出的功能:其余功能待续完成......


package com.tiger.tree;
/**
* 节点存储数据
* @author tiger
*
*/
public class Node {
Node right = null;
Node left = null;
int num = 0; //存储数据
public Node(int num) {
this.num = num;
}
}
package com.tiger.tree;
/**
* 二叉树算法
* @author tiger
*
*/
public class BinaryTree{
Node root;
public void add(int num) {
Node cursor = new Node(num);
//如果为空时,直接添加
if (root == null) {
System.out.println("root == null," + "添加成功 : "+cursor.num);
this.root = cursor;
return;
}
//否则
insertNode(root, cursor);
}
//比较添加
public void insertNode(Node root,Node cursor){//insertNode(Node c,Node newNode)
//如果
if (root.num < cursor.num) {
if (root.right == null) {
System.out.println("root.right == null," + "添加成功 : "+cursor.num);
root.right = cursor;
}else{
System.out.println("insertNode(root.right, cursor)");
insertNode(root.right, cursor);//递归调用
}
}else if (root.num > cursor.num) {
if (root.left == null) {
System.out.println("root.left == null," + "添加成功 : "+cursor.num);
root.left = cursor;
}else{
System.out.println("insertNode(root.left, cursor)");
insertNode(root.left, cursor);
}
}
}
/*
*
*/
public void MediumForEach(){
intoNode( this.root );
}
/*
*
*/
public void intoNode(Node c){
if (c == null) {
return;
}
intoNode(c.left);//从小到大排序,小的都在左边
prt(c);
intoNode(c.right);
}
/*
* 打印
*/
public void prt(Node c){
if (c != null) {
System.out.print(c.num + " ");
}
}
}package com.tiger.tree;
/**
* 测试代码
* @author tiger
*
*/
public class BinaryTreeTest {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
System.out.println("添加第1个元素155,根节点,直接添加");
tree.add(155);
System.out.println("添加第2个元素14,14<155,添加在155的【左】下");
tree.add(14);
System.out.println("添加第3个元素159,159>155,添加在155的【右】下");
tree.add(159);
System.out.println("添加第4个元素20,20<155,20>14,添加在155的[左]下的14的【右】下");
tree.add(20);
System.out.println("添加第5个元素22,22<155,22>14,22>20,添加在155的[左]下的14的[左]下的20的【右】下");
tree.add(22);
System.out.println("添加第6个元素23,23<155,23>14,23>20,23>22,添加在155的[左]下的14的[左]下的20的[右]下的22的【右】下");
tree.add(23);
System.out.println("添加第7个元素2323,2323>155,2323>159,添加在155的[左]下的159的【右】下");
tree.add(2323);
System.out.println("添加第8个元素223,223>155,223>159,223<2323,添加在155的[左]下的159的[右]下的2323的【左】下");
tree.add(223);
tree.MediumForEach();
}
}



程序运行结果如下:

添加第1个元素155,根节点,直接添加

root == null,添加成功 : 155

添加第2个元素14,14<155,添加在155的【左】下

root.left == null,添加成功 : 14

添加第3个元素159,159>155,添加在155的【右】下

root.right == null,添加成功 : 159

添加第4个元素20,20<155,20>14,添加在155的[左]下的14的【右】下

insertNode(root.left, cursor)

root.right == null,添加成功 : 20

添加第5个元素22,22<155,22>14,22>20,添加在155的[左]下的14的[左]下的20的【右】下

insertNode(root.left, cursor)

insertNode(root.right, cursor)

root.right == null,添加成功 : 22

添加第6个元素23,23<155,23>14,23>20,23>22,添加在155的[左]下的14的[左]下的20的[右]下的22的【右】下

insertNode(root.left, cursor)

insertNode(root.right, cursor)

insertNode(root.right, cursor)

root.right == null,添加成功 : 23

添加第7个元素2323,2323>155,2323>159,添加在155的[左]下的159的【右】下

insertNode(root.right, cursor)

root.right == null,添加成功 : 2323

添加第8个元素223,223>155,223>159,223<2323,添加在155的[左]下的159的[右]下的2323的【左】下

insertNode(root.right, cursor)

insertNode(root.right, cursor)

root.left == null,添加成功 : 223

---排序后的结果---


14 20 22 23 155 159 223 2323