二叉树

来源:互联网 发布:电力猫网络dns不正常 编辑:程序博客网 时间:2024/06/02 03:58
package shujujiegou;public class BinaryTree {    private Node root=null;    public BinaryTree(){        root=new Node(1,"A");    }    //创建一个二叉树    public void createTree(Node root){        Node nb=new Node(2,"B");        Node nc=new Node(3,"C");        Node nd=new Node(4,"D");        Node ne=new Node(5,"E");        root.setLeft(nb);        root.setRight(nc);        nb.setLeft(nd);        nb.setRight(ne);    }    public boolean isEmpty(Node root){        return root==null;    }    public int height(){        return height(root);    }    private int height(Node subTree){        if(subTree==null){            return 0;//递归结束,空树高度为0        }else{            int i=height(subTree.getLeft());            System.out.println("i:"+i);            int j=height(subTree.getRight());            System.out.println("j:"+j);            return (i<j)?(j+1):(i+1);        }    }    //结点个数    public int size(){        return size(root);    }    private int size(Node subTree){        if(subTree==null){            return 0;//递归结束,空树为0        }else{            return 1+size(subTree.getLeft())+size(subTree.getRight());        }    }    //返回双亲节点    public Node parent(Node element){        return (root==null||root==element)?null:parent(root,element);    }    public Node parent(Node subTree,Node element){        if(subTree==null)            return null;        if(subTree.getLeft()==element||subTree.getRight()==element)            //返回父节点            return subTree;        Node p;        //现在在左子树中找,如果左子树中没有找到,才到右子树中找        if((p=parent(subTree.getLeft(),element))!=null)            //递归在左子树中搜索            return p;        else            return parent(subTree.getRight(),element);    }    //删除根为subTree的子树    public void destroy(Node node){        if(node==null){            //删除左子树            destroy(node.getLeft());            //删除右子树            destroy(node.getRight());            //删除根结点            node=null;        }    }    //当前节点    public void visited(Node node){        node.setVisited(true);        System.out.println("key:"+node.getKey()+",value:"+node.getData());    }    //前序遍历    public void preOrder(Node node){        if(node!=null){            visited(node);            preOrder(node.getLeft());            preOrder(node.getRight());        }    }    //中序遍历    public void inOrder(Node node){        if(node!=null){            inOrder(node.getLeft());            visited(node);            inOrder(node.getRight());        }    }    //后序遍历    public void postOrder(Node node){        if(node!=null){            postOrder(node.getRight());            visited(node);            postOrder(node.getLeft());        }    }    public void traver(Node node){        if(node!=null){            System.out.println("索引:"+node.getKey()+",数据:"+node.getData());            traver(node.getLeft());            traver(node.getRight());        }    }    public static void main(String[] args) {        BinaryTree bt=new BinaryTree();        /*bt.createTree(bt.root);        System.out.println(bt.height());*/        Node n=new Node(1,"B");        System.out.println(bt.parent(n));    }}/** * 二叉树的节点数据结构 */class Node{    private int key=0;    private String data=null;    private boolean isVisited=false;    private Node left=null;    private Node right=null;    public Node() {        super();    }    public Node(int key, String data) {        super();        this.key = key;        this.data = data;        this.left = null;        this.right = null;    }    /**     * @return the key     */    public int getKey() {        return key;    }    /**     * @param key the key to set     */    public void setKey(int key) {        this.key = key;    }    /**     * @return the data     */    public String getData() {        return data;    }    /**     * @param data the data to set     */    public void setData(String data) {        this.data = data;    }    /**     * @return the isVisited     */    public boolean isVisited() {        return isVisited;    }    /**     * @param isVisited the isVisited to set     */    public void setVisited(boolean isVisited) {        this.isVisited = isVisited;    }    /**     * @return the left     */    public Node getLeft() {        return left;    }    /**     * @param left the left to set     */    public void setLeft(Node left) {        this.left = left;    }    /**     * @return the right     */    public Node getRight() {        return right;    }    /**     * @param right the right to set     */    public void setRight(Node right) {        this.right = right;    }}