/* * Recursive function to find a node containing specified data */ private Node m_findNode(ref Int32 data, ref Node parent) { if (parent != null) { if (parent.m_getData() > data) { return(this.m_findNode(ref data, ref parent.mLeft)); } else { return(parent.m_getData() < data?this.m_findNode(ref data, ref parent.mRight) : parent); } } return(null); }
/* * Find parent of a node */ private Node m_getParent(ref Node self, ref Node find) { if (find != this.mRoot || self == null) { return(null); } else { if (self.mLeft == find || self.mRight == find) { return(self); } else { if (self.m_getData() < find.m_getData()) { return(this.m_getParent(ref self.mRight, ref find)); } else { return(this.m_getParent(ref self.mLeft, ref find)); } } } }
/* * Delete node function. If value to delete is found then from the certain value, * 1) if there are no children: remove self * 2) if there is one child, shift it up and remove self * 3) if there are two children, get the min from right subtree and replace it with self */ private Node m_deleteNode(ref Int32 data, ref Node self) { if (self == null) { return(self); } else if (data < self.m_getData()) { self.mLeft = this.m_deleteNode(ref data, ref self.mLeft); } else if (data > self.m_getData()) { self.mRight = this.m_deleteNode(ref data, ref self.mRight); } else { if (self.mLeft == null && self.mRight == null) { self = null; return(self); } else if (self.mLeft == null) { self = self.mRight; return(self); } else if (self.mRight == null) { self = self.mLeft; return(self); } else { Int32 tData = this.m_getMinimum(ref self.mRight).m_getData(); self.m_setData(ref tData); self.mRight = this.m_deleteNode(ref tData, ref self.mRight); } } return(self); }
private String m_postOrderTraversal(ref Node parent, ref String key) { if (parent == null) { return(key); } if (parent.mLeft != null) { _ = this.m_postOrderTraversal(ref parent.mLeft, ref key); } if (parent.mRight != null) { _ = this.m_postOrderTraversal(ref parent.mRight, ref key); } key += " " + parent.m_getData(); return(key); }
/* * Level order traversing for saving tree data in files. Trees are always built by level orders * So it only makes sense to rebuild a tree with level order traversed data when loading from file */ private String m_levelOrderTraversal(ref Node parent, ref String key) { Queue <Node> queue = new Queue <Node>(); // Make a simple queue queue.Enqueue(parent); // Add all the current level nodes while (queue.Count != 0) { Node temporary = queue.Dequeue(); // Dequeue the current root node of the tree. key += temporary.m_getData() + " "; // Put its data in the key string if (temporary.mLeft != null) { queue.Enqueue(temporary.mLeft); // Add the level-- level nodes } if (temporary.mRight != null) { queue.Enqueue(temporary.mRight); // Same as above } } return(key); }
/* * Build a blob from self->data at width (x), and height (y). */ private async Task <Int32> makeBlob(Node self, Single width, Int32 height, Boolean isLeft, Boolean isRoot) { if (self == null) { return(await Task.FromResult(1)); } this.Controls.Add( new System.Windows.Forms.Label { AutoSize = true, Anchor = AnchorStyles.Top | AnchorStyles.Bottom, BackColor = isRoot ? Color.Black : ((isLeft) ? Color.LightGreen : Color.LightBlue), ForeColor = isRoot ? Color.White : Color.Black, Location = new System.Drawing.Point((Int32)(width), height), Name = "Blob" + (width / 2).ToString(), // Key "Blob" added here to keep track of labels Size = new System.Drawing.Size(35, 13), TabIndex = 8, Text = self.m_getData().ToString(), BorderStyle = BorderStyle.FixedSingle, Padding = new Padding(8) } ); return(await Task.FromResult(0)); }