您的位置:首页 > 理论基础 > 数据结构算法

数据结构:树、二叉树、二叉查找树

2016-01-08 16:22 309 查看
树是有一些节点组成的集合。该集合可以为空集,若不为空集,则树是有一个根节点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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: