Home / Expert Answers / Other / a-the-add-method-in-binarysearchtree-calls-the-addnode-methodin-the-inner-node-class-to-pr

(Solved): a. The add method in BinarySearchTree calls the addNode methodin the inner Node class to p ...



ordering. Study these twomethods and draw a picture of the tree that is constructed when yourun the TreeTester program below. Label each node in the tree withthe object that is stored there.

public class BinarySearchTree{ private Node root; public BinarySearchTree() { } public void add(Comparable obj) { Node newNode = new Node(); newNode.data = obj; newNode.left = null; newNode.right = null; if (root == null) { root = newNode; } else { root.addNode(newNode); } } class Node { public Comparable data; public Node left; public Node right; public void addNode(Node newNode) { int comp = newNode.data.compareTo(data); if (comp < 0) { if (left == null) { left = newNode; } else { left.addNode(newNode); } } else if (comp > 0) { if (right == null) { right = newNode; } else { right.addNode(newNode); } } } }} ------------------------------------------------------ /** This program tests the binary search tree class.*/public class TreeTester{ public static void main(String[] args) { BinarySearchTree t = new BinarySearchTree(); t.add("D"); t.add("B"); t.add("A"); t.add("C"); t.add("F"); t.add("E"); t.add("I"); t.add("G"); t.add("H"); }}

b. What would the tree look like if we ran the following versionof the TreeTester program?

public class TreeTester{ public static void main(String[] args) { BinarySearchTree t = new BinarySearchTree(); t.add("A"); t.add("B"); t.add("C"); t.add("D"); }}


We have an Answer from Expert

View Expert Answer

Expert Answer


Answer to a. The add method in BinarySearchTree calls the addNode method in the inner Node class to produce the ordering. Study th...
We have an Answer from Expert

Buy This Answer $6

Place Order