how to import binary search tree class to this code? public class Dictionary {
Question:
how to import binary search tree class to this code?
"public class Dictionary { private Node root; // This is the root node of the Binary Search Tree // Constructor to initialize the root node to null public Dictionary() { root = null; } // Method to add a new entry to the dictionary public void addEntry(String key, String value) { Node newNode = new Node(key, value); // Create a new node with the given key and value if (root == null) { // If the root is null, this is the first node to be added to the tree root = newNode; // Set the root to be the new node } else { // If the root is not null, we need to traverse the tree to find the correct position for the new node Node current = root; // Start at the root of the tree Node parent; while (true) { // Loop indefinitely until we find the correct position for the new node parent = current; // Set the parent node to the current node if (key.compareTo(current.getKey()) < 0) { // If the key is less than the current node's key, move to the left child current = current.getLeftChild(); if (current == null) { // If the left child is null, we've found the correct position for the new node parent.setLeftChild(newNode); // Set the left child of the parent node to be the new node return; // Exit the method } } else { // If the key is greater than or equal to the current node's key, move to the right child current = current.getRightChild(); if (current == null) { // If the right child is null, we've found the correct position for the new node parent.setRightChild(newNode); // Set the right child of the parent node to be the new node return; // Exit the method } } } } } // Method to search for an entry in the dictionary public String search(String key) { Node current = root; // Start at the root of the tree while (current != null && !current.getKey().equals(key)) { // Loop while the current node is not null and its key does not match the search key if (key.compareTo(current.getKey()) < 0) { // If the key is less than the current node's key, move to the left child current = current.getLeftChild(); } else { // If the key is greater than or equal to the current node's key, move to the right child current = current.getRightChild(); } } if (current == null) { // If we've reached the end of the tree without finding the search key, return null return null; } else { // If we've found the node with the search key, return its value return current.getValue(); } } // Method to print the entire dictionary in alphabetical order public void printDictionary() { printTree(root); // Call the recursive printTree method with the root of the tree as the starting point } // Recursive method to print the tree in alphabetical order private void printTree(Node root) { if (root != null) { // If the root node is not null, there are still nodes to print printTree(root.getLeftChild()); // Recursively print the left subtree System.out.println(root.getKey() + " : " + root.getValue()); // Print the current node's key and value printTree"
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates