Java 构建二叉查找树

代码参考 --《Java语言程序设计(进阶篇) 第八版》--chapter 二叉查找树


1 构建树节点

public class TreeNode {
	int value;
	TreeNode left;
	TreeNode right;
	public TreeNode(int element) {
		// TODO Auto-generated constructor stub
		value=element;
	}
	
	}

2 构建二叉树结构和基本函数

函数列表:

Java 构建二叉查找树_第1张图片

实现代码:



import java.util.ArrayList;

public class MyTree {
	TreeNode root;
	int size=0;
	public MyTree(){
		
	}
	//将数组中元素添加到树节点
	public MyTree(int[] array){
		for(int i=0;icurrent.value){
				parent=current;
				current=current.right;	
			}
			else {
				return false;
			}
				if(elementcurrent.value)
					{
					//parent=current;
					current=current.right;
					}
				else if(element path (int element){
    	//返回从根节点到该节点的路径
    	ArrayList arrayList=new ArrayList();
    	TreeNode current=root;
    	if(current==null)
    		return null;
    	else{
    		while(current!=null){
    			if(element==current.value)
    				{
    				//arrayList.add(current);
    				break;
    				}
    			else if(element>current.value){
    				arrayList.add(current);
    				current=current.right;
    			}
    			else{
    				arrayList.add(current);
    				current=current.left;
    				
    			}
    			
    		}
    		
    	}
		return arrayList;
    	
    	
    }

    public boolean deleNode(int element){
    	TreeNode parent=null;
    	TreeNode current=root;
    	while(current!=null){
    		if(elementcurrent.value)
        		{
        			parent=current;
        			current=current.right;
        		}
    		else break;
    	
    	}
    	//查不到该节点时候,(或者树为空时候)直接返回false
    	if(current==null)
    		return false;
    	//case1:当删除节点的左子树为空时候	
    	if(current.left==null){
    		if(parent==null)
    			root=current.right;
    		else {
    			if(elementparent.value)
    				parent.right=current.right;
    			
    		}
    	}
    	//case2:删除节点存在左子树;
    	else{
    		TreeNode rightMost_father=current;
    		TreeNode rightMost=current.left;
    		//rightmost 是current节点左子树中最大的右节点,且该节点没有右子数了
    		while(rightMost.right!=null)
    			{
    			  rightMost_father=rightMost;
    			  rightMost=rightMost.right;
    			}
    		current.value=rightMost.value;
    		if(rightMost_father.right==rightMost)
    		rightMost_father.right=rightMost.left;
    		else
    			rightMost_father.left=rightMost.left;
    		
    				
    		
    	}
    		
   size--; 	
    	
		return true;
    	
    }

}
3 测试函数

package offerExam;

import java.util.ArrayList;

public class TestTree {
	//测试函数一定要写main 函数呀  !!!
	public static void main(String[] args){
		MyTree tree=new MyTree();
		tree.insert(60);
		tree.insert(55);
		tree.insert(100);
		tree.insert(45);
		tree.insert(57);
		tree.insert(67);
		tree.insert(107);
		tree.insert(59);
		tree.insert(101);
		//测试遍历函数
		System.out.println("测试遍历1:");
		System.out.println(" inorder:");
		tree.inorder(tree.root);
		System.out.println("\n preOrder:");
		tree.preorder(tree.root);
		System.out.println("\n postorder:");
		tree.postorder(tree.root);
		System.out.println();
		//测试查找函数
		int num=56;
		System.out.println(num+"  is in the tree? "+(tree.search(num)?true:false));
		//测试路径函数(path)
		int node_element=107;
		ArrayList arrayList=tree.path(node_element);
		System.out.println("从根节点到 数值 "+node_element+" 路径是:");
		for(int i=0;arrayList!=null&&i


运行结果

测试遍历1:
 inorder:
45 55 57 59 60 67 100 101 107 
 preOrder:
60 55 45 57 59 100 67 107 101 
 postorder:
45 59 57 55 67 101 107 100 60 
56  is in the tree? false
从根节点到 数值 107 路径是:
60 100 
测试删除树种节点100,并将结果用中序遍历
45 55 57 59 60 67 101 107 
 


测试遍历2:
 inorder:
45 55 57 59 60 67 100 101 107 
 preOrder:
60 55 45 57 59 100 67 107 101 
 postorder:
45 59 57 55 67 101 107 100 60 

你可能感兴趣的:(java程序设计代码)