Question

In: Computer Science

Here is a picture of a Binary Search Tree. First, construct the Binary Search Tree using...

Here is a picture of a Binary Search Tree.



First, construct the Binary Search Tree using the following BinaryNode as we discussed in class.

public class BinaryNode {
        private int value;
        private BinaryNode leftChild;
        private BinaryNode rightChild;

        public BinaryNode(int value) {
                this.value = value;
                leftChild = null;
                rightChild = null;
        }

        public BinaryNode(int value, BinaryNode leftChild, BinaryNode rightChild)
        {
                this.value = value;
                this.leftChild = leftChild;
                this.rightChild = rightChild;
        }

        public int getValue() {
                return value;
        }

        public void setValue(int value) {
                this.value = value;
        }

        public BinaryNode getLeftChild() {
                return leftChild;
        }

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

        public BinaryNode getRightChild() {
                return rightChild;
        }

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

        @Override
        public String toString() {
                return "BinaryNode: " +
                                "value=" + value;
        }
}

Second, print the nodes in level order, that is, the root node first, then the children of the root node, then the grand-children, etc. It is recommended that you accomplish this by using a queue to store the nodes, printing the first nodes that have been added to the queue.

Your program should print the following when it runs.

42 27 50 21 38 60 33 41 72

Submit the file LevelOrder.java when done.

Solutions

Expert Solution

//BinaryNode.java

public class BinaryNode {
        private int value;
        private BinaryNode leftChild;
        private BinaryNode rightChild;

        public BinaryNode(int value) {
                this.value = value;
                leftChild = null;
                rightChild = null;
        }

        public BinaryNode(int value, BinaryNode leftChild, BinaryNode rightChild)
        {
                this.value = value;
                this.leftChild = leftChild;
                this.rightChild = rightChild;
        }

        public int getValue() {
                return value;
        }

        public void setValue(int value) {
                this.value = value;
        }

        public BinaryNode getLeftChild() {
                return leftChild;
        }

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

        public BinaryNode getRightChild() {
                return rightChild;
        }

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

        @Override
        public String toString() {
                return "BinaryNode: " +
                                "value=" + value;
        }
}

//BinarySearchTree.java

public class BinarySearchTree{
   private BinaryNode root;
  
   public BinarySearchTree() {
       root = null;
   }
  
   private BinaryNode insert(BinaryNode root,int data) {
       if(root==null)return new BinaryNode(data,null,null);
       if(root.getValue()>data)
           root.setLeftChild(insert(root.getLeftChild(),data));
      
       if(root.getValue() < data)
           root.setRightChild(insert(root.getRightChild(),data));
       return root;
   }
  
   public void insert(int data) {
       root = insert(root , data);
   }
  
   public void levelOrder() {
        if (root == null)
          return;
  
        Queue<BinaryNode> queue = new LinkedList<>();
        queue.add(root);
        queue.add(null);
        while (!queue.isEmpty()) {
  
          BinaryNode curr = queue.poll();
          if (curr == null) {
            if (!queue.isEmpty()) {
              queue.add(null);
            }
          } else {
            if (curr.getLeftChild() != null)
              queue.add(curr.getLeftChild());
  
            if (curr.getRightChild() != null)
              queue.add(curr.getRightChild());
  
            System.out.print(curr.getValue() + " ");
          }
        }
      }
}

//LevelOrder.java

import java.util.LinkedList;
import java.util.Queue;

public class LevelOrder {
   public static void main(String arg[]) {
       //42 27 50 21 38 60 33 41 72
       BinarySearchTree bst = new BinarySearchTree();
       bst.insert(42);
       bst.insert(27);
       bst.insert(50);
       bst.insert(21);
       bst.insert(38);
       bst.insert(60);
       bst.insert(33);
       bst.insert(41);
       bst.insert(72);
      
       bst.levelOrder();
   }
}


Related Solutions

Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
M = [4,3,7,6,5,2,4,1,0,7] Construct a binary search tree for M. Then traverse it (post order)
M = [4,3,7,6,5,2,4,1,0,7] Construct a binary search tree for M. Then traverse it (post order)
C++ Instantiate a binary search tree object and create such tree using elements of the sequence...
C++ Instantiate a binary search tree object and create such tree using elements of the sequence 8,3,10, 1,6,9, 14, 4,7, 13 with 8 as root of the tree. Find maximum and minimum elements of the tree, successor(10) and predecessor(13), print the inorder, postorder and preorder traversal of the tree.
How to read and print all contents in a binary file using a Binary Search Tree...
How to read and print all contents in a binary file using a Binary Search Tree inorder traversal in C. Additionally, how to search using a Binary Search Tree to display the specific Athlete and his/her information. An example would be a sports record binary file that consist of name of athlete, height , weight, championships won. Athlete: Michael Jordan Height: 6’6” Weight : 205 lbs Championships won: 6 =================== Athlete: LeBron James Height: 6’8” Weight: 250 lbs Championships won:...
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary Tree Node structure class Node: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None class BSTree(): def __init__(self, rootdata): self.root = Node(rootdata)    def insert(self, data, cur_node): if data < cur_node.data: if cur_node.left == None: cur_node.left = Node(data) else: self.insert(data, cur_node.left)    elif data > cur_node.data: if cur_node.right == None: cur_node.right = Node(data) else:...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT