java数据结构二叉树的遍历和二叉查找树

《java数据结构二叉树的遍历和二叉查找树》

**一、实验目的
1、掌握二叉树的特点及其存储方式;
2、掌握二叉树的创建;
3、掌握二叉树前序、中序、后序遍历的基本方法及应用;
4、掌握二叉查找树的特点;
5、掌握二叉查找树查找(包含contain)、插入和删除操作的实现。
二、实验内容
1、用前序方法建立一棵二叉树;
2、实现前序、中序和后序遍历二叉树的操作;
3、实现统计二叉树叶子结点个数或计算二叉树深度的操作;
4、将输入的一组数据逐个插入实现创建二叉查找树;
5、用非递归实现二叉查找树的查找和删除操作。
三、实验环境
eclipse环境
四、实验步骤
1、二叉链表节点类的定义;
2、二叉树类的定义;
3、建立下图所示的二叉树

以字符串的形式“根左右”定义一棵二叉树时,写出创建二叉树的操作:
4、编程实现以上二叉树的前序、中序和后序遍历操作,输出遍历序列;
5、完成统计以上二叉树中叶子结点的个数或计算以上二叉树的深度;
6、定义二叉查找树类;实现二叉查找树的查找、插入和删除操作;
7、从键盘上输入六个整数45、24、53、12、37、9构造二叉查找树,输出二叉查找树的中序遍历结果;
8、在二叉查找树上查找37和50,并输出能否查找成功;
9、删除数据元素24和53,输出其中序遍历结果。
五、问题讨论
1、先序、中序、后序遍历二叉树的区别?
2、在先序、中序非递归算法中为什么使用栈?能不能借助其它数据结构来实现?
3、二叉查找树中序遍历结果有什么特点?
4、在二叉查找树中插入一个新结点,总是插入到叶结点下面吗?
5、在任意一棵非空二叉查找树中,删除某结点后又将其插入,则所得二叉查找树与原二叉查找树相同吗?
六、实验报告内容
1、实验目的
2、实验内容和具体要求
3、完成情况和实验记录,实验记录为实验过程中遇到的问题及解决方法
4、程序清单
5、所输入的数据及相应的运行结果
6、问题讨论回答
7、实验心得**

源码:
package 实验二;

//二叉树的创建、前序、中序、后序遍历、统计叶子节点以及计算二叉树的深度

public class Test2 <AnyType extends Comparable<?super AnyType>>{
    BTNode<AnyType> rootNode=new BTNode<AnyType>();
    int i=0;
    int count=0;
    static BTNode root;

    //二叉树节点类的初始化
    public static class BTNode<AnyType>{
        public BTNode<AnyType> leftchild;
        public BTNode<AnyType> rightchild;
        public AnyType data;
        BTNode(){
            this.data=null;
            this.leftchild=rightchild=null;
        }
        BTNode(AnyType thedata){
            this.data=thedata;
            this.leftchild=rightchild=null;
        }
       public BTNode<AnyType> getleftChild( ){
            return leftchild;
       }             
       public BTNode<AnyType> getrightChild( ){
            return rightchild;
       }
       public Object getdata( ){
            return data;
       }
    }
    //二叉树的创建
    public BTNode<AnyType>CreateBinaryTree(AnyType[] st){
        BTNode<AnyType> rootNode=null;
        if(i<st.length){
            if (st[i]=="*"){
                rootNode = null;
                i++;
            }
            else{
                AnyType data=st[i];
                i++;
                rootNode=new BTNode<AnyType>(data);
                rootNode.leftchild=CreateBinaryTree(st);
                rootNode.rightchild=CreateBinaryTree(st);
            }
         }
         return rootNode;   
     }
        //二叉树的前序遍历void的直接输出
     public  void FontOrder(BTNode<AnyType>  t){  
         if(t!=null){   
                System.out.print(t.data+" ");  
                FontOrder(t.leftchild);   
                FontOrder(t.rightchild);  
         }
     }
        //二叉树的中序遍历void类型直接输出
     public  void MiddleOrder(BTNode<AnyType>  t){ 
         if(t!=null) {  
                MiddleOrder(t.leftchild);   
                System.out.print(t.data+" ");  
                MiddleOrder(t.rightchild);  
         }

     }
        //二叉树的后序遍历void类型直接输出
     public  void Behind( BTNode<AnyType> t){  
         if(t!=null) {  
             Behind(t.leftchild);   
             Behind(t.rightchild);  
                System.out.print(t.data+" ");  
         }
     }
        //二叉树得到二叉树的叶子节点数返回int类型
     public int Getleaves(BTNode<AnyType> rootNode ){  
         int left,right;
         if(rootNode==null) return 0;
               left = Getleaves(rootNode.leftchild);   
               right = Getleaves(rootNode.rightchild); 
               if(left<right){
                   return right+1;
               }else 
                   return left+1;
     }
        //二叉树计算深度返回int类型
     public  int Getdepth(BTNode<AnyType>  rootNode ){ 
         if(rootNode!=null) {
             count++;
             Getdepth(rootNode.leftchild);   
             Getdepth(rootNode.rightchild);  
         }

         return count/2;
     }
     //Test2的main方法人口实现实验要求的二叉树
     public static void main(String[] args) {
         String[] st={"a","b","d","*","c","e","*","f"};
         Test2 answer=new Test2();
         //实验第一问实现二叉树
         root=answer.CreateBinaryTree(st);
         System.out.println("二叉树的前序遍历:");
         answer.FontOrder(root);
         System.out.println( "\n二叉树的中序遍历:");
         answer.MiddleOrder(root);
         System.out.println("\n二叉树的后序遍历:");
         answer.Behind(root);
         System.out.println("\n二叉树的深度:");
         System.out.println(answer.Getdepth(root));
         System.out.println("二叉树的叶子节点数:");
         System.out.println(answer.Getleaves(root));   

    }
}
                /*二叉树的前序遍历: a b d c e f 二叉树的中序遍历: d e f c b a 二叉树的后序遍历: f e c d b a 二叉树的叶子节点数: 6*/
package 实验二;
    //二叉查找树的节点类
  class BinaryNode<AnyType>{
            AnyType element;
            BinaryNode<AnyType> left;
            BinaryNode<AnyType> right;
            BinaryNode(AnyType theelement){
                element=theelement;
                left=right=null;
            }
            BinaryNode(AnyType theelement,BinaryNode<AnyType> lt,BinaryNode<AnyType>rt){
                element=theelement;
                left=lt;
                right=rt;

            }
        }
  //二叉查找树数值的插入,查找以及删除操作
public class BinaryTree<AnyType extends Comparable<? super AnyType>> {
    private BinaryNode<AnyType> root;

    public boolean isEmpty(){
        return root==null;
    }
    //判断到底查找到不到,若存在数值那么返回true并告诉查找到。
    public boolean contains(AnyType x){
        return contains(x,root);
    }
    private boolean contains(AnyType x,BinaryNode<AnyType>t){
        if(t==null){
            System.out.println("亲,你要的数据不存在哦!");
            return false;}
        int result=x.compareTo(t.element);
        if(result<0)
            return contains(x,t.left);
        else if(result>0)
            return contains(x,t.right);
        else{
            System.out.println("找到了!");
        return true;
        }
    }
    public AnyType findMin() throws Exception{
        if(isEmpty())throw new Exception("错误提示:当前二叉查找树为空!");
        return findMin(root).element;
    }
    //查找到二叉查找树中最小数,然后删除的时候用的到
    private BinaryNode<AnyType>findMin(BinaryNode<AnyType> t){
        if(t==null)
            return null;
        else if(t.left==null)
            return t;
        return findMin(t.left);
    }

    public void insert(AnyType x){
        root=insert(x,root);
    }
    //按照要求逐个的插入到二叉查找树中
    private BinaryNode<AnyType>insert(AnyType x,BinaryNode<AnyType>t){
        if(t==null)
            return new BinaryNode<AnyType>(x,null,null);
        int compareresult=x.compareTo(t.element);
        if(compareresult<0)
            t.left=insert(x,t.left);
        else if(compareresult>0)
            t.right=insert(x,t.right);
        else 
            ;
        return t;
    }
    //按照要求实现插入后、删除后的中序遍历操作
    public void MiddleOrder(){
        if(root!=null)
            MiddleOrder(root);
    }
    public void MiddleOrder(BinaryNode <AnyType> t){
        if(t!=null){
            MiddleOrder(t.left);
            System.out.print(" "+ t.element);
            MiddleOrder(t.right);
        }
    }
    //按照要求实现二叉查找树的删除
    public void remove(AnyType x){
        if(root!=null)
            remove(x,root);
    }

    public BinaryNode<AnyType>remove(AnyType x,BinaryNode<AnyType>t){
        if(t==null)
            return t;
        int comresult=x.compareTo(t.element);
        if(comresult<0)
            t.left=remove(x,t.left);
        else if(comresult>0)
            t.right=remove(x,t.right);
        else if(t.left!=null&&t.right!=null){
            t.element=findMin(t.right).element;
            t.right=remove(t.element,t.right);
        }
        else
            t=(t.left!=null)?t.left:t.right;

        return t;
    }

    public static void main(String [] args){
        BinaryTree r=new BinaryTree();
        BinaryNode tree = null;
        Integer [] str={45,24,53,12,37,9};
        for(int i=0;i<str.length;i++){
            r.insert(str[i]);
        }
        System.out.print("中序遍历为:");
        r.MiddleOrder();
        System.out.println("\n查找数值37的查找结果:");
        r.contains(37);
        System.out.println("查找数字50的查找结果:");
        r.contains(50);
        System.out.println("删除24的操作的中序遍历为:");
        r.remove(24);
        r.MiddleOrder();
        System.out.println("\n删除53的操作的中序遍历为:");
        r.remove(53);
        r.MiddleOrder();
    }
    /* 中序遍历为: 9 12 24 37 45 53 查找数值37的查找结果: 找到了! 查找数字50的查找结果: 亲,你要的数据不存在哦! 删除24的操作的中序遍历为: 9 12 37 45 53 删除53的操作的中序遍历为: 9 12 37 45 */

}
package 实验二;




public class BITreeNode <AnyType> {
            AnyType data;
            BITreeNode<AnyType> leftchild,rightchild;
            BITreeNode(){
                this.data=null;
                this.leftchild=rightchild=null;
            }
            BITreeNode(AnyType thedata){
                this.data=thedata;
                this.leftchild=rightchild=null;
            }

        /* BITreeNode(AnyType thedata,BITreeNode<AnyType> lt,BITreeNode<AnyType> rt){ data=thedata; leftchild=lt; rightchild=rt; } BITreeNode(AnyType data){ this(data,null,null); }*/

            public BITreeNode<AnyType> getleftchild(){
                return leftchild;
            }
            public BITreeNode<AnyType>getrightchild(){
                return rightchild;
            }
            public Object getdata(){
                return data;
            }



    public static class BINaryTree<AnyType  extends Comparable<? super AnyType>>{
        BITreeNode<AnyType>rootNode;
        int count=0;
    public BINaryTree(){
        rootNode=null;
    }
    public BINaryTree(AnyType rootNodeItem){
        rootNode.data=rootNodeItem;
        rootNode.leftchild=rootNode.rightchild=null;

    }   
    public BINaryTree(BITreeNode<AnyType> t){
        rootNode=t;
    }
    public boolean isEmpty(){
        System.out.println("目前二叉查找树数据为空!");
        return rootNode==null;
    }
    public void makeEmpty(){
        rootNode=null;
    }
    public BITreeNode<AnyType> getrootNode(){
        return rootNode;
    }
    public boolean contains(AnyType x){
        return contains(x);
    }
    //找到最小数
    public AnyType findMin(BITreeNode<AnyType> rootNode)throws Exception{
        if(isEmpty());
        if(rootNode.leftchild==null)
            return  (AnyType) rootNode;
        return findMin(rootNode.leftchild);

    }
    //根左右
   public void FontOrder(BITreeNode<AnyType> rootNode){
        if(rootNode!=null){
            System.out.println(" "+rootNode.data);
            FontOrder(rootNode.leftchild);
            FontOrder(rootNode.rightchild);
        }
    }

    //左右根
    public void MiddleOrder(BITreeNode<AnyType> rootNode){
        if(rootNode!=null){
            MiddleOrder(rootNode.leftchild);
            System.out.println(" "+rootNode.data);
            MiddleOrder(rootNode.rightchild);
        }
    }

    //左右根
    public void BehindOrder(BITreeNode<AnyType> rootNode){
        if(rootNode!=null){
            BehindOrder(rootNode.leftchild);
            BehindOrder(rootNode.rightchild);
            System.out.println(" "+ rootNode.data);
        }
    }

    //统计节点个数
    public int countleaves(BITreeNode<AnyType> rootNode){
        if(rootNode!=null){
            countleaves(rootNode.leftchild);
            countleaves(rootNode.rightchild);
        }else{
            count++;
        }
        return count/2;
    }

    //统计二叉树 的深度
    public int getdepth(BITreeNode <AnyType> rootNode){
        int left,right;
        if(rootNode==null) return 0;
         left=getdepth(rootNode.leftchild);
         right=getdepth(rootNode.rightchild);
         if(left<right){
             return right+1;
         }else{
             return left+1;
         }
    }

    //用中序构造二叉查找树

    public BITreeNode<AnyType> CreateBINaryTree(int[] st){
        BITreeNode<AnyType> rootNode=null;
        int i=0;
        if(i<st.length){
            if(st[i]==-1){
                rootNode=null;
                i++;
            }else{
                int data=st[i];
                i++;
             rootNode   =new BITreeNode<AnyType>(null);
            rootNode.leftchild=CreateBINaryTree(st);
            rootNode.rightchild=CreateBINaryTree(st);
            }
        }
        return rootNode;
    }


    //查找二叉树中节点
    public boolean Find(BITreeNode<AnyType> t,AnyType x){
        if(t==null){
            System.out.println("没有办法来查找!");
        }else{
            while(t!=null){
                int result=x.compareTo(t.data);
                if(result==0){
                    System.out.println("查找到元素,在二叉树中!");
                    return true;
                }else if(result>0){
                    t=t.rightchild;
                }else if(result<0){
                    t=t.leftchild;
                }
            }
        }
        return false;
    }

    }



        public static void main(String [] args ){
            int [] st={45,24,53,12,37,9};
             BINaryTree answer=new BINaryTree();
             //实验第一问实现二叉树
             answer.CreateBINaryTree(st);
             System.out.println("二叉树的前序遍历:");
             answer.FontOrder(  answer.CreateBINaryTree(st));
             System.out.println( "\n二叉树的中序遍历:");
             answer.MiddleOrder(answer.CreateBINaryTree(st));
             System.out.println("\n二叉树的后序遍历:");
             answer.BehindOrder(answer.CreateBINaryTree(st));
             System.out.println("\n二叉树的深度:");
             System.out.println(answer.getdepth(answer.CreateBINaryTree(st)));
             System.out.println("二叉树的叶子节点数:");
             System.out.println(answer.countleaves(answer.CreateBINaryTree(st)));   

        }

}


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