Structure.BSTree(二叉树、排序树、平衡树、SB树)

二叉树

BBTree 最优二叉树,包含Huffman编码

BSTree 二叉排序树

SBT SizedBalancedTree 平衡树的一种,acm竞赛中比较常用的数据结构,请参考陈启峰大牛的论文,SBT文件夹。比红黑树代码少了3/4

package Structure.BSTree;
import java.util.Collections;
import java.util.Vector;
import java.util.Stack;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
class NodeInfo implements Comparable<NodeInfo>
{
	char letter_;
	int num_;
	public NodeInfo(char letter,int num){letter_ = letter ; num_ = num;}
	@Override
	public int compareTo(NodeInfo o) {
		// TODO Auto-generated method stub
		return num_ - o.num_;
	}
}

public class BBTree{
	private TreeNode<NodeInfo> root_;
	public BBTree(){root_ = null;}
	
	public void BuildBestTree(Vector<NodeInfo> v)
	{
		int size = v.size();
		for(int i = 0 ; i < size-1;++i)
		{
			Collections.sort(v);
			NodeInfo one = v.get(0);
			NodeInfo two = v.get(1);
			NodeInfo root = new NodeInfo(' ',(one.num_+two.num_));
			if(root_ == null)
			{
				root_ = new TreeNode<NodeInfo>(root,null,null);
				root_.left_ = new TreeNode<NodeInfo>(one,null,null);
				root_.right_ = new TreeNode<NodeInfo>(two,null,null);
			}
			else
			{
				TreeNode<NodeInfo> n = new TreeNode<NodeInfo>(root,null,root_);
				n.left_ = new TreeNode<NodeInfo>(one,null,null);
				root_ = n;
			}
			v.remove(one);
			v.remove(two);
			v.add(root);
		}
	}
	
	public void HuffmanCode()
	{
		Stack<Count<NodeInfo> > stack = new Stack<Count<NodeInfo> >();
		stack.push(new Count<NodeInfo>(root_));
		while(!stack.isEmpty())
		{
			Count<NodeInfo> cur = stack.peek();
			if(cur.num == 0)
			{
				if(cur.node_.left_ != null) stack.push(new Count<NodeInfo>(cur.node_.left_));
				cur.num++;
			}
			else if(cur.num == 1)
			{
				if(cur.node_.right_ != null) stack.push(new Count<NodeInfo>(cur.node_.right_));
				cur.num++;
			}
			else if(cur.num == 2)
			{
				if(cur.node_.data_.letter_ != ' ')
				{
					StringBuffer s = new StringBuffer();
					for(int i = 0 ; i < stack.size() -1 ; ++i)
					{
						Count<NodeInfo> father = stack.get(i);
						Count<NodeInfo> son = stack.get(i+1);
						if(father.node_.right_ == son.node_)
						{
							s.append('1');
						}
						else if(father.node_.left_ == son.node_)
						{
							s.append('0');
						}
					}
					System.out.println(cur.node_.data_.letter_ + ": " + s);
				}
				stack.pop();
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vector<NodeInfo> v = new Vector<NodeInfo>();
		v.add(new NodeInfo('C',2));
		v.add(new NodeInfo('E',7));
		v.add(new NodeInfo('S',4));
		v.add(new NodeInfo('T',5));
		
		BBTree tree = new BBTree();
		tree.BuildBestTree(v);
		
		System.out.println();
		tree.HuffmanCode();
	}

}

package Structure.BSTree;
import java.util.Iterator;
import java.util.Stack;
import java.util.LinkedList;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
class TreeNode<E extends Comparable<E> >
{
	TreeNode<E> left_;
	TreeNode<E> right_;
	E data_;
	public TreeNode(E data,TreeNode<E> left,TreeNode<E> right)
	{
		data_ = data;
		left_ = left;
		right_ = right;
	}
}

class Count<E extends Comparable<E> >
{
	TreeNode<E> node_;
	int num;
	public Count(TreeNode<E> node){node_ = node; num = 0;}
}

public class BSTree<E extends Comparable<E> > {
	private TreeNode<E> root_;
	public BSTree(){root_ = null;}
	
	public TreeNode<E> GetRoot(){return root_;}
	
	public E FindKth(int kth)
	{
		return FindKth(root_,kth);
	}
	
	public void Insert(E value)
	{
		root_ = Insert(root_,value);
	}
	
	public boolean Remove(E value)
	{
		TreeNode<E> node = FindNode(root_,value);
		if( node != null)
		{
			root_ = Remove(root_,node);
			return true;
		}else return false;
	}
	
	public void InPrintByStack()
	{
		Stack<Count<E> > stack = new Stack<Count<E> >();
		stack.push(new Count<E>(root_));
		while(!stack.isEmpty())
		{
			Count<E> cur = stack.peek();
			if(cur.num == 1)
			{
				stack.pop();
				System.out.print(cur.node_.data_+ " ");
				if(cur.node_.right_ != null)
					stack.push(new Count<E>(cur.node_.right_));
			}
			else if(cur.num == 0)
			{
				if(cur.node_.left_ != null)
					stack.push(new Count<E>(cur.node_.left_));
				cur.num++;
			}
		}
		System.out.println();
	}
	
	public void PrePrintByStack()
	{
		Stack<TreeNode<E> > stack = new Stack<TreeNode<E>>();
		stack.push(root_);
		while(!stack.isEmpty())
		{
			TreeNode<E> cur = stack.peek();stack.pop();
			if(cur.right_ != null ) stack.push(cur.right_);
			if(cur.left_ != null) stack.push(cur.left_);
			System.out.print(cur.data_+" ");
		}
		System.out.println();
	}
	
	public void PostPrintByStack()
	{
		Stack<Count<E> > stack = new Stack<Count<E> >();
		stack.push(new Count<E>(root_));
		while(!stack.isEmpty())
		{
			Count<E> cur = stack.peek();
			if(cur.num == 2)
			{
				stack.pop();
				System.out.print(cur.node_.data_ + " ");
			}
			else if(cur.num == 1)
			{
				if(cur.node_.right_ != null)
					stack.push(new Count<E>(cur.node_.right_));
				cur.num++;
			}
			else if(cur.num == 0)
			{
				if(cur.node_.left_ != null)
					stack.push(new Count<E>(cur.node_.left_));
				cur.num++;
			}
		}
		System.out.println();
	}
	
	public void LevelPrintByQueue()
	{
		LevelPrintByQueue(root_);
	}
	
	public void PrePrint()
	{
		PrePrint(root_);
	}
	
	public void InPrint()
	{
		InPrint(root_);
	}
	
	public void PostPrint()
	{
		PostPrint(root_);
	}
	
	public int Height(){return Height(root_);}
	public int Size(){return Size(root_);}
	
	private void LevelPrintByQueue(TreeNode<E> root)
	{
		if(root == null) return;
		LinkedList<TreeNode<E>> upLevel = new LinkedList<TreeNode<E>>();
		LinkedList<TreeNode<E>> downLevel = new LinkedList<TreeNode<E>>();
		upLevel.add(root);
		while(!upLevel.isEmpty())
		{
			Iterator<TreeNode<E>> it = upLevel.iterator();
			while(it.hasNext())
			{
				TreeNode<E> temp = it.next();
				if(temp.left_ != null) downLevel.add(temp.left_);
				if(temp.right_ != null) downLevel.add(temp.right_);
			}
			while(!upLevel.isEmpty())
			{
				TreeNode<E> temp = upLevel.removeFirst();
				System.out.print(temp.data_+" ");
			}
			upLevel = downLevel;
			downLevel = new LinkedList<TreeNode<E>>();
		}
	}
	
	private E FindKth(TreeNode<E> root,int kth)
	{
		if(root == null) return null;
		int lcount = Size(root.left_);
		if(lcount + 1 == kth){return  root.data_;}
		else if( lcount + 1 > kth)
		{
			return FindKth(root.left_,kth);
		}else 
			return FindKth(root.right_,kth-lcount-1);
	}
	
	private TreeNode<E> Remove(TreeNode<E> root,TreeNode<E> node)
	{
		if(root == null) return null;
		else
		{
			if(root.data_.compareTo(node.data_) == 0 )
			{
				if(root.left_ == null) 
				{
					root = root.right_;
				}
				else if(root.right_ == null)
				{
					root = root.left_;
				}
				else
				{
					TreeNode<E> t = FindMin(root.right_);
					root.right_ = Remove(root.right_,t);
					t.left_ = root.left_;
					t.right_ = root.right_;
					root = t;
				}
			}
			else if(root.data_.compareTo(node.data_) > 0)
			{
				root.left_ = Remove(root.left_,node);
			}else
			{
				root.right_ = Remove(root.right_,node);
			}
		}
		return root;
	}
	
	private TreeNode<E> FindNode(TreeNode<E> root,E value)
	{
		if(root == null) return null;
		if(root.data_.compareTo(value) == 0) return root;
		else
		{
			if(root.data_.compareTo(value) > 0) return FindNode(root.left_,value);
			else return FindNode(root.right_,value);
		}
	}
	
	private TreeNode<E> FindMin(TreeNode<E> root)
	{
		if(root.left_ == null) return root;
		else
		{
			TreeNode<E> t = root.left_;
			while(t.left_ != null) t = t.left_;
			return t;
		}
	}
	
	private void PostPrint(TreeNode<E> root)
	{
		if(root == null) return;
		PostPrint(root.left_);
		PostPrint(root.right_);
		System.out.print(root.data_+" ");
	}
	
	private void InPrint(TreeNode<E> root)
	{
		if(root == null) return;
		InPrint(root.left_);
		System.out.print(root.data_ +" ");
		InPrint(root.right_);
	}
	
	private void PrePrint(TreeNode<E> root)
	{
		if(root == null) return;
		System.out.print(root.data_ +" ");
		PrePrint(root.left_);
		PrePrint(root.right_);
	}
	
	private int MaxOfTwo(Integer first,Integer second)
	{
		return (first.compareTo(second)<= 0)?second:first;
	}
	
	private int Height(TreeNode<E> root)
	{
		if(root == null) return 0;
		else 
		return 1+MaxOfTwo(Height(root.left_),Height(root.right_));
	}
	
	private int Size(TreeNode<E> root)
	{
		if(root == null) return 0;
		else
		return Size(root.left_)+Size(root.right_)+1;
	}
	
	private TreeNode<E> Insert(TreeNode<E> root,E value)
	{
		if(root == null)
		{
			root = new TreeNode<E>(value,null,null);
		}
		else
		{
			if(root.data_.compareTo(value) >= 0)
			{
				root.left_ = Insert(root.left_,value);
			}
			else{ root.right_ = Insert(root.right_,value);}
		}
		return root;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BSTree<Integer> tree = new BSTree<Integer>();
		tree.Insert(40);
		tree.Insert(20);
		tree.Insert(30);
		tree.Insert(50);
		tree.Insert(80);
		tree.Insert(100);
		tree.Insert(10);
		tree.Insert(60);
		tree.Insert(70);
		
		tree.InPrint();
		System.out.println();
		tree.PrePrint();
		System.out.println();
		tree.PostPrint();
		System.out.println();
		
		System.out.println(tree.Size());
		System.out.println(tree.Height());
		
		tree.Remove(0);
		tree.InPrint();
		System.out.println();
		
		tree.Remove(40);
		tree.InPrint();
		System.out.println();
		
		tree.Remove(30);
		tree.InPrint();
		System.out.println();
		
		tree.Remove(80);
		tree.InPrint();
		System.out.println();
		
		tree.Insert(10);
		tree.InPrint();
		System.out.println();
		
		tree.Remove(10);
		tree.InPrint();
		System.out.println();
		
		tree.Remove(10);
		tree.InPrint();
		System.out.println();
		System.out.println();
		
		tree.PrePrintByStack();
		tree.PrePrint();
		System.out.println();
		System.out.println();
		
		tree.PostPrintByStack();
		tree.PostPrint();
		System.out.println();
		System.out.println();
		
		tree.InPrintByStack();
		tree.InPrint();
		System.out.println();
		
		tree.LevelPrintByQueue();
	}

}

package Structure.BSTree;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
class SBTreeNode<E extends Comparable<E>> {
	E key;
	SBTreeNode<E> leftChild;
	SBTreeNode<E> rightChild;
	int size;

	public SBTreeNode(E value) {
		key = value;
		leftChild = rightChild = null;
		size = 1;
	}
}

class SBTree<E extends Comparable<E>> {
	private SBTreeNode<E> root;

	public SBTreeNode<E> getRoot() {
		return root;
	}

	public SBTree() {
		root = null;
	}

	public void Insert(E v) {
		root = Insert(root, v);
	}
	
	public void Delete(E v)
	{
		SBTreeNode<E> n = Find(root,v);
		if(n != null)
		{
			root = Delete(root, n);
		}
	}
	
	public boolean Find(E v)
	{
		SBTreeNode<E> n = Find(root,v);
		return n != null ?true:false;
	}
	public E Select(int i)
	{
		SBTreeNode<E> s = Select(root,i);
		return s.key;
	}
	
	public int Size(){return Size(root);}
	public void PreOrder(){PreOrder(root);}
	public E FindMin(){return FindMin(root).key;}
	public E FindMax(){return FindMax(root).key;}
	
	private void PreOrder(SBTreeNode<E> t)
	{
		if(t == null) return;
		PreOrder(t.leftChild);
		System.out.print(t.key+", ");
		PreOrder(t.rightChild);
	}

	private SBTreeNode<E> LR(SBTreeNode<E> t) {
		SBTreeNode<E> k = t.rightChild;
		t.rightChild = k.leftChild;
		k.leftChild = t;
		k.size = Size(t);
		UpdateSize(t);
		return k;
	}
	
	private SBTreeNode<E> RR(SBTreeNode<E> t) {
		SBTreeNode<E> k = t.leftChild;
		t.leftChild = k.rightChild;
		k.rightChild = t;
		k.size = Size(t);
		UpdateSize(t);
		return k;
	}

	private int Size(SBTreeNode<E> t) {
		return t == null ? 0 : t.size;
	}
	
	private void UpdateSize(SBTreeNode<E> t)
	{
		if(t == null) return;
		t.size = Size(t.leftChild) + Size(t.rightChild) + 1;
	}

	private SBTreeNode<E> Maintain(SBTreeNode<E> t, boolean flag)
	{
		if( t == null) return t;
		if( flag == false)
		{
			if( t.leftChild != null )
			{
				if ( Size(t.leftChild.leftChild) > Size(t.rightChild))
					t = RR(t);
				else if( Size(t.leftChild.rightChild) > Size(t.rightChild))
				{
					t.leftChild = LR(t.leftChild);
					t = RR(t);
				}
				else return t;
			}
			else return t;
		}
		else
		{
			if(t.rightChild != null)
			{
				if( Size(t.rightChild.rightChild) > Size(t.leftChild))
					t = LR(t);
				else if( Size(t.rightChild.leftChild) > Size(t.leftChild))
				{
					t.rightChild = RR(t.rightChild);
					t = LR(t);
				}
				else return t;
			}
			else return t;
			
		}
		t.leftChild = Maintain(t.leftChild,false);
		t.rightChild = Maintain(t.rightChild,true);
		t = Maintain(t,false);
		t = Maintain(t,true);
		return t;
	}
	
	private SBTreeNode<E> Insert(SBTreeNode<E> t, E v) {
		if( t == null)
			t = new SBTreeNode<E>(v);
		else
		{
			t.size = t.size + 1;
			if( v.compareTo(t.key) < 0 )
				t.leftChild = Insert(t.leftChild,v);
			else
				t.rightChild = Insert(t.rightChild,v);
		}
		t = Maintain(t, v.compareTo(t.key) >= 0 );
		return t;
	}
	
	private SBTreeNode<E> Select(SBTreeNode<E> x, int i)
	{
		int r = Size(x.leftChild) + 1;
		if( i == r)
			return x;
		else if( i < r)
			return Select(x.leftChild,i);
		else return Select(x.rightChild, i - r);
	}
	
	private SBTreeNode<E> FindMin(SBTreeNode<E> t)
	{
		while( t.leftChild != null)
			t = t.leftChild;
		return t;
	}
	
	private SBTreeNode<E> FindMax(SBTreeNode<E> t)
	{
		while(t.rightChild != null)
			t = t.rightChild;
		return t;
	}
	
	private SBTreeNode<E> Delete(SBTreeNode<E> t,SBTreeNode<E> k)
	{
		if( t == null) return t;
		if( (k.key.compareTo(t.key) == 0) && (t.size == k.size) )
		{
			if(t.rightChild == null)
			{
				k = t.leftChild;
				t = k;
			}
			else
			{
				SBTreeNode<E> tmp = FindMin(t.rightChild);
				t.rightChild = Delete(t.rightChild,tmp);
				tmp.leftChild = t.leftChild;
				tmp.rightChild = t.rightChild;
				t = tmp;
			}
		}
		else
		{
			if( k.key.compareTo(t.key) <= 0)
				t.leftChild = Delete(t.leftChild,k);
			else
				t.rightChild = Delete(t.rightChild,k);
		}
		UpdateSize(t);
		return t;
	}
	
	private SBTreeNode<E> Find(SBTreeNode<E> t,E v)
	{
		if(t == null) return null;
		if( t.key == v) return t;
		else if( t.key.compareTo(v) < 0) return Find(t.rightChild,v);
		else return Find(t.leftChild,v);
	}
}

public class SBT {
	public static void main(String[] args)
	{
		SBTree<Integer> sbt = new SBTree<Integer>();
		sbt.Insert(10);
		sbt.Insert(11);
		sbt.Insert(12);
		sbt.Insert(5);
		sbt.Insert(9);
		sbt.Insert(18);
		sbt.Insert(22);
		sbt.Insert(17);
		
		sbt.PreOrder();
		System.out.println();
		System.out.println(sbt.Select(sbt.Size()));
		System.out.println(sbt.Find(5));
		System.out.println(sbt.Find(6));
		System.out.println(sbt.Find(22));
		
		sbt.Delete(18);
		sbt.PreOrder();
		System.out.println();
		System.out.println(sbt.FindMax());
		System.out.println(sbt.FindMin());
	}
}

    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/qq_34690598/article/details/51175115
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞