java的树结构(二叉树)

import java.util.*;
public class Main{
    ChainBin bt;
    public Main(){}
    public Main(ChainBin bt) {
        this.bt=bt;
    }
    public static void main(String[] args) {
        ChainBin root = null;
        char select;
        do{
            System.out.println("1.设置二叉树的根元素,\n2.添加二叉树节点\n3.先序遍历\n4.中序遍历\n5.后序遍历\n6按层遍历\n7二叉树深度\0退出");
            Scanner s = new Scanner(System.in);
            select = s.next().charAt(0);
            System.out.println(select+"-->");
            switch (select) {
            case '1':
                root = Init();
                System.out.println(root.Data.data);
                break;
            case '2':
                System.out.println("添加二叉树节点");
                new ChainTree().BinAdd(root);
                break;
            case '3':
                System.out.println("先序遍历!");
                new ChainTree().BinTree_BAA(root, new Meth(root));
                System.out.println();
                break;
            case '4':
                System.out.println("中序遍历!");
                new ChainTree().BinTree_ABA(root, new Meth(root));
                System.out.println();
                break;
            case '5':
                System.out.println("后序遍历!");
                new ChainTree().BinTree_AAB(root, new Meth(root));
                System.out.println();
                break;
            case '6':
                System.out.println("按层遍历!");
                new ChainTree().BinTree_level(root, new Meth(root));
                System.out.println();
                break;
            case '7':
                System.out.println("二叉树深度!");
                System.out.println("深度为"+new ChainTree().BinTreeDepth(root));
                System.out.println();
                break;
            case '0':
                break;
            default:
                break;
            }
        }while(select!='0');
        new ChainTree().BinTreeClear(root);
        root=null;
    }
    static ChainBin Init(){
        ChainBin node= new ChainBin();
        System.out.println("please input the node.");
        Scanner s = new Scanner(System.in);
        int a=s.nextInt();
        node.Data=new DATA(a);
        node.left = null;
        node.right = null;
        return node;
    }
}
interface Method{
    void fun();
    void fun(ChainBin bt);
}
class Meth implements Method{
    ChainBin root =null;
    public Meth(ChainBin root) {
        this.root = root;
    }
    public void fun(ChainBin bt){
        System.out.println(bt.Data.data);
    }
    public void fun() {}
}
class DATA{
    public int data;
    public DATA(int a) {
        this.data = a;
    }
}
class ChainBin{
    DATA Data;
    ChainBin left;
    ChainBin right;
}
class ChainTree{
    ChainBin node=null;
    public ChainTree(){}
    public ChainTree(ChainBin node) {
        this.node = node;
    }
    public ChainBin getNode() {
        return node;
    }
    //input the ChainBin to the ChainTree
    boolean BinAdd(ChainBin bt , ChainBin node ,int n){//add the Bin to ChainTree
        //bt is the father Bin , node is the child ,n 1 is left other 2 right
        if(bt==null){
            return false;
        }
        System.out.println("input data");
        Scanner s = new Scanner (System.in);
        node.Data = new DATA(s.nextInt());
        switch(n){
        case 1:{
                if (bt.left!=null){//left isn't exist
                    return false;
                }else{
                    bt.left=node;
                    System.out.println("left ok");
                }
            }break;
        case 2:{
                if (bt.right!=null){//right isn't exist
                    return false;
                }else{
                    bt.right=node;
                    System.out.println("left ok");
                }
            }break;
        default :
            return false;
        }
        return true;
    }
    void BinAdd(ChainBin bt ){//add the Bin to ChainTree
        ChainBin node=null ,parent=null;
        DATA data=null;
        char select;
        node=new ChainBin();
        System.out.println("input root data ..");
        Scanner s = new Scanner(System.in);
        data = new DATA(s.nextInt());
        parent = BinTreeFind(bt, data);
        if(parent==null){
            System.out.println("find not..");
            return ;
        }
        System.out.println("1.to add Left Tree\n2.to add Left Tree");
        do {
            select =s.next().charAt(0);
            if (select=='1' || select=='2') {
                BinAdd(parent, node, (int)(select-'0'));
                System.out.println("ok");
            }
        } while (select!='1' && select!='2');
        System.out.println("over");
    }
    //get the left right TreeBin
    ChainBin BinTreeLeft(ChainBin bt){
        if(bt!=null){
            return bt.left;
        }else{
            return null;
        }
    }
    ChainBin BinTreeRight(ChainBin bt){
        if(bt!=null){
            return bt.right;
        }else{
            return null;
        }
    }
    //cheek the Tree is empty;
    boolean BinTreeIsEmpty (ChainBin bt){
        if(bt ==null){
            return true;
        }else{
            return false;
        }
    }
    int BinTreeDepth(ChainBin bt){
        int dep1 , dep2 ;
        if(bt==null){
            return 0;
        }else{
            dep1 = BinTreeDepth(bt.left);
            dep2 = BinTreeDepth(bt.right);
            if(dep1 > dep2){
                return dep1 +1;
            }else{
                return dep2 +1;
            }
        }
    }
    //check from TreeBin
    ChainBin BinTreeFind(ChainBin bt,DATA data){
        ChainBin p=null;
        if ( bt==null){
            System.out.println("null");
            return bt;
        }else{    //recursion
            if(bt.Data.data == data.data){
                System.out.println(bt.Data.data+"has"+data.data);
                return bt;
            }else{
                if((p=BinTreeFind(bt.left,data))!=null){
                    System.out.println("--->>");
                    return p;
                }else if ((p=BinTreeFind(bt.right,data))!=null){
                    System.out.println("--->>");
                    return p;
                }else{
                    return null;
                }
            }
        }
    }
    //clear the TreeBin
    void BinTreeClear(ChainBin bt){
        if(bt!=null){
            BinTreeClear(bt.left);
            BinTreeClear(bt.right);
        }
        bt=null;
    }
    //send out Tree
    void BinTree_BAA(ChainBin bt,Method oper){//前序递归
        if(bt!=null){
            oper.fun(bt);
            BinTree_BAA(bt.left,oper); //recursion
            BinTree_BAA(bt.right,oper); //recursion
        }
        return;
    }
    void BinTree_ABA(ChainBin bt,Method oper){//中序递归
        if(bt!=null){
            oper.fun(bt);
            BinTree_ABA(bt.left,oper); //recursion
            BinTree_ABA(bt.right,oper); //recursion
        }
        return;
    }
    void BinTree_AAB(ChainBin bt,Method oper){//后序递归
        if(bt!=null){
            oper.fun(bt);
            BinTree_AAB(bt.left,oper); //recursion
            BinTree_AAB(bt.right,oper); //recursion
        }
        return;
    }
    void BinTree_level(ChainBin bt ,Method oper){
        ChainBin p=null;
        ChainBin q[] = new ChainBin[1000];
        int head=0,tail=0;
        if (bt!=null) {
            tail = (tail+1)%1000;
            q[tail] = bt;
        }
        while (head!=tail) {
            head = (head+1)%1000;
            p = q[head];
            oper.fun(p);
            if (p.left!=null) {
                tail = (tail+1)%1000;
                q[tail] = p.left;
            }
            if (p.right!=null) {
                tail = (tail+1)%1000;
                q[tail] = p.right;
            }
        }
        return ;
    }
}

猜你喜欢

转载自blog.csdn.net/u012651389/article/details/44886693