Example #1
0
        /**
         * Insert an item into the tree
         *
         * @param {[type]} int Data the new item to add to the three
         * @return true if the item was inserted (ie it was not already in the tree)
         */
        public bool Insert(int Data)
        {
            TreeNode DataNode = new TreeNode(Data);
            bool success = false;
            treeLock.EnterWriteLock();
            if(head == null){
                head = DataNode;
                success = true;
            }else{
                var node = head;
                while(node != null && !success){
                    var LeftValid = node.Left != null;
                    var RightValid = node.Right != null;
                    if(DataNode.Data < node.Data){
                        if(LeftValid) node = node.Left;
                        else{
                            node.Left = DataNode;
                            success = true;
                        }
                    }else if(DataNode.Data > node.Data){
                        if(RightValid) node = node.Right;
                        else{
                            node.Right = DataNode;
                            success = true;
                        }
                    }else break;
                }

            }
            treeLock.ExitWriteLock();
            return success;
        }
Example #2
0
 public SearchTree()
 {
     head = null;
     treeLock = new ReaderWriterLockSlim();
 }
Example #3
0
 //recursive helper routine for the InOrder enumerator
 private IEnumerable<int> InOrder(TreeNode node)
 {
     if(node != null){
         foreach(var x in InOrder(node.Left))
             yield return x;
         yield return head.Data;
         foreach(var x in InOrder(node.Right))
             yield return x;
     }
 }
Example #4
0
 public bool Remove(int value, TreeNode parent)
 {
     if(value < this.Data){
         if(Left != null) return Left.Remove(value, this);
         else return Right.Remove(value, this);
     }else if(value > this.Data){
         if(Right != null) return Right.Remove(value, this);
         else return false;
     }else{
         if(Left != null && Right != null){
             this.Data = Right.FindMin().Data;
             Right.Remove(this.Data, this);
         }else if(parent.Left == this){
             parent.Left = (Left == null) ? Right : Left;
         }else if(parent.Right == this){
             parent.Right = (Left == null) ? Right : Left;
         }
         return true;
     } // end mutex region
 }
Example #5
0
 /**
  * Remove an element from the tree.
  *
  * @param {[type]} int key @param key the data to be removed
  * @return true if the item was removed without error
  */
 public bool Remove(int key)
 {
     treeLock.EnterWriteLock();
     if(head == null){
         treeLock.ExitWriteLock();
         return false;
     }else {
         if(head.Data == key){
             var auxRoot = new TreeNode(0); //the 0 value is just a dumby value. Not used for anything
             auxRoot.Left = head;
             var result = head.Remove(key, auxRoot);
             head = auxRoot.Left;
             treeLock.ExitWriteLock();
             return result;
         }else{
             var success = head.Remove(key, null);
             treeLock.ExitWriteLock();
             return success;
         }
     }
 }