二进制搜索树打印路径

时间:2014-07-29 18:25:13

标签: java binary-search-tree

尝试创建打印TreeNode方法的路径时,我有点困惑。不太确定我哪里出错但我认为它可能在第二else

代码:

public static ArrayList<Integer> printPath(TreeNode node, ArrayList<Integer> path, int value) {
        if (node == null) {
            return path;
        } else {
            if (node.data == value) {
                path.add(value);
                return path;
            } else {
                path.add(node.data);
                printPath(node.left, path, value);
                printPath(node.right, path, value);
            }
        }
        return path;
    }

enter image description here

目前我的输出为[20, 8, 4, 12, 22]当我应该只获得[20,8,12]时。

我在图片中添加了二进制搜索树,路径为空ArrayList,值为12

3 个答案:

答案 0 :(得分:4)

假设您只想要从root-Node到给定value的最短路径,则必须将该值与当前节点的数据进行比较,然后决定是向左还是向右(而不是两个方向)。

public static ArrayList<Integer> printPath(TreeNode node, ArrayList<Integer> path, int value) {
    if (node == null)
        return path;

    path.add(node.data);

    int cmp = Integer.compare(value, node.data);

    if (cmp < 0) // value is smaller, so go left
        printPath(node.left, path, value);
    else if (cmp > 0) // value is larger, so go right
        printPath(node.right, path, value);
    else /* if (cmp == 0) */
         return path; // value found

    return path;
}

在调用时,这应该为建议的树提供[20, 8, 12]

printPath(root, new ArrayList<Integer>(), 12);

答案 1 :(得分:1)

public static ArrayList<Integer> printPath(TreeNode node, ArrayList<Integer> path, int value) {
        if (node == null) {
            return path;
        } 
        path.add(node.data);
        if (node.data < value) {
            printPath(node.right, path, value);
        } else if(node.data>value){
            printPath(node.left, path, value);
        } 
        return path;
    }

答案 2 :(得分:1)

包含一些样本数据的可测试解决方案:

import java.util.ArrayList;
import java.util.List;



class TreeNode 
{
     int data;
     TreeNode left;
     TreeNode right;

     public TreeNode(int x) { data = x; }


     public TreeNode(TreeNode node)
     {
         this.data = node.data;
         this.left = node.left;
         this.right = node.right; 

     }

     public void add(TreeNode node)
     {

        if(data > node.data)
        {

            if(left == null)
                left = node;

            else
                left.add(node);
        }

        if(data < node.data)
            if(right == null)
                right = node;

            else
                right.add(node);
     }


}


class Tree
{
    TreeNode root;

    public Tree(TreeNode node)
    {
        this.root = node;

    }
    public Tree()
    {
        this.root = null;
    }

    public void add(TreeNode node)
    {

        if(root == null)
        {
            root = node;
        }
        if(root.data > node.data){

            if(root.left == null)
                root.left = node;
            else
                root.left.add(node);
        }

        if(root.data < node.data)
        {
            if(root.right == null)
                root.right = node;
            else
                root.right.add(node);
        }

    }

    public void addInt(int value){

        add(new TreeNode(value));

    }

     public void postorder(TreeNode n)
     {
      if (n != null)
      {
       postorder(n.left);
       postorder(n.right);
       System.out.print(n.data + " ");
      }
     }

     public void inorder(TreeNode n)
     {
      if (n != null)
      {
       inorder(n.left);
       System.out.print(n.data + " ");
       inorder(n.right);
      }
     }

}


public class TreeTest
{


    public static void main(String[] args)
    {

        Tree tree = new Tree();

        tree.add(new TreeNode(3));
        tree.add(new TreeNode(2));
        tree.add(new TreeNode(5));
        tree.add(new TreeNode(9));
        tree.add(new TreeNode(4));
        tree.add(new TreeNode(1));
        tree.add(new TreeNode(10));
        tree.addInt(11);


        ArrayList<Integer> mylist = printPath(tree.root, new ArrayList<Integer>(),10);

        System.out.println("the path is "+mylist);

        tree.inorder(tree.root);
        System.out.println("");
        tree.postorder(tree.root);


    }

    public static ArrayList<Integer> printPath(TreeNode node, ArrayList<Integer> path, int value) {

        if (node == null) {
            return path;
        } 

        if (node.data == value) {
            path.add(value);
            return path;
        } 

        if(node.data > value){
            path.add(node.data);
            printPath(node.left, path, value);
        }
        if(node.data < value) {
            path.add(node.data);
            printPath(node.right,path, value);    
        }

        return path;
    }

}