简体   繁体   中英

Binary search tree implementation in java with traversal

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

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.

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