1
votes

I have implemented a Binary search tree with insert and traversal method but am not getting correct output for PreOrder and Postorder ,am getting inOrder in correct order. Could some one please tell me where am wrong.

I tried the same example on paper but the PreOrder and PostOrder is not same.

Here is my Code

Node Class

package com.BSTTest;

public class Node implements Comparable<Node> {
    private int data;
    private Node leftChild;
    private Node rightChild;

    public Node(int data) {
        this(data, null, null);
    }

    public Node(int data, Node leftChild, Node rightChild) {
        this.data = data;
        this.leftChild = leftChild;
        this.rightChild = rightChild;

    }

    public int getData() {
        return data;
    }

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

    public Node getLeftChild() {
        return leftChild;
    }

    public void setLeftChild(Node leftChild) {
        this.leftChild = leftChild;
    }

    public Node getRightChild() {
        return rightChild;
    }

    public void setRightChild(Node rightChild) {
        this.rightChild = rightChild;
    }

    public int compareTo(Node o) {
        return Integer.compare(this.data, o.getData());
    }
}

Tree Class

package com.BSTTest;

import com.BSTTest.Node;

public class Tree {
    private Node root = null;

    public Node getRoot() {
        return root;
    }
     //Inserting data**strong text**
    public void insertData(int data) {
        Node node = new Node(data, null, null);
        if (root == null) {
            root = node;
        } else {
            insert(node, root);
        }
    }
    //Helper method for insert
    private void insert(Node node, Node currNode) {
        if (node.compareTo(currNode) < 0) {
            if (currNode.getLeftChild() == null) {
                currNode.setLeftChild(node);
            } else {
                insert(node, currNode.getLeftChild());
            }
        } else {

            if (currNode.getRightChild() == null) {
                currNode.setRightChild(node);
            } else {
                insert(node, currNode.getRightChild());
            }
        }
    }

    public void printInorder() {
        printInOrderRec(root);
        System.out.println("");
    }


      //Helper method to recursively print the contents in an inorder way

    private void printInOrderRec(Node currRoot) {
        if (currRoot == null) {
            return;
        }
        printInOrderRec(currRoot.getLeftChild());
        System.out.print(currRoot.getData() + ", ");
        printInOrderRec(currRoot.getRightChild());
    }

    public void printPreorder() {
        printPreOrderRec(root);
        System.out.println("");
    }


     // Helper method for PreOrder Traversal recursively 

    private void printPreOrderRec(Node currRoot) {
        if (currRoot == null) {
            return;
        }
        System.out.print(currRoot.getData() + ", ");
        printPreOrderRec(currRoot.getLeftChild());
        printPreOrderRec(currRoot.getRightChild());
    }

    public void printPostorder() {
        printPostOrderRec(root);
        System.out.println("");
    }

    /**
     * Helper method for PostOrder method to recursively print the content
     */
    private void printPostOrderRec(Node currRoot) {
        if (currRoot == null) {
            return;
        }
        printPostOrderRec(currRoot.getLeftChild());
        printPostOrderRec(currRoot.getRightChild());
        System.out.print(currRoot.getData() + ", ");
    }
    //Main Mthod
    public static void main(String[] args) {
        Tree obj = new Tree();
        //Inserting data
        obj.insertData(3);
        obj.insertData(5);
        obj.insertData(6);
        obj.insertData(2);
        obj.insertData(4);
        obj.insertData(1);
        obj.insertData(0);

        //printing content in Inorder way
        System.out.println("Inorder traversal");
        obj.printInorder();

        //printing content in Inorder way
        System.out.println("Preorder Traversal");
        obj.printPreorder();

        //printing content in Inorder way
        System.out.println("Postorder Traversal");
        obj.printPostorder();
    }
}
1
can you tell us what it prints??Dunes Buggy
Currently it is giving output as Inorder traversal 0, 1, 2, 3, 4, 5, 6, Preorder Traversal 3, 2, 1, 0, 5, 4, 6, Postorder Traversal 0, 1, 2, 4, 6, 5, 3,RJ_Singh
@Raj What is it the correct output?Evan Bechtol
@EvanBechtol Expected O/P : PreOrder : 3,1,0,2,5,4,6, PostOrder : 0,2,1,4,6,5,3 Getting O/P : PreOrder : 3,2,1,0,5,4,6, PostOrder: 0,1,2,4,6,5,3 –RJ_Singh
@Raj The output is the correct preorder and postorder traversal. (en.wikipedia.org/wiki/Tree_traversal). You should recheck your definition of preorder, postorderDunes Buggy

1 Answers

0
votes

Look my friend,your code is absolutely fine as the outputs you mentioned are absolutely correct.

I think you have not understood the concept of Binary Search Tree correctly.

You are right,3 is root node but you wrong in saying that 1 is its left child.

The first value that appears after 3 and that is smaller than 3 is 2,therefore 2 is left child of 3 and not 1

Refer to Cormenn book if still there is confusion.