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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Binary Search tree

  • 27-11-2017 5: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, Registered Users 2 Posts: 6,286 ✭✭✭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