简体   繁体   中英

Binary Search Tree print path

I am a bit stuck trying to create a print the path to TreeNode method. Not quite sure where I went wrong but I think it might be in the 2nd else .

code:

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;
    }

在此处输入图片说明

Currently I am getting output as [20, 8, 4, 12, 22] When I should be only getting [20,8,12] .

I added the Binary search tree in the picture, path is an empty ArrayList , and value is 12

Assuming you want only the shortest path from the root-Node to the given value , you must compare the value with the current node's data and then decide whether to go left or right (and not go both directions).

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;
}

This should give [20, 8, 12] for the proposed tree when calling:

printPath(root, new ArrayList<Integer>(), 12);
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;
    }

A testable Solution with some sample data:

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;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM