/// <summary> /// Remove button click handler /// Removes entered value from chosen tree /// </summary> private void btnRemove_Click(object sender, EventArgs e) { try { int value = Convert.ToInt32(tbRemove.Text); if (value > 9999 || value < -999) { throw new OverflowException("Please enter number between [-999;9999]."); } switch (this.activeTab) { case Tab.BST: activeRoot = Binary_Search_Tree.Remove(activeRoot, value); bst_root = activeRoot; break; case Tab.AVL: activeRoot = AVL_Tree.Remove(activeRoot, value); avl_root = activeRoot; break; case Tab.RBT: break; case Tab.SPL: activeRoot = AVL_Tree.Remove(spl_root, value); spl_root = activeRoot; break; } activePictureBox.Invalidate(); } catch (OverflowException ex) { MessageBox.Show(ex.Message, ex.GetType().Name); } catch (FormatException) { } tbRemove.Text = string.Empty; }
/// <summary> /// Insert button click handler /// Inserts a value into chosen tree /// </summary> private void btnInsert_Click(object sender, EventArgs e) { try { int value = Convert.ToInt32(tbInsert.Text); if (value > 9999 || value < -999) { throw new OverflowException("Please enter number between [-999;9999]."); } switch (this.activeTab) { case Tab.BST: if (this.total_nodes > 35) { MessageBox.Show("Node limit is set to 35!", "Warning"); return; } activeRoot = Binary_Search_Tree.Insert(activeRoot, value); bst_root = activeRoot; break; case Tab.AVL: if (this.total_nodes > 60) { MessageBox.Show("Node limit is set to 60!", "Warning"); return; } activeRoot = AVL_Tree.Insert(activeRoot, value); avl_root = activeRoot; break; case Tab.RBT: break; case Tab.SPL: if (this.total_nodes > 35) { MessageBox.Show("Node limit is set to 35!", "Warning"); return; } activeRoot = Splay_Tree.Insert(activeRoot, value); spl_root = activeRoot; break; } activePictureBox.Invalidate(); } catch (OverflowException ex) { MessageBox.Show(ex.Message, ex.GetType().Name); } catch (FormatException) { } tbInsert.Text = string.Empty; }
public static Node Splay(Node root, int value) { if (root == null || root.value == value) { return(root); } if (root.value > value) { if (root.lChild == null) { return(root); } // Zig-Zag (Left->Left) if (root.lChild.value > value) { root.lChild.lChild = Splay(root.lChild.lChild, value); root = AVL_Tree.rightRotate(root); } // Zig-Zag (Left->Right) else if (root.lChild.value < value) { root.lChild.rChild = Splay(root.lChild.rChild, value); if (root.lChild.rChild != null) { root.lChild = AVL_Tree.leftRotate(root.lChild); } } // Do second rotation for root return((root.lChild == null) ? root : AVL_Tree.rightRotate(root)); } else { if (root.rChild == null) { return(root); } // Zig-Zag (Right Left) if (root.rChild.value > value) { root.rChild.lChild = Splay(root.rChild.lChild, value); if (root.rChild.lChild != null) { root.rChild = AVL_Tree.rightRotate(root.rChild); } } else if (root.rChild.value < value)// Zag-Zag (Right Right) { root.rChild.rChild = Splay(root.rChild.rChild, value); root = AVL_Tree.leftRotate(root); } // Do second rotation for root return((root.rChild == null) ? root : AVL_Tree.leftRotate(root)); } }