public void add(int data)
        {
            BinaryNode toAdd = new BinaryNode(data);

            if (root == null)
            {
                root = toAdd;
            }else{
                addRec(root, toAdd);
            }
        }
 public BinarySearchTree()
 {
     root = null;
 }
 public void setRight(BinaryNode newRight)
 {
     right = newRight;
 }
 public void setLeft(BinaryNode newLeft)
 {
     left = newLeft;
 }
 private void preOrderTraversalRec(BinaryNode subTree)
 {
     if (subTree != null)
     {
         Console.Out.Write(subTree.getData());
         Console.Out.Write(" ");
         preOrderTraversalRec(subTree.getLeft());
         preOrderTraversalRec(subTree.getRight());
     }
 }
Example #6
0
 public BinaryTree(T rootValue)
 {
     Root = new BinaryNode <T>(rootValue);
 }
Example #7
0
 public void SetUp()
 {
     _bst = CreateBst();
     _notBst = CreateNotBst();
     _comparer = Comparer<int>.Default;
 }
Example #8
0
 public void Add(T item)
 {
     root    = new BinaryNode <T>(null, item);
     _count += 1;
 }
        //======Private Methods======
        private void deleteValueRec(BinaryNode prevNode, BinaryNode subTree, int value)
        {
            BinaryNode nextNode = null;
            if (subTree != null)
            {
                if (subTree.getData() == value)
                {
                    deleteNode(prevNode, subTree, value);
                }
                else if (subTree.getData() > value)
                {
                    nextNode = subTree.getLeft();

                    deleteValueRec(subTree, nextNode, value);
                }
                else
                {
                    nextNode = subTree.getRight();

                    deleteValueRec(subTree, nextNode, value);
                }
            }
        }
 private void deleteNode(BinaryNode prev, BinaryNode curr, int value)
 {
     BinaryNode temp = null;
     if (prev == null)
     {
         if (root.getLeft() == null && curr.getRight() == null)
         {
             root = null;
         }
         else if (root.getLeft() != null && curr.getRight() == null)
         {
             root = root.getLeft();
         }
         else if (root.getLeft() == null && curr.getRight() != null)
         {
             root = root.getRight();
         }
         else
         {
             temp = root;
             root = getNDelNextInOrderNode(null, root);
             root.setLeft(temp.getLeft());
             root.setRight(temp.getRight());
         }
     }
     else
     {
         if (curr.getLeft() != null && curr.getRight() == null)
         {
             if (prev.getData() > value)
             {
                 prev.setLeft(curr.getLeft());
             }
             else
             {
                 prev.setRight(curr.getRight());
             }
         }
         else if (curr.getLeft() == null && curr.getRight() != null)
         {
             if (prev.getData() > value)
             {
                 prev.setLeft(curr.getRight());
             }
             else
             {
                 prev.setRight(curr.getRight());
             }
         }
         else
         {
             if (prev.getData() > value)
             {
                 temp = curr;
                 prev.setLeft(getNDelNextInOrderNode(prev, curr));
                 prev.getLeft().setLeft(curr.getLeft());
                 prev.getLeft().setRight(curr.getRight());
             }
             else
             {
                 temp = curr;
                 prev.setRight(getNDelNextInOrderNode(prev, curr));
                 prev.getRight().setLeft(curr.getLeft());
                 prev.getRight().setRight(curr.getRight());
             }
         }
     }
 }
 public BinaryNode(int data)
 {
     this.data = data;
     left = null;
     right = null;
 }
 private void addRec(BinaryNode subTree, BinaryNode toAdd)
 {
     if (subTree.getData() > toAdd.getData())
     {
         if(subTree.getLeft() == null){
             subTree.setLeft(toAdd);
         }else{
             addRec(subTree.getLeft(), toAdd);
         }
     }else{
         if(subTree.getRight() == null){
             subTree.setRight(toAdd);
         }else{
             addRec(subTree.getRight(), toAdd);
         }
     }
 }
Example #13
0
 public BinaryNode(M value, BinaryNode <M> parent = null)
 {
     Value  = value;
     Parent = parent;
 }
Example #14
0
        private void ProcessNode(BinaryNode <T> node, Action <BinaryNode <T> > action, RoutingMode routing, DirectionMode direction)
        {
            if (node == null)
            {
                return;
            }

            switch (routing)
            {
            case RoutingMode.Direct:
            {
                action?.Invoke(node);

                if (direction == DirectionMode.Left)
                {
                    ProcessNode(node.LeftSon, action, routing, direction);
                    ProcessNode(node.RightSon, action, routing, direction);
                }
                else if (direction == DirectionMode.Right)
                {
                    ProcessNode(node.RightSon, action, routing, direction);
                    ProcessNode(node.LeftSon, action, routing, direction);
                }

                break;
            }

            case RoutingMode.Internal:
            {
                if (direction == DirectionMode.Left)
                {
                    ProcessNode(node.LeftSon, action, routing, direction);
                    action?.Invoke(node);
                    ProcessNode(node.RightSon, action, routing, direction);
                }
                else if (direction == DirectionMode.Right)
                {
                    ProcessNode(node.RightSon, action, routing, direction);
                    action?.Invoke(node);
                    ProcessNode(node.LeftSon, action, routing, direction);
                }

                break;
            }

            case RoutingMode.Inverted:
            {
                if (direction == DirectionMode.Left)
                {
                    ProcessNode(node.LeftSon, action, routing, direction);
                    ProcessNode(node.RightSon, action, routing, direction);
                }
                else if (direction == DirectionMode.Right)
                {
                    ProcessNode(node.RightSon, action, routing, direction);
                    ProcessNode(node.LeftSon, action, routing, direction);
                }

                action?.Invoke(node);
                break;
            }
            }
        }
Example #15
0
        private void DeleteNode(BinaryNode <T> node, DirectionMode direction)
        {
            if (node.IsLeaf)
            {
                if (!node.HasParent)
                {
                    Root = null;
                }
                else
                {
                    if (node.Parent.LeftSon == node)
                    {
                        node.Parent.LeftSon = null;
                    }
                    else if (node.Parent.RightSon == node)
                    {
                        node.Parent.RightSon = null;
                    }
                }
            }
            else if (node.OnlyOneSon)
            {
                if (!node.HasParent)
                {
                    if (node.HasLeftSon)
                    {
                        Root = node.LeftSon;
                        node.LeftSon.Parent = null;
                    }
                    else if (node.HasRightSon)
                    {
                        Root = node.RightSon;
                        node.RightSon.Parent = null;
                    }
                }
                else
                {
                    if (node.HasLeftSon)
                    {
                        if (node.Parent.LeftSon == node)
                        {
                            node.LeftSon.Parent = node.Parent;
                            node.Parent.LeftSon = node.LeftSon;
                        }
                        else if (node.Parent.RightSon == node)
                        {
                            node.LeftSon.Parent  = node.Parent;
                            node.Parent.RightSon = node.LeftSon;
                        }
                    }
                    else if (node.HasRightSon)
                    {
                        if (node.Parent.LeftSon == node)
                        {
                            node.RightSon.Parent = node.Parent;
                            node.Parent.LeftSon  = node.RightSon;
                        }
                        else if (node.Parent.RightSon == node)
                        {
                            node.RightSon.Parent = node.Parent;
                            node.Parent.RightSon = node.RightSon;
                        }
                    }
                }
            }

            else if (node.BothSons)
            {
                if (direction == DirectionMode.Left)
                {
                    var buffNode = FindMax(node.LeftSon);
                    node.Value = buffNode.Value;
                    DeleteNode(buffNode, direction);
                }

                else if (direction == DirectionMode.Right)
                {
                    var buffNode = FindMin(node.RightSon);
                    node.Value = buffNode.Value;
                    DeleteNode(buffNode, direction);
                }
            }
        }
 private int getHeightRec(BinaryNode subTree)
 {
     if (subTree == null)
     {
         return 0;
     }
     else
     {
         return Math.Max(getHeightRec(subTree.getLeft()), getHeightRec(subTree.getRight())) + 1;
     }
 }
Example #17
0
 public BinaryNode(BinaryNode <T> parent, T item)
 {
     _children = new List <BinaryNode <T> >();
     _parent   = parent;
     _item     = item;
 }
        private BinaryNode getNDelNextInOrderNode(BinaryNode prev, BinaryNode currNode)
        {
            BinaryNode prevNode = currNode;
            BinaryNode toReturn = currNode.getRight();
            bool executed = false;

            while (toReturn.getLeft() != null)
            {
                prevNode = toReturn;
                toReturn = toReturn.getLeft();
                executed = true;
            }

            if (executed == true)
            {
                prevNode.setLeft(null);
            }
            else
            {
                prevNode.setRight(null);
            }

            return toReturn;
        }
Example #19
0
 protected override bool CheckTree(BinaryNode<int> node)
 {
     return node.IsBinarySearchTree1(_comparer);
 }
 private bool hasValueRec(BinaryNode subTree, int value)
 {
     if (subTree == null)
     {
         return false;
     }
     else if (subTree.getData() == value)
     {
         return true;
     }
     else if (subTree.getData() > value)
     {
         return hasValueRec(subTree.getLeft(), value);
     }
     else
     {
         return hasValueRec(subTree.getRight(), value);
     }
 }
Example #21
0
 public BinaryNode(object data)
 {
     this.data = data;
     left      = right = null;
 }
Example #22
0
 public BinaryTree()
 {
     root = null;
 }