当前位置: 首页 > news >正文

算法导论学习笔记——二叉查找树

//SearchTree类
public class SearchTree {

	/**
	 * 中序遍历:根据二叉查找树的性质,中序遍历将按排列顺序输出树中的所有关键字。
	 * @param root
	 */
	public static void inorderTree(Node root){
		if(root!=null){
			inorderTree(root.getLchild());
			System.out.println(root.getKey());
			inorderTree(root.getRchild());
		}
	}
	/**
	 * 关键字查询:该过程从树的根结点开始进行查找,并沿着树下降。
	 * 对碰到的每个结点,比较其关键字。如果两个关键字相同,则查找结束;
	 * 如果结点关键字大于查询关键字,则继续查找结点的左子树;如果结点关键字小于或等于查询关键字,则继续查找结点的右子树。
	 * @param root
	 * @param value
	 * @return
	 */
	public static Node treeSearch(Node root,int value){
		Node temp = root;
		while(temp!=null&&temp.getKey()!=value){
			if(value<temp.getKey())
				temp = temp.getLchild();
			else
				temp = temp.getRchild();
		}
		if(value==temp.getKey())
			return temp;
		else
			return null;
	}
	/**
	 * 最大关键字结点:要查找二叉树中具有最大关键字的结点,
	 * 只要从根结点开始,沿着各结点的right指针查询下去,直到遇到NULL为止。
	 * @param root
	 * @return
	 */
	public static Node treeMax(Node root){
		Node temp = root;
		while(null!=temp.getRchild()){
			temp = temp.getRchild();
		}
		return temp;
	}
	/**
	 *最小关键字结点: 要查找二叉树中具有最小关键字的结点,只要从根结点开始,沿着各结点的left指针查询下去,直到遇到NULL为止。
	 * @param root
	 * @return
	 */
	public static Node treeMin(Node root){
		Node temp = root;
		while(null!=temp.getLchild()){
			temp = temp.getLchild();
		}
		return temp;
	}
	/**
	 * 前趋结点:给定一个二叉查找树中的结点,它的前趋结点是在中序遍历顺序下它的前一个结点。
	 * 如果给定查询结点的左子树不为空,则它的前趋结点是左子树中的最大关键字结点;
	 * 如果左子树为空,需要向上判断每一个父结点,直到父结点为空或者遍历结点是父结点的右子结点时,根据中序遍历性质,该父结点即为查询结点的前趋。
	 * @param root
	 * @return
	 */
	public static Node treePredecessor(Node node){
		
		Node preNode = null;
		Node tempNode = null;
		if(node.getLchild()!=null)
			return treeMax(node.getLchild());
		System.out.println("----");
		preNode = node.getParent();
		tempNode = node;
		while(preNode!=null&&preNode.getRchild()!=tempNode){
			tempNode = preNode;
			preNode = preNode.getParent();
		}
		return preNode;
	}
	/**
	 * 后继结点:与前趋结点相反,它是在中序遍历顺序下的后一个结点。
	 * 如果给定查询结点的右子树不为空,则它的后继结点是右子树中的最小关键字结点;
	 * 如果右子树为空,需要向上判断每一个父结点,直到父结点为空或者遍历结点是父结点的左子结点时,则该父结点即为查询结点的后继。
	 * @param node
	 * @return
	 */
	public static Node treeSuccessor(Node node){
		Node preNode = null;
		Node tempNode = null;
		if(node.getRchild()!=null)
			return treeMin(node.getRchild());
		preNode = node.getParent();
		tempNode = node;
		while(preNode!=null&&preNode.getLchild()!=tempNode){
			tempNode = preNode;
			preNode = preNode.getParent();
		}
		return preNode;
	}
	/**
	 * 插入操作:插入操作从根结点开始,沿树下降,并不断比较当前结点与要插入结点的关键字,
	 * 以决定向左转或向右转,直到当前结点为空,这个空所在的位置即是将要插入的位置。
	 * 最后,修改插入结点的父结点指针,并根据与父结点关键字的比较结果,修改父结点的子结点指针。
	 * 如果父结点为空,说明二叉查找树是空的,这时,将新插入结点作为树的根结点。
	 * 由于每次都将新结点插入到叶子结点的位置,通过这种插入操作构建的二叉查找树往往很不平衡,高度值较大,使得查询操作的运行时间较大。
	 * @param root
	 * @param node
	 */
	public static void treeInsert(Node root,Node node){
		Node tempNode = root;
		Node parentNode = null;
		while(tempNode!=null){
			parentNode = tempNode;
			if(node.getKey()<tempNode.getKey())
				tempNode = tempNode.getLchild();
			else
				tempNode = tempNode.getRchild();
		}
		if(null==parentNode)
			root = node;
		else if(node.getKey()<parentNode.getKey()){
			parentNode.setLchild(node);
		}else{
			parentNode.setRchild(node);
		}
		node.setParent(parentNode);
	}
	/**
	 * 删除操作有三种情况:
         * 1)如果要删除的结点没有子结点,则只要修改其父结点的对应子结点指针为空;
	 * 2)如果要删除的结点只有一个子结点,则可以通过其子结点与其父结点间建立一条链接来删除该结点;
	 * 3)如果要删除的结点有两个子结点,可以先删除该结点的后继结点(这里的后继结点一定在右子树中,并且该后继结点不可能有左子结点),
	 *      再用后继结点的内容来替换要删除结点的内容(实际上,这里使用前趋结点来替换也是可行的)。
	 * 下面的操作分为四个步骤:
	 *  1)确定要删除的结点(当没有子结点或只有一个子结点时就是该结点本身;当有两个子结点时就是该结点的后继结点);
	 *  2)确定要删除结点的子结点,这里,要么没有子结点,要么只有一个;
	 *  3)在要删除结点的父子结点之间建立链接,如果父结点为空,说明要删除的是树的根结点;
	 *  4)如果删除的是后继结点,则进行内容替换。
	 * @param root
	 * @param node
	 */
	public static void treeDelete(Node root,Node node){
		Node tempNode = null;
		Node childNode = null;
		//步骤1
		if(node.getLchild()==null||node.getRchild()==null)
			tempNode = node;
		else
			tempNode = treeSuccessor(node);
		//步骤2
		if(tempNode.getLchild()!=null)
			childNode = tempNode.getLchild();
		else
			childNode = tempNode.getRchild();
		//步骤3
		if(childNode!=null)
			childNode.setParent(tempNode.getParent());
		if(tempNode.getParent()==null)
			root = childNode;
		else if(tempNode==tempNode.getParent().getLchild())
			tempNode.getParent().setLchild(childNode);
		else
			tempNode.getParent().setRchild(childNode);
		//步骤4
		if(tempNode!=node)
			node.setKey(tempNode.getKey());
	}
	//test.....
	public static void main(String args[]){
		int arr[]={15,5,3,12,10,13,6,7,16,20,18,23};
		Node root = new Node(15);
		for(int i = 1;i<arr.length;i++)
			treeInsert(root,new Node(arr[i]));
/*		inorderTree(root);*/
/*		Node temp = treeSearch(root,18);
 * 		if(null!=temp)
		System.out.println(temp.getKey());*/
/*		Node temp = treeMax(root);
 *      if(null!=temp)
		System.out.println(temp.getKey());*/
/*		Node temp = treeMin(root);
 * 		if(null!=temp)
		System.out.println(temp.getKey());*/
		Node node = treeSearch(root,5);
		Node temp = treePredecessor(node);
		if(null!=temp)
		System.out.println(temp.getKey());
/*		Node temp = treeSuccessor(node);
		if(null!=temp)
		System.out.println(temp.getKey());*/
/*		treeDelete(root,new Node(5));
		inorderTree(root);*/
	}	
}

Node类:

public class Node {

	private int key;
	private Node lchild;
	private Node rchild;
	private Node parent;
	
	public Node(int key){
		this.key = key;
		this.lchild = null;
		this.rchild = null;
		this.parent = null;
	}
	public Node(int key,Node lchild,Node rchild,Node parent){
		this.key = key;
		this.lchild = lchild;
		this.rchild = rchild;
		this.parent = parent;
	}
	public int getKey() {
		return key;
	}
	public void setKey(int key) {
		this.key = key;
	}
	public Node getLchild() {
		return lchild;
	}
	public void setLchild(Node lchild) {
		this.lchild = lchild;
	}
	public Node getRchild() {
		return rchild;
	}
	public void setRchild(Node rchild) {
		this.rchild = rchild;
	}
	public Node getParent() {
		return parent;
	}
	public void setParent(Node parent) {
		this.parent = parent;
	}	



相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • win10安装blueCFD
  • 算法导论学习笔记——红黑树
  • 理解Linux文件系统
  • 沙盒SandBox
  • Python数据类型间互转(字典、字符串、列表、元组)
  • 输入n个整数,输出其中最小的k个
  • cocos2dx 3.x 精灵重叠时点击最上层的精灵
  • 输入一个整型数组,求所有子数组中和的最大值
  • 软件测试知识之分类
  • 算法导论学习笔记——贪心算法
  • web前端开发浏览器兼容性处理大全
  • 算法导论学习笔记——动态规划
  • Google技术学习
  • 【无聊放个模板系列】 半平面交
  • 最长公共子序列(LCS)问题
  • 分享一款快速APP功能测试工具
  • [case10]使用RSQL实现端到端的动态查询
  • [译]如何构建服务器端web组件,为何要构建?
  • CSS盒模型深入
  • docker-consul
  • ECMAScript6(0):ES6简明参考手册
  • Hibernate【inverse和cascade属性】知识要点
  • HTTP中GET与POST的区别 99%的错误认识
  • magento 货币换算
  • Python语法速览与机器学习开发环境搭建
  • Redis字符串类型内部编码剖析
  • vue 配置sass、scss全局变量
  • 回顾2016
  • 技术:超级实用的电脑小技巧
  • 如何解决微信端直接跳WAP端
  • 如何在GitHub上创建个人博客
  • 算法之不定期更新(一)(2018-04-12)
  • 微信开源mars源码分析1—上层samples分析
  • 云栖大讲堂Java基础入门(三)- 阿里巴巴Java开发手册介绍
  • 走向全栈之MongoDB的使用
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • 【云吞铺子】性能抖动剖析(二)
  • # Panda3d 碰撞检测系统介绍
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • #VERDI# 关于如何查看FSM状态机的方法
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (react踩过的坑)antd 如何同时获取一个select 的value和 label值
  • (第61天)多租户架构(CDB/PDB)
  • (分享)自己整理的一些简单awk实用语句
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (三分钟了解debug)SLAM研究方向-Debug总结
  • (十七)Flask之大型项目目录结构示例【二扣蓝图】
  • (算法)Game
  • (一)插入排序
  • (转)Android中使用ormlite实现持久化(一)--HelloOrmLite
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • .net core IResultFilter 的 OnResultExecuted和OnResultExecuting的区别
  • .NET Core 中插件式开发实现