如何实现二叉树

时间:2015-03-11 03:35:15

标签: java tree binary-tree

     package tree;

     public class Tree {

     TreeNode treenode=new TreeNode();
     TreeNode root=null;

     void insert(int data){
         root=insert(root,data);

     }

     TreeNode insert(TreeNode node,int data)
     {
         if(node==null)
         {
             node=new TreeNode(data); 
             //node.data=data;

         }
         else
         {

            // while(node!=null){
            if(node.lchild==null){
                node.lchild=insert(node.lchild,data);
                //node.lchild=node;
            }
            else if(node.rchild==null){

                node.rchild=insert(node.rchild,data);
                //node.lchild=node;
            }

    //   }

         }
        return node;



     }
     void printTree(TreeNode treenode)
     {
         if(treenode==null){
             return;
         }
         else
         {
            printTree(treenode.lchild);
            System.out.print("->"+treenode.data);
            printTree(treenode.rchild);

         }
     }

    public void printTree() {
        // TODO Auto-generated method stub
        printTree(root);

    }

     void printTreePostOrder(TreeNode treenode)
     {
         System.out.println("Post Order");
         if(treenode==null){
             return;
         }
         else
         {
            printTree(treenode.lchild);
            printTree(treenode.rchild);
            System.out.print("->"+treenode.data);

         }
     }

    public void printTreePostOrder() {
        // TODO Auto-generated method stub
        printTreePostOrder(root);

    }



}

----------------2nd Class--------------------


package tree;

public class TreeNode {


    enter code here
    int data;
    TreeNode lchild;
    TreeNode rchild;



public TreeNode(int data) {
        // TODO Auto-generated constructor stub
    this.data=data;
    this.rchild=null;
    this.lchild=null;
    }



public TreeNode() {
    // TODO Auto-generated constructor stub
    this.data=0;;
    this.lchild=null;
    this.rchild=null;
}



int getData()
{
    return this.data;
}



public TreeNode getLchild() {
    return lchild;
}



public void setLchild(TreeNode lchild) {
    this.lchild = lchild;
}



public TreeNode getRchild() {
    return rchild;
}



public void setRchild(TreeNode rchild) {
    this.rchild = rchild;
}



public void setData(int data) {
    this.data = data;
}


public static void main(String args[]){
    Tree tree=new Tree();
    tree.insert(4);
    tree.insert(5);
    tree.insert(2);
    tree.insert(7);
    tree.insert(1);
    tree.insert(6);
    tree.printTree();
    tree.printTreePostOrder();

}

}

在此,我无法正确添加3个以上的节点。 。我正在研究一个简单的有序二叉树而不是二叉搜索树。任何人都可以告诉我们应该怎么做。我正在尝试实现二叉树而不是二叉搜索树

2 个答案:

答案 0 :(得分:1)

问题出在insert(TreeNode node,int data)方法的这一部分:

        if(node.lchild==null){
            node.lchild=insert(node.lchild,data);
            //node.lchild=node;
        }
        else if(node.rchild==null){

            node.rchild=insert(node.rchild,data);
            //node.lchild=node;
        }

lchildrchild都不为空时,您没有处理此案例。如果发生这种情况,您可以通过调用insert(node.lchild,data)insert(node.rchild,data)来对这两个孩子中的一个进行递归调用。

        if(node.lchild==null){
            node.lchild=insert(node.lchild,data);
            //node.lchild=node;
        }
        else if(node.rchild==null){

            node.rchild=insert(node.rchild,data);
            //node.lchild=node;
        }else{
            double random = Math.random();
            if(random < 0.5)
               insert(node.lchild, data);
            else
               insert(node.rchild, data);
        }

答案 1 :(得分:0)

下面是我在java中使用按顺序遍历

实现二叉树的解决方案
class BinaryTreeNode<T>{
    private T data;
    private BinaryTreeNode left;
    private BinaryTreeNode right;

    BinaryTreeNode(T data){
        this.data = data;
    }

    BinaryTreeNode addLeft(T data){
        left = new BinaryTreeNode(data);
        return left;
    }

    BinaryTreeNode addRight(T data){
        right = new BinaryTreeNode(data);
        return right;
    }

    void inOrderTraversal(BinaryTreeNode node){
        if(node != null){
            inOrderTraversal(node.left);
            System.out.print(" " +node.data);
            inOrderTraversal(node.right);
        }
        return;
    }

}

假设我的树结构如下所示。 Binary Tree 然后上面树结构的客户端类就像。

public class Client {
    public static void main(String[] args) {
        BinaryTreeNode root = new BinaryTreeNode(1);
        BinaryTreeNode left = root.addLeft(2);
        BinaryTreeNode right = root.addRight(3);

        left.addLeft(4);
        left = left.addRight(5);
        left.addLeft(7);
        left.addRight(8);

        right = right.addRight(6);
        right = right.addRight(9);
        right.addLeft(10);
        right.addRight(11);

        root.inOrderTraversal(root);

   }
}

输出 4 2 7 五 8 1 3 6 10 9 11