- 樹是有一些節點組成的集合。該集合可以爲空集,若不爲空集,則樹是有一個根節點r以及n(n>=0)個子樹組成,子樹的根都來自於r的一條有向邊。
- 一棵樹有N個節點和N-1條邊組成,除去根節點每一個節點都有一個父節點。
- 沒有子節點的節點稱爲樹葉。
- 從節點n₁到nk的路徑定義爲節點n₁,n₂… …nk的一個序列,使對於
(1≤i<k)
的節點ni是n(i+1)的父親,這條路徑的長爲該路徑上的邊數,即k-1.每個節點到他自己都有一個長度爲0的路徑。
- 對於任意節點n,n的深度爲從根到n的唯一路徑長。根的深度爲0. n的高度爲從n到一片葉子的最長路徑的長。一棵樹的高度等於他的根的高。
- 二叉樹是每一個節點都不能有對於兩個子節點的樹。
- 二叉查找樹:一種特殊的二叉樹,他的每一個節點X的左子樹中的所有項的值小於X中的值,而他的右子樹中所有項的值大於X中的項。他的平均深度爲O(logN).
- 二叉查找樹代碼:
package com.algith;
import java.util.Random;
public class BinarySearchTree<T extends Comparable<? super T>> {
private static class BinaryNode<T>{
private T element;
private BinaryNode<T> leftChild;
private BinaryNode<T> rightChild;
public BinaryNode(T theElement) {
this(theElement,null,null);
}
BinaryNode(T theElement,BinaryNode<T> leftChild,BinaryNode<T> rightChild){
element = theElement;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
}
private BinaryNode<T> root;
public BinarySearchTree(){
root = null;
}
public void makeEmpty(){
root = null;
}
public boolean isEmpty(){
return root == null;
}
public boolean cotains(T x){
return cotains(x,root);
}
private boolean cotains(T x, BinaryNode<T> root2) {
if(root2 == null){
return false;
}
int compareResult = x.compareTo(root2.element);
if(compareResult>0){
return cotains(x, root2.rightChild);
}else if(compareResult<0){
return cotains(x, root2.leftChild);
}else{
return true;
}
}
public T findMin(){
return findMin(root).element;
}
private BinaryNode<T> findMin(BinaryNode<T> t) {
if(t == null){
return null;
}else if(t.leftChild==null){
return t;
}
return findMin(t.leftChild);
}
public T findMax(){
return findMax(root).element;
}
private BinaryNode<T> findMax(BinaryNode<T> t) {
if(t==null){
return null;
}
while(t.rightChild!=null){
t = t.rightChild;
}
return t;
}
public void insert(T x){
root = insert(x,root);
}
private BinaryNode<T> insert(T x, BinaryNode<T> t) {
if(t==null){
return new BinaryNode<T>(x, null, null);
}
int compareResult = x.compareTo(t.element);
if(compareResult<0){
t.leftChild = insert(x, t.leftChild);
}else if(compareResult>0){
t.rightChild = insert(x, t.rightChild);
}
return t;
}
public void remove(T x){
root = remove(x,root);
}
private BinaryNode<T> remove(T x, BinaryNode<T> t) {
if(t==null)return null;
int compareResult = x.compareTo(t.element);
if(compareResult<0){
t.leftChild = remove(x,t.leftChild);
}else if(compareResult>0){
t.rightChild = remove(x, t.rightChild);
}else if(t.leftChild!=null&&t.rightChild!=null){
t.element = findMin(t.rightChild).element;
t.rightChild = remove(t.element, t.rightChild);
}else{
t = (t.leftChild!=null)?t.leftChild:t.rightChild;
}
return t;
}
public void printTree(){
if(isEmpty()){
System.out.println("Empty tree");
}else{
printTree(root);
}
}
private void printTree(BinaryNode<T> t){
if(t!=null){
printTree(t.leftChild);
System.out.println("Node's value is "+ t.element);
printTree(t.rightChild);
}else{
System.out.println("T is null");
}
}
public static void main(String[] args) {
BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();
tree.insert(1);
tree.insert(6);
tree.insert(2);
tree.insert(9);
tree.printTree();
}
}