0

How to print out a binary search tree in java? I have written the code to insert to the tree but without being able to print the tree i am insure if the elements are adding. I will post my code below.

public class TreeNode {

    TreeNode left;
    TreeNode right;
    TreeNode root;

    int data;

    public TreeNode(int d) {

        data = d;
        left = right = null;
        root = null;

    }

    public synchronized void insert(int d) {
        if (root == null){
            root = new TreeNode( d );
        }
        if (d < data) {
            if (left == null) {
                left = new TreeNode(d);
            } else {
                left.insert(d);
            }
        } else if (d > data) {
            if (right == null) {
                right = new TreeNode(d);
            } else {
                right.insert(d);
            }
        }
    }

    public TreeNode treeSearch(TreeNode root, int target) {
        if (root != null) {
            if (target < root.data) {
                root = treeSearch(root.left, target);
            } else if (target > root.data) {
                root = treeSearch(root.right, target);
            }
        }
        return root;
    }
}
5
  • 2
    there are at least too many ways of printing it. Can you provide a simple input and desired output? Have you tried anything yourself? Commented Nov 27, 2017 at 11:38
  • Its hard to explain the way i would like it but i will try. The output should output the root first then the lower level with the outputs branching off the root and then a lower level branching off this etc depending on the values inserted. Commented Nov 27, 2017 at 11:44
  • I think you missed the "Can you provide a simple input and desired output?" part while reading my comment. This meant editing the question, so everybody can easily see that, without looking at the comments Commented Nov 27, 2017 at 11:55
  • @Bryan I think you mean the Preorder traversal. For more info : geeksforgeeks.org/… Commented Nov 27, 2017 at 11:57
  • Thank you will check it out now! Commented Nov 27, 2017 at 12:08

5 Answers 5

4

You may use following method:

 void printTree(TreeNode node, String prefix)
 {
    if(node == null) return;

    System.out.println(prefix + " + " + node.data);
    printTree(node.left , prefix + " ");
    printTree(node.right , prefix + " ");
 }

Initial call should be printTree( root,""); from where you want to print the tree. Here root is reference of root node.

UPDATED:
You can see this code working here

Sign up to request clarification or add additional context in comments.

12 Comments

Thank you for your help, can you please explain this method in more detail.
@Bryan This method use recursion-method to print the tree. First parameter is the node which is going to be printed and second argument is a String having spaces for better visibility of tree. This method returns if node is null. if it is not null then it prints data part and call itself for left and right child.
Thank you for further explanation!
your method has not solved it exactly but it has given me a basis to try modify and tweak it to what i want exactly!
@Bryan What is not covered in this answer?
|
0

you can print the tree data like this.

public void printTree(TreeNode root) {
        if (root == null) return;
        System.out.println(root.data);
        printTree(root.left);
        printTree(root.right);
    }   

Comments

0
private void print(PrintWriter out) {
    print(root, out, "");
}

private void print(TreeNode subtree, PrintWriter out, String indent) {
    if (subtree != null) {
        print(subtree.left, out, indent + "--");
        out.printf("%s %s%n", indent, subtree.data);
        print(subtree.right, out, indent + "--");
    }
}

Comments

0

Infix order: Left - root - right

public String printInfix(TreeNode root) {//prints all the words in the tree in infix order
    if(root==null) {
        return "";
    }

     return printAll(root.left+" "+root.data+" "+printAll(root.right);
}

Comments

0
public static void printTree(TreeNode node, String prefix)
    {
        if(node == null) return;

        printTree(node.right , prefix + "   ");
        System.out.println(prefix + node.data);
        printTree(node.left , prefix + "   ");
       
    } 

Prints the tree in BST structure flatly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.