Binary Searching Tree

a binary search tree (BST ), which may sometimes also be called an ordered or sorted binary tree , is a node-based binary tree data structure which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

Generally, the information represented by each node is a record rather than a single data element. However, for sequencing purposes, nodes are compared according to their keys rather than any part of their associated records.

The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order traversal can be very efficient.

Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets , multisets , and associative arrays .

 

Implements in Java.

 

Tree Node:

public class Node<E extends Comparable<E>>{
E key = null;
//parent node
Node<E> parent;
//left child node
Node<E> lchild;
//right child node
Node<E> rchild;
Node(E key){
this.key = key;
}
}
 

BSTree:

public class BSTree<T extends Comparable<T>> {
	
	private Node<T> root = null;

	public BSTree(){}
	
	public Node<T> getRoot() {
		return root;
	}
	
	public boolean insert(T value){
		if(value == null){
			return false;
		}
		if(root==null){
			root = new Node<T>(value);
			return true;
		}else{
			return BSTInsert(root,value);
		}
	}
	
	private boolean BSTInsert(Node<T> node,T value){
		int result = node.key.compareTo(value);
		if(result == 0){
			throw new RuntimeException("the value has already existed");
		}else if(result >0){
			if(node.lchild==null){
				node.lchild = new Node<T>(value);
				node.lchild.parent = node;
				return true;
			}else{
				return BSTInsert(node.lchild, value);
			}
		}else{
			if(node.rchild==null){
				node.rchild = new Node<T>(value);
				node.rchild.parent = node;
				return true;
			}else{
				return BSTInsert(node.rchild, value);
			}
		}
	}
	
	public boolean search(T value){
		if(value==null||root==null){
			return false;
		}
		return (BSTSearch(root,value)==null);
	}
	
	private Node<T> BSTSearch(Node<T> node, T value){
		int result = node.key.compareTo(value);
		if(result == 0){
			return node;
		}else if(result >0){
			return BSTSearch(node.lchild,value);
		}else{
			return BSTSearch(node.rchild,value);
		}
	}
	
	public boolean delete(T value){
		if(root==null||value == null){
			return false;
		}
		Node<T> delNode = BSTSearch(root, value);
		if(delNode == null){
			return false;
		}
		Node<T> parent = delNode.parent;
		if(delNode.rchild==null){
			if(parent.lchild.key == value){
				parent.lchild = delNode.lchild;
			}else{
				parent.rchild = delNode.lchild;
			}
			return true;
		}
		if(delNode.lchild==null){
			if(parent.lchild.key == value){
				parent.lchild = delNode.rchild;
			}else{
				parent.rchild = delNode.rchild;
			}
			return true;
		}
		// if either the left child nor the right child is null
		Node<T> p = delNode;
		Node<T> s = delNode.lchild;
		// find the most right node of s, and p is the parent of s
		while(s.rchild!=null){
			p = s;
			s = s.rchild;
		}
		//replace the value of delNode with the value of s
		delNode.key = s.key;
		//rebuild the left child 
		if(p!=delNode){
			p.rchild = s.lchild;
		}else{
			p.lchild = s.lchild;
		}
		s.lchild.parent = p;
		s.parent = null;
		s.lchild = null;
		return true;
	}
}
 

 

 

 

 

 

 


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值