二叉树的简单使用【Java】(原创)

13 篇文章 0 订阅
8 篇文章 0 订阅
节点类
public class SyTree {
	//存储的数据
	public double info;
	//左子节点
	public SyTree lTree = null;
	//右子节点
	public SyTree rTree = null;
	//父节点
	public SyTree pTree ;

	public SyTree() {
		super();
	}

	public SyTree(double info) {
		super();
		this.info = info;
	}

	public SyTree(double info, SyTree lTree, SyTree rTree, SyTree pTree) {
		super();
		this.info = info;
		this.lTree = lTree;
		this.rTree = rTree;
		this.pTree = pTree;
	}

	public SyTree(SyTree pTree) {
		super();
		this.pTree = pTree;
	}

	public SyTree(double info, SyTree pTree) {
		super();
		this.info = info;
		this.pTree = pTree;
	}

	@Override
	public String toString() {
		return "SyTree [info=" + info + ", lTree=" + lTree + ", rTree=" + rTree
				+ ", pTree=" + pTree + "]";
	}
	
}

———————————————————————————————————————————————————————————

二叉树类

import java.util.Arrays;

public class ArrayBinTree {

	// 存放树的数组
	public SyTree data[];
	public SyTree root;
	// 树的默认深度
	public final int DEF_DEEP = 4;
	public int treeDeep = 0;

	public ArrayBinTree() {
		super();
		if (treeDeep == 0)
			data = new SyTree[DEF_DEEP];
		else {
			treeDeep *= 2;
			SyTree[] temp = new SyTree[treeDeep];
			temp = Arrays.copyOf(data, data.length);
			data = temp;
		}
	}

	// 添加某一节点
	public void insert(SyTree tree) {
		if (root == null) {
			root = tree;
		} else {
			SyTree current = root;
			while (true) {
				if (tree.info < current.info) {
					if (current.lTree == null) {
						current.lTree = tree;
						tree.pTree = current;
						return;
					}
					current = current.lTree;
				} else {
					if (current.rTree == null) {
						current.rTree = tree;
						tree.pTree = current;
						return;
					}
					current = current.rTree;
				}
			}
		}
	}

	// 删除某一节点(先插找到该节点,然后将相关节点的指向替换即可)
	public void del(SyTree tree) {
		SyTree current = root;
		if (root == tree) {
			current = root;
		} else {
			boolean flag = true;
			SyTree temp = root;
			while (flag) {
				if (temp.info < tree.info) {
					if (current.lTree == null)
						return;
					temp = current.lTree;
				} else if (temp.info > tree.info) {
					if (current.rTree == null)
						return;
					temp = current.rTree;
				} else if (temp.info == tree.info) {
					current = temp;
					flag = false;
				}
			}
		}
		// 开始进行删除操作
		if (current.lTree != null) {
			if (current.lTree.lTree != null || current.lTree.rTree != null) {
				SyTree temp = current.lTree;
				while (true) {
					// 如果temp符合条件
					if (temp.rTree == null) {
						// 如果其存在左子树,将其左子树移到其原来的位置
						if (temp.lTree != null) {
							if (temp.pTree.lTree == temp) {
								temp.pTree.lTree = temp.lTree;
								temp.lTree.pTree = temp.pTree;
							} else {
								temp.pTree.rTree = temp.lTree;
								temp.lTree.pTree = temp.pTree;
							}
						}
						// 将其移到目标的空缺位置
						current.lTree.pTree = temp;
						current.rTree.pTree = temp;
						temp.lTree = current.lTree;
						temp.rTree = current.rTree;
						current = null;
						return; // 删除完毕
					}
					temp = temp.rTree;
				}
			} else {
				current.lTree.lTree = null;
				current.lTree.rTree = current.rTree;
				current.lTree.pTree = current.pTree;
				current = null;
				return;// 删除完毕
			}
		}
		// 如果其左子树为空(类似于上面的)
		else if (current.rTree != null) {
			if (current.rTree.lTree != null || current.rTree.rTree != null) {
				SyTree temp = current.rTree;
				while (true) {
					// 如果temp符合条件
					if (temp.lTree == null) {
						// 如果其存在左子树,将其左子树移到其原来的位置
						if (temp.rTree != null) {
							if (temp.pTree.rTree == temp) {
								temp.pTree.rTree = temp.rTree;
								temp.rTree.pTree = temp.pTree;
							} else {
								temp.pTree.lTree = temp.rTree;
								temp.lTree.pTree = temp.pTree;
							}
						}
						// 将其移到目标的空缺位置
						current.rTree.pTree = temp;
						current.lTree.pTree = temp;
						temp.lTree = current.lTree;
						temp.rTree = current.rTree;
						current = null;
						return; // 删除完毕
					}
					temp = temp.lTree;
				}
			}
			//左右都无子树,直接删掉即可
			else {
				current = null;
				return;// 删除完毕
			}
		}
	}
}
———————————————————————————————————————————————————————————
主要用到C的指针思想
添加节点的方法和思想很简单就不说了。不懂百度。
删除节点:
    1、先找到该节点所在位置(二分法查找应该很快捷);
    2、判断目标节点有无左右子树,如果有左子树,就选取左子树最右边的一个节点来替换目标节点。(使用递归来获取)
    3、如果目标节点无左子树,就选取其右子树最左边的节点来替换目标节点。(类似于步骤2,也采用递归来获取)
    4、找到替换的后,还要判断该替换的节点有无子节点,有字节要将该字节先移到该节点所在位置,然后再进行替换操作。
    5、将要替换的节点置空,等待垃圾回收器回收他。
________________________________________________________________________________________________________________
先存档,有问题以后再说。。。心情不好,就不验证能否正常使用了(反正主要学思想就行了) 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值