欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java Tree,

来源: javaer 分享于  点击 16765 次 点评:95

Java Tree,


节点的结构

public class TreeNode {

	int val;
	TreeNode left;
	TreeNode right;
	
	public TreeNode(int x){
		val = x;
	}
	public TreeNode(){
		
	}
}

给定数据集,构造BST

public TreeNode BSTinsert(TreeNode root, int element){
		
		if(null == root){
			root = new TreeNode(element);
			root.left = null;
			root.right = null;
			count++;
			return root;
		}
		if(element == root.val)
			return null;
		if(element < root.val)
			root.left = BSTinsert(root.left, element);
		else
			root.right = BSTinsert(root.right, element);
		return root;
				
	}
	
	public boolean createBST(int[] data){
		
		for(int element: data){
			if((root=BSTinsert(root,element)) ==null)
				return false;
		//	System.out.println("insert: "+ element);
		}
		return true;
	}

中间结构

class MyQueue<T>{
	private LinkedList<T> queue; 
	public  MyQueue(){
		queue = new LinkedList<T>();
	}
	public void add(T t){
		queue.addFirst(t);
	}
	public T poll(){
		return queue.removeLast();
	}
	public boolean isEmpty(){
		return queue.isEmpty();
	}
}

class MyStack<T>{
	private LinkedList<T> data;
	public MyStack(){
		data = new LinkedList<T>();
	}
	public T pop(){
		return data.removeFirst();
	}
	public void push(T t){
		 data.addFirst(t);
	}
	public T getTop(){
		return data.getFirst();
	}
	public boolean isEmpty(){
		return data.isEmpty();
	}
}

递归遍历BST

<pre name="code" class="java">	public void inOrderTraverse(TreeNode root){
		if(null !=root){
			inOrderTraverse(root.left);
			System.out.println(root.val);
			inOrderTraverse(root.right);
		}
	}



非递归遍历BST

	public void inOrderTraverseWithoutRec(BST bst){
		
		MyStack<TreeNode> TreeNodes =  new MyStack<TreeNode>();
		TreeNode root = bst.root;
		TreeNode currantNode = root;
		while(currantNode!=null || !TreeNodes.isEmpty()){
			while(currantNode!=null){
				TreeNodes.push(currantNode);
				currantNode = currantNode.left;
			}
			
			if(TreeNodes.isEmpty())
				return;
			else{
				currantNode = TreeNodes.pop();
				System.out.println(currantNode.val);
				currantNode = currantNode.right;
			}
		}
	}

	public void PreOrderTraverseWithoutRec(BST bst){
			
		MyStack<TreeNode> TreeNodes =  new MyStack<TreeNode>();
		TreeNode root = bst.root;
		TreeNode currantNode = root;
		while(currantNode!=null || !TreeNodes.isEmpty()){
			while(currantNode!=null){
				System.out.println(currantNode.val);
				TreeNodes.push(currantNode);
				currantNode = currantNode.left;
			}
			
			if(!TreeNodes.isEmpty()){
				currantNode = TreeNodes.pop();
				currantNode = currantNode.right;
			}
		}
	}
	
	public void postOrderTraverWithoutRec(BST bst){
		
		MyStack<TreeNode> TreeNodes =  new MyStack<TreeNode>();
		TreeNode root = bst.root;
		TreeNode currantNode = root;
		while(currantNode!=null){
			
			for(;currantNode.left!=null;currantNode = currantNode.left)
				TreeNodes.push(currantNode);
			
			while(currantNode!=null &&(currantNode.right==null || currantNode.right==root)){
				System.out.println(currantNode.val);
				root = currantNode;
				if(TreeNodes.isEmpty())
					return;
				currantNode = TreeNodes.pop();
			}
			TreeNodes.push(currantNode);
			currantNode = currantNode.right;
		}
	}
	
	public void LevelOrder(BST bst){
		
		MyQueue<TreeNode> BSTreeNode = new MyQueue<TreeNode>();
		if(bst.root ==null)
			return;
		BSTreeNode.add(bst.root);
		while(!BSTreeNode.isEmpty()){
			TreeNode currantNode = BSTreeNode.poll();
			System.out.println(currantNode.val);
			if(currantNode.left!=null)
				BSTreeNode.add(currantNode.left);
			if(currantNode.right!=null)
				BSTreeNode.add(currantNode.right);
		}
	}
	


BST中第K个最小的数

	
	public int kthSmallest(TreeNode root, int k) {
		if(size(root.left) == k-1)
			return root.val;
		
		else if(size(root.left) < k-1)
			return kthSmallest(root.right, k-(size(root.left)+1));
		
		else
			return kthSmallest(root.left, k);	
    }
	
	public int size(TreeNode root){
		if(root == null)
			return 0;
		if(null == root.left && null == root.right)
			return 1;
		else
			return size(root.left)+size(root.right)+1;
	}
	


 

相关文章

    暂无相关文章
相关栏目:

用户点评