Question

In: Computer Science

1. Modify a binary search tree implementation code (that AVL tree code depends on) to print...

1. Modify a binary search tree implementation code (that AVL tree code depends on) to print out the data value for each node it encounters in the insert operation.

Remember that the module AVL tree code gets part of its functionality from the BST type, since an AVL tree is a binary search tree, but adds some additional functionality.

2. Add code to the main method to meet the following requirements:

(a) Create an AVL tree to hold names stored as String objects (one name in each node of the tree)

(b) Repeat the following at least four times: i. Insert another String data node whose value ’comes after’ (lexicographically) the value last inserted, as ’banana’ comes after ’apple’, using standard tree operations

Try insert

ii. Print out the entire tree contents of the tree using a preorder traversal

Solutions

Expert Solution

Program

// An AVL tree node
class AVLTree
{
   //AVLTreeNode(inner) class
   public class AVLTreeNode
   {
       public String key;
       public AVLTreeNode left;
       public AVLTreeNode right;
       public char balanceFactor;
       int height;
      
       //constructoror
       public AVLTreeNode(String key, AVLTreeNode left, AVLTreeNode right)
       {
           this.key = key;
           this.left = left;
           this.right = right;
           height = 1;    // new AVLTreeNodeis initially added at leaf
           balanceFactor = '-';
       }
   }
  
   private AVLTreeNode root;
  
   //constructor
   public AVLTree()
   {
       root = null;
   }
  
   // A utility method to get height of the tree
   int height(AVLTreeNode N)
   {
       if (N == null)
           return 0;
       return max(height(N.left) , height(N.right)) + 1;
   }
  
   // A utility method to get maximum of two integers
   int max(int a, int b)
   {
       return (a > b)? a : b;
   }
  
   // A utility method to right rotate
   AVLTreeNode rightRotate(AVLTreeNode y)
   {
       AVLTreeNode x = y.left;
       AVLTreeNode T2 = x.right;
  
       // Perform rotation
       x.right = y;
       y.left = T2;
      
       return x;
   }
  
   // A utility function to left rotate
   AVLTreeNode leftRotate(AVLTreeNode x)
   {
       AVLTreeNode y = x.right;
       AVLTreeNode T2 = y.left;
      
       // Perform rotation
       y.left = x;
       x.right = T2;
      
       return y;
   }
  
   // Get Balance factor of AVLTreeNodeN
   char getBalance(AVLTreeNode N)
   {
       if (N == null)
       return '-';
       if(height(N.left) - height(N.right) > 1)
           return '/';
       if(height(N.left) - height(N.right) < -1)
           return '\\';
       return '-';
   }
  
   // Recursive method to insert key in subtree rooted
   // with AVLTreeNodeand returns new root of subtree.
   AVLTreeNode insert(AVLTreeNode node, String key)
   {
       /* 1. Perform the normal BST insertion */
       if (node== null)
           return new AVLTreeNode(key, null, null);
      
       System.out.print (node.key + " ");
       if (key.compareTo(node.key)<0)
           node.left = insert(node.left, key);
       else if (key.compareTo(node.key)>0)
           node.right = insert(node.right, key);
       else // Equal keys are not allowed in BST
           return node;
      
       /* 2. Update height of this ancestor AVLTreeNode*/
       node.height = node.height + 1;
      
       /* 3. Get the balance factor of this ancestor
       AVLTreeNodeto check whether this AVLTreeNodebecame
       unbalanced */
       char balance = getBalance(node);
      
       // If this AVLTreeNodebecomes unbalanced, then
       // there are 4 cases
      
       // Left Left Case
       if (balance == '/' && key.compareTo(node.left.key)<0)
           return rightRotate(node);
      
       // Right Right Case
       if (balance == '\\' && key.compareTo(node.right.key)>0)
           return leftRotate(node);
      
       // Left Right Case
       if (balance == '/' && key.compareTo(node.left.key)>0)
       {
           node.left = leftRotate(node.left);
           return rightRotate(node);
       }
      
       // Right Left Case
       if (balance == '\\' && key.compareTo(node.right.key)<0)
       {
           node.right = rightRotate(node.right);
           return leftRotate(node);
       }
      
       /* return the (unchanged) AVLTreeNodepointer */
       return node;
   }
  
   public void insert(String key)
   {
       root = insert(root, key);
       System.out.println ();
   }
   // Helper method to traverse the avltree in preorder
   private void preorder(AVLTreeNode root)
   {
       if(root!=null)
       {
           System.out.print(root.key+ " ");
           preorder(root.left);
           preorder(root.right);
       }
   }
   //method to traverse the avltree in preorder
   public void preorder()
   {
       preorder(root);
   }
}

//Driver class to test
class Driver
{
   //main method
   public static void main (String[] args)
   {
       //create object of AVLTree
       AVLTree avl = new AVLTree();
      
       //insert String data
       System.out.print ("Insert \"apple\": ");
       avl.insert("apple");
       System.out.print ("Insert \"banana\": ");
       avl.insert("banana");
       System.out.print ("Insert \"cat\": ");
       avl.insert("cat");
       System.out.print ("Insert \"dog\": ");
       avl.insert("dog");
       //traverse the avltree in preorder
       System.out.println ("Preorder traversal: ");
       avl.preorder();
   }
}

Output:

Insert "apple":
Insert "banana": apple
Insert "cat": apple banana
Insert "dog": banana cat
Preorder traversal:
banana apple cat dog

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason.

Thank you.


Related Solutions

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:...
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
​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.
Write an implementation of the set class, with associated iterators using a binary search tree. Add...
Write an implementation of the set class, with associated iterators using a binary search tree. Add to each node a link to the parent node. (C++)
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class...
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class a pointer to the parent node. Modify the insert and erase functions to properly set those pointers. b)Then define a TreeIterator class that contains a pointer to a TreeNode and has member functions get and next. i)The iterator’s get member function should return the data value of the node to which it points. ii)The iterator’s next member function should find the next element in...
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 . •...
AVL tree; Insert and Range Minimum operation in Java code.
AVL tree; Insert and Range Minimum operation in Java code.
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT