Question: I'm coding in C# and I need help understanding how to add a method(s) to the AVLTree class that accepts a value. Please use same
I'm coding in C# and I need help understanding how to add a method(s) to the AVLTree class that accepts a value. Please use same terminology as is found in the class below.
If the value is in the AVL Tree, the method prints the value and all the nodes within the value's subtree. For example: if 47 is passed into the method, it would print 47, 36, 65, and 77. Below the picture is base code. public class AVLTree
public AVLTree() { root = null; } private int GetHeight(Node temp) { if (temp == null) return -1; else return temp.Height; } public void Insert(T newItem) { root = Insert(newItem, root); } private Node Insert(T newItem, Node temp) { if (temp == null) return new Node(newItem, null, null); else if (newItem.CompareTo(temp.Data) < 0) { temp.Left = Insert(newItem, temp.Left); if ((GetHeight(temp.Left) - GetHeight(temp.Right)) == 2) if (newItem.CompareTo(temp.Left.Data) < 0) temp = RotateLeftChild(temp); else temp = DoubleRotationLeft(temp); } else if (newItem.CompareTo(temp.Data) > 0) { temp.Right = Insert(newItem, temp.Right); if ((GetHeight(temp.Right) - GetHeight(temp.Left)) == 2) if (newItem.CompareTo(temp.Right.Data) > 0) temp = RotateRightChild(temp); else temp = DoubleRotationRight(temp); } else // Duplicate throw new ApplicationException("Tree did not insert " + newItem + " since an item with that value is already in the tree");
temp.Height = Math.Max(GetHeight(temp.Left), GetHeight(temp.Right)) + 1; return temp; }
private Node RotateLeftChild(Node top) { Node left = top.Left; top.Left = left.Right; left.Right = top;
// update heights top.Height = Math.Max(GetHeight(top.Left), GetHeight(top.Right)) + 1; left.Height = Math.Max(GetHeight(left.Left), GetHeight(top)) + 1;
return left; } private Node RotateRightChild(Node top) { Node right = top.Right; top.Right = right.Left; right.Left = top;
// update heights top.Height = Math.Max(GetHeight(top.Left), GetHeight(top.Right)) + 1; right.Height = Math.Max(GetHeight(right.Left), GetHeight(top)) + 1;
return right; } private Node DoubleRotationLeft(Node temp) { temp.Left = RotateRightChild(temp.Left); return RotateLeftChild(temp); } private Node DoubleRotationRight(Node temp) { temp.Right = RotateLeftChild(temp.Right); return RotateRightChild(temp); } public T Find(T value) { return Find(value, root); } private T Find(T value, Node temp) { if (temp == null) throw new ApplicationException("BinSearchTree could not find " + value); else if (value.CompareTo(temp.Data) < 0) return Find(value, temp.Left); else if (value.CompareTo(temp.Data) > 0) return Find(value, temp.Right); else return temp.Data; } private int SubtreeBalance(Node temp) { UpdateHeight(temp.Left); UpdateHeight(temp.Right); if (temp == null) return 0; else if (temp.Left == null && temp.Right == null) return 0; else if (temp.Left == null) return -(temp.Right.Height + 1); else if (temp.Right == null) return temp.Left.Height + 1; else return temp.Left.Height - temp.Right.Height; } private int UpdateHeight(Node temp) { int height = -1; if (temp != null) { int l = UpdateHeight(temp.Left); int r = UpdateHeight(temp.Right); height = Math.Max(l, r) + 1; temp.Height = height; } return height; } } Show transcribed image text
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
