Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Finally, my code provides validation. This is intended for testing purposes only, though I'm not sure how this should be exposed to a test project without making Node internal and then exposing the project's internals exposing the project's internals. Maybe there's no better way.

Finally, my code provides validation. This is intended for testing purposes only, though I'm not sure how this should be exposed to a test project without making Node internal and then exposing the project's internals. Maybe there's no better way.

Finally, my code provides validation. This is intended for testing purposes only, though I'm not sure how this should be exposed to a test project without making Node internal and then exposing the project's internals. Maybe there's no better way.

deleted 56 characters in body
Source Link
rookie
  • 1.2k
  • 3
  • 14
  • 32
 /// <summary>
 /// Sets/Replaces node located at child.key in root 
 /// and rebalances the tree if necessary
 /// </summary>
 private Node Set(Node root, Node child)
 {
 if (root == null) return child;
 int cmp = child.Key.CompareTo(root.Key);
 if (cmp == 0)
 return child;
 else if (cmp > 0)
 {
 root.Right = Set(root.Right, child);
 if (root.Right.Priority < root.Priority)
 root = LRotate(root);
 }
 else if (cmp < 0)
 {
 root.Left = Set(root.Left, child);
 if (root.Left.Priority < root.Priority)
 root = RRotate(root);
 }
 return root;
 }
 /// <summary>
 /// Deletes key from root by demoting it down to a leaf
 /// and rebalances the tree if necessary
 /// </summary>
 private Node Delete(Node root, TKey key)
 {
 if (root == null) return null;
 int cmp = key.CompareTo(root.Key);
 if (cmp < 0) root.Left = Delete(root.Left, key);
 if (cmp > 0) root.Right = Delete(root.Right, key);
 if (cmp== 0)
 {
 if (root.Left != null && root.Right != null)
 {
 if (root.Left.Priority < root.Right.Priority)
 root = RRotate(root); // it's a min-heap and L < R; promote left
 else
 root = LRotate(root); // it's a min-heap and R < L; promote right
 }
 else if (root.Left != null) root = RRotate(root);
 else if (root.Right != null) root = LRotate(root);
 else return null;
 root = Delete(root, key);
 }
 return root;
 } 
 /// <summary>
 /// Sets/Replaces node located at child.key in root 
 /// and rebalances the tree if necessary
 /// </summary>
 private Node Set(Node root, Node child)
 {
 if (root == null) return child;
 int cmp = child.Key.CompareTo(root.Key);
 if (cmp == 0)
 return child;
 else if (cmp > 0)
 {
 root.Right = Set(root.Right, child);
 if (root.Right.Priority < root.Priority)
 root = LRotate(root);
 }
 else if (cmp < 0)
 {
 root.Left = Set(root.Left, child);
 if (root.Left.Priority < root.Priority)
 root = RRotate(root);
 }
 return root;
 }
 /// <summary>
 /// Deletes key from root 
 /// and rebalances the tree if necessary
 /// </summary>
 private Node Delete(Node root, TKey key)
 {
 if (root == null) return null;
 int cmp = key.CompareTo(root.Key);
 if (cmp < 0) root.Left = Delete(root.Left, key);
 if (cmp > 0) root.Right = Delete(root.Right, key);
 if (cmp== 0)
 {
 if (root.Left != null && root.Right != null)
 {
 if (root.Left.Priority < root.Right.Priority)
 root = RRotate(root); // it's a min-heap and L < R; promote left
 else
 root = LRotate(root); // it's a min-heap and R < L; promote right
 }
 else if (root.Left != null) root = RRotate(root);
 else if (root.Right != null) root = LRotate(root);
 else return null;
 root = Delete(root, key);
 }
 return root;
 } 
 /// <summary>
 /// Sets/Replaces node located at child.key in root 
 /// and rebalances the tree if necessary
 /// </summary>
 private Node Set(Node root, Node child)
 {
 if (root == null) return child;
 int cmp = child.Key.CompareTo(root.Key);
 if (cmp == 0)
 return child;
 else if (cmp > 0)
 {
 root.Right = Set(root.Right, child);
 if (root.Right.Priority < root.Priority)
 root = LRotate(root);
 }
 else if (cmp < 0)
 {
 root.Left = Set(root.Left, child);
 if (root.Left.Priority < root.Priority)
 root = RRotate(root);
 }
 return root;
 }
 /// <summary>
 /// Deletes key from root by demoting it down to a leaf
 /// and rebalances the tree if necessary
 /// </summary>
 private Node Delete(Node root, TKey key)
 {
 if (root == null) return null;
 int cmp = key.CompareTo(root.Key);
 if (cmp < 0) root.Left = Delete(root.Left, key);
 if (cmp > 0) root.Right = Delete(root.Right, key);
 if (cmp== 0)
 {
 if (root.Left != null && root.Right != null)
 {
 if (root.Left.Priority < root.Right.Priority)
 root = RRotate(root); 
 else
 root = LRotate(root); 
 }
 else if (root.Left != null) root = RRotate(root);
 else if (root.Right != null) root = LRotate(root);
 else return null;
 root = Delete(root, key);
 }
 return root;
 } 
added 145 characters in body
Source Link
rookie
  • 1.2k
  • 3
  • 14
  • 32

Here is the public wrapper for Delete.

 public void Delete(TKey key)
 {
 root = Delete(root, key);
 }

Here is the public wrapper for Delete.

 public void Delete(TKey key)
 {
 root = Delete(root, key);
 }
added 440 characters in body
Source Link
rookie
  • 1.2k
  • 3
  • 14
  • 32
Loading
Tweeted twitter.com/StackCodeReview/status/656653880617488384
edited tags
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
Loading
deleted 1 character in body
Source Link
rookie
  • 1.2k
  • 3
  • 14
  • 32
Loading
added 87 characters in body
Source Link
rookie
  • 1.2k
  • 3
  • 14
  • 32
Loading
Source Link
rookie
  • 1.2k
  • 3
  • 14
  • 32
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /