二叉排序

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

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 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ljt-tiger

thanks

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值