/// <summary> /// Accepts a node as input and inserts that node into the first avaliable nodes first avaliable child using left child as priority for insert. /// </summary> /// <param name="temp">Incoming node that will be inserted into tree.</param> private void AddHelper(Node temp) { // Creates a landing point for the TryGetValue from storage. Node current; // Grabs the first avaliable node from the CanAdd queue. Storage.TryGetValue(CanAdd.Peek(), out current); // Checks to see if there is storage avaliable in the current nodes left child. if (current.Left == 0) { // Sets the current nodes left value to the incoming nodes key value. current.Left = temp.Key; // Sets the incoming nodes parent value to current. temp.Parent = current.Key; } // Adds temp node to current nodes right child. else { current.Right = temp.Key; temp.Parent = current.Key; } // Adds the incoming node to storage. if (!Storage.ContainsKey(temp.Key)) { Storage.Add(temp.Key, temp); } CanAdd.Enqueue(temp.Key); // Checks to see if there is any storage space left in the node and dequeues if the nodes children are both occupied. if (current.Left != 0 && current.Right != 0) { CanAdd.Dequeue(); } }
//------ Add method and Add Related Methods ------------------------------------------------------------------------------------------ /// <summary> /// Adds a node to the binary tree with a generic value as input. /// </summary> /// <param name="value">object value</param> public void Add(int value) { // Creates a temp node using Counter as the nodes key and user input value as the node's value. Node temp = new Node(NextKeyValue, value); // Handles an empty tree. if (Root == 0) { Root = temp.Key; CanAdd.Enqueue(temp.Key); Storage.Add(NextKeyValue, temp); } // Calls the add helper method with the temp node as input. else { AddHelper(temp); } // Increases the Size of the Hashtree Size = Size + 1; // Increases the Counter of the Hashtree NextKeyValue = NextKeyValue + 1; }