Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Binary Search tree

Options
  • 27-11-2017 6:00pm
    #1
    Closed Accounts Posts: 191 ✭✭


    Hi i have got a program that creates a BST in Java i have a method to insert data into the tree and i have a method that prints the data in order. Can anyone help me modify my print method so it prints like the following eg.

    Root 50
    New Leaf
    Left child 20 , Right child 60
    New Leaf
    Left child 10 , Right child etc
    etc...

    I will post my code i have made so far below, Thanks.

    package a4;
    
    public class BinaryTree {
    
        Node root;
    
        public void add(int data) {
            Node nodeToAdd = new Node(data);
            if (root == null) {
                root = nodeToAdd;
            }
    
            traverseAndAddNode(root, nodeToAdd);
    
        }
    
        public void traverseAndAddNode(Node node, Node nodeToAdd) {
            if (nodeToAdd.getData() < node.getData()) {
    
                if (node.getLeftChild() == null) {
                    node.setLeftChild(nodeToAdd);
                } else {
                    traverseAndAddNode(node.getLeftChild(), nodeToAdd);
                }
                traverseAndAddNode(node.getLeftChild(), nodeToAdd);
            } else if (nodeToAdd.getData() > node.getData()) {
    
                if (node.getRightChild() == null) {
                    node.setRightChild(nodeToAdd);
                } else {
                    traverseAndAddNode(node.getRightChild(), nodeToAdd);
                }
                traverseAndAddNode(node.getRightChild(), nodeToAdd);
            }
        }
    
        public void traverse() {
            if (root != null) {
    
                Node nodeToTraverse = root;
                if (nodeToTraverse.getLeftChild() == null && nodeToTraverse.getRightChild() == null) {
                    System.out.println(nodeToTraverse.getData());
                } else {
                    inOrderTraversal(nodeToTraverse);
                }
            }
        }
    
        public void inOrderTraversal(Node node) {
            
            System.out.println(node.getData());
            
            if (node.getLeftChild() != null) {
    
                inOrderTraversal(node.getLeftChild());
            }
            
            if (node.getRightChild() != null) {
    
                inOrderTraversal(node.getRightChild());
            }
        }
    }
    
    
    
    Tagged:


Comments

  • Registered Users Posts: 6,150 ✭✭✭Talisman


    You want an in order traversal so the pattern is :

    1 - Visit left sub-tree
    2 - Display node value
    3 - Visit right sub-tree


Advertisement