---
title: "Binary Searching Tree"
author: "taimukang"
date: "2012-08-26T23:12:14Z"
tags: []
categories: ["编程语言"]
slug: "binary-searching-tree"
draft: false
lastmod: "2012-08-26T23:15:46Z"
id: 1663411
url: "https://taimukang.iteye.com/blog/1663411"
---

a **binary search tree** (**BST** ), which may sometimes also be called an **ordered** or **sorted binary tree** , is a [node-based](http://en.wikipedia.org/wiki/Node_%28computer_science%29 "Node (computer science)") [binary tree](http://en.wikipedia.org/wiki/Binary_tree "Binary tree") [data structure](http://en.wikipedia.org/wiki/Data_structure "Data structure") which has the following properties:[](http://en.wikipedia.org/wiki/Binary_search_tree#cite_note-0)

- The left [subtree](http://en.wikipedia.org/wiki/Tree_%28data_structure%29#subtrees "Tree (data structure)") 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](http://en.wikipedia.org/wiki/Sorting_algorithm "Sorting algorithm") and [search algorithms](http://en.wikipedia.org/wiki/Search_algorithm "Search algorithm") such as [in-order traversal](http://en.wikipedia.org/wiki/In-order_traversal "In-order traversal") can be very efficient.

Binary search trees are a fundamental [data structure](http://en.wikipedia.org/wiki/Data_structure "Data structure") used to construct more abstract data structures such as [sets](http://en.wikipedia.org/wiki/Set_%28computer_science%29 "Set (computer science)") , [multisets](http://en.wikipedia.org/wiki/Set_%28computer_science%29#Multiset "Set (computer science)") , and [associative arrays](http://en.wikipedia.org/wiki/Associative_array "Associative array") .

 

Implements in Java.

 

Tree Node:

public class Node&lt;E extends Comparable&lt;E&gt;&gt;{  
E key = null;  
//parent node  
Node&lt;E&gt; parent;  
//left child node  
Node&lt;E&gt; lchild;  
//right child node  
Node&lt;E&gt; 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;
	}
}
```

 

 

 

 

 

 

 

```

```