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

//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;
	}	



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉搜索树,也叫二叉查找树(BST,Binary Search Tree),是一种特殊的二叉树。相对于普通的二叉树,它有一个特点:所有左子树的节点都比根节点小,所有右子树的节点都比根节点大。因此,它可以快速地进行查找、插入、删除等操作。 具体来说,二叉搜索树的定义如下: - 节点的左子树中所有节点的值都小于该节点的值。 - 节点的右子树中所有节点的值都大于该节点的值。 - 左右子树也都是二叉搜索树。 如下图所示,就是一个二叉搜索树的例子: ``` 8 / \ 3 10 / \ \ 1 6 14 / \ / 4 7 13 ``` 在这个树中,每个节点都满足左子树的节点值小于该节点的值,右子树的节点值大于该节点的值。比如,节点 3 的左子树是 1 和 6,右子树是 4 和 7,都满足要求。 二叉搜索树的主要操作包括查找、插入和删除。 查找操作可以通过递归或者循环实现。递归实现如下: ``` def search(root, val): if not root or root.val == val: return root if root.val > val: return search(root.left, val) else: return search(root.right, val) ``` 插入操作需要先查找到插入的位置,然后创建一个新节点插入到该位置: ``` def insert(root, val): if not root: return TreeNode(val) if root.val > val: root.left = insert(root.left, val) else: root.right = insert(root.right, val) return root ``` 删除操作比较复杂,需要考虑多种情况。如果要删除的节点只有一个子节点,直接将其子节点替换上来即可。如果要删除的节点有两个子节点,可以找到其右子树中的最小节点(或者左子树中的最大节点)来替换该节点,然后再删除该最小节点(或者最大节点)。 ``` def delete(root, val): if not root: return None if root.val == val: if not root.left: return root.right elif not root.right: return root.left else: # 找到右子树中的最小节点 p = root.right while p.left: p = p.left root.val = p.val root.right = delete(root.right, p.val) elif root.val > val: root.left = delete(root.left, val) else: root.right = delete(root.right, val) return root ``` 需要注意的是,在删除节点时,要保证删除后的树仍然是二叉搜索树。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值