Advertisement
yahorrr

Untitled

Nov 11th, 2021
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. if (this.Root is null)
  2.             {
  3.                 return false;
  4.             }
  5.  
  6.             var result = RemoveHelper(this.Root, item);
  7.  
  8.             if (result is null)
  9.             {
  10.                 return false;
  11.             }
  12.  
  13.             --this.Count;
  14.             return true;
  15.  
  16.             BinarySearchTreeNode RemoveHelper(BinarySearchTreeNode root, T value)
  17.             {
  18.                 if (root is null)
  19.                 {
  20.                     return null;
  21.                 }
  22.  
  23.                 var comparisonResult = this.comparer.Compare(value, root.Value);
  24.                 if (comparisonResult < 0)
  25.                 {
  26.                     root.LeftNode = RemoveHelper(root.LeftNode, value);
  27.                 }
  28.                 else if (comparisonResult > 0)
  29.                 {
  30.                     root.RightNode = RemoveHelper(root.RightNode, value);
  31.                 }
  32.                 else if (root.LeftNode is null)
  33.                 {
  34.                     return root.RightNode;
  35.                 }
  36.                 else if (root.RightNode is null)
  37.                 {
  38.                     return root.LeftNode;
  39.                 }
  40.                 else
  41.                 {
  42.                     root.Value = GetMin(root.RightNode);
  43.                     root.RightNode = RemoveHelper(root.RightNode, root.Value);
  44.                 }
  45.  
  46.                 return root;
  47.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement