コード例 #1
0
        private static void Zig(BinarySearchTreeNode node, BinarySearchTreeNode.SideOfParent side)
        {
            var p = node.Parent;

            BinarySearchTreeNode.ReplaceChild(p.Parent, p, node);

            if (side == BinarySearchTreeNode.SideOfParent.LEFT)
            {
                var nr = node.Right;
                node.Right = p;
                p.Left     = nr;
            }
            else
            {
                var nl = node.Left;
                node.Left = p;
                p.Right   = nl;
            }
        }
コード例 #2
0
ファイル: BinarySearchTree.cs プロジェクト: Mattsull01/CSCore
        private BinarySearchTreeNode <T> Insert(ref BinarySearchTreeNode <T> currentNode, T value)
        {
            if (currentNode == null)
            {
                currentNode = new BinarySearchTreeNode <T>(value);
                return(currentNode);
            }

            if (value.CompareTo(currentNode.Value) <= 0)
            {
                Insert(ref currentNode.Left, value);
            }
            else
            {
                Insert(ref currentNode.Right, value);
            }

            return(currentNode);
        }
コード例 #3
0
        public BinarySearchTreeNode Delete(BinarySearchTreeNode root, int data)
        {
            if (root == null)
            {
                return(null);
            }
            if (data < root.Data)
            {
                Delete(root.left, data);
            }
            else if (data > root.Data)
            {
                Delete(root.right, data);
            }
            else
            {
                if (root.left == null && root.right == null)
                {
                    root = null;
                    return(root);
                }

                if (root.left != null && root.right == null)
                {
                    root = root.left;
                    return(root);
                }
                else if (root.left != null && root.right == null)
                {
                    root = root.right;
                    return(root);
                }

                if (root.left != null && root.right != null)
                {
                    var replacementNode = Min(root.right);
                    root.Data  = replacementNode.Data;
                    root.right = Delete(replacementNode, replacementNode.Data);
                }
            }
            return(root);
        }
コード例 #4
0
        public static BinarySearchTreeNode Delete(BinarySearchTreeNode node)
        {
            if (node.Right == null)
            {
                var nl = node.Left;
                BinarySearchTreeNode.ReplaceChild(node.Parent, node, nl);
                node.Erase();

                return(nl);
            }

            var next = Next(node);

            BinarySearchTreeNode.ReplaceChild(next.Parent, next, next.Right);
            BinarySearchTreeNode.ReplaceChild(node.Parent, node, next);
            next.Left  = node.Left;
            next.Right = node.Right;
            node.Erase();

            return(next);
        }
コード例 #5
0
        public static void Insert(long key, BinarySearchTreeNode root)
        {
            var parent = Find(key, root);

            if (parent.Key == key)
            {
                return;
            }

            //Add as child
            var node = new BinarySearchTreeNode {
                Key = key, Parent = parent
            };

            if (parent.Key > key)
            {
                parent.Left = node;
            }
            else
            {
                parent.Right = node;
            }
        }
コード例 #6
0
ファイル: BinarySearchTree.cs プロジェクト: Mattsull01/CSCore
        private BinarySearchTreeNode <T> Search(BinarySearchTreeNode <T> currentNode, T value)
        {
            if (currentNode == null)
            {
                return(null);
            }

            if (currentNode.Value.CompareTo(value) == 0)
            {
                return(currentNode);
            }

            if (currentNode.Left != null && value.CompareTo(currentNode.Value) <= 0)
            {
                return(Search(currentNode.Left, value));
            }

            if (currentNode.Right != null)
            {
                return(Search(currentNode.Right, value));
            }

            return(null);
        }
コード例 #7
0
 public static void Delete(BinarySearchTreeNode node)
 {
     BinarySearchTree.Delete(node);
     Rebalance(node);
 }
コード例 #8
0
 public static BinarySearchTreeNode Next(BinarySearchTreeNode node)
 {
     return((node.Right != null) ? LeftDescendant(node.Right) : RightAncestor(node));
 }
コード例 #9
0
        public static IEnumerable <BinarySearchTreeNode> RangeSearch(long leftKey, long rightKey, BinarySearchTreeNode root)
        {
            var results = new List <BinarySearchTreeNode>();
            var node    = Find(leftKey, root);

            while (node != null && node.Key <= rightKey)
            {
                if (node.Key >= leftKey)
                {
                    results.Add(node);
                }

                node = Next(node);
            }
            return(results);
        }
コード例 #10
0
 private static void MergeAsRoot(BinarySearchTreeNode left, BinarySearchTreeNode right, BinarySearchTreeNode root)
 {
     root.Left    = left;
     root.Right   = right;
     left.Parent  = root;
     right.Parent = root;
 }
コード例 #11
0
 public static void Insert(long key, BinarySearchTreeNode root)
 {
     BinarySearchTree.Insert(key, root);
     Rebalance(root);
 }
コード例 #12
0
 private static BinarySearchTreeNode TreeMergeWithRoot(BinarySearchTreeNode left, BinarySearchTreeNode right, BinarySearchTreeNode root)
 {
     if (Math.Abs(left.Rank - right.Rank) <= 1)
     {
         MergeAsRoot(left, right, root);
         AdjustHeight(root);
         //return this;
     }
     else if (left.Rank > right.Rank)
     {
         var rightPrime = TreeMergeWithRoot(left.Right, right, root);
         left.Right        = rightPrime;
         rightPrime.Parent = left;
         Rebalance(left);
         //return this;
     }
     else if (left.Rank < right.Rank)
     {
         var leftPrime = TreeMergeWithRoot(left, right.Right, root);
         right.Right      = leftPrime;
         leftPrime.Parent = right;
         Rebalance(right);
         //return this;
     }
     return(root);
 }
コード例 #13
0
        public static BinarySearchTreeNode Delete(long key, BinarySearchTreeNode root)
        {
            var node = Find(key, root);

            return((node.Key == key) ? Delete(node) : node);
        }
コード例 #14
0
 public BinarySearchTreeNode(int data)
 {
     Data  = data;
     left  = null;
     right = null;
 }
コード例 #15
0
 private static BinarySearchTreeNode Delete(BinarySearchTreeNode node)
 {
     Splay(BinarySearchTree.Next(node));
     Splay(node);
     return(BinarySearchTree.Delete(node));
 }
コード例 #16
0
 public BinarySearchTreeNodePrinter(BinarySearchTreeNode node)
 {
     _original = node;
     _sb       = new StringBuilder();
 }
コード例 #17
0
ファイル: BinarySearchTree.cs プロジェクト: Mattsull01/CSCore
        /// <summary>
        /// Inserts and returns item
        /// </summary>
        /// <param name="value">Item value to be inserted</param>
        /// <returns></returns>
        public BinarySearchTreeNode <T> Insert(T value)
        {
            var node = Root;

            return(Root = Insert(ref node, value));
        }
コード例 #18
0
 public void Insert(int data)
 {
     root = InsertRec(root, data);
 }
コード例 #19
0
 private static void AdjustHeight(BinarySearchTreeNode node)
 {
     node.Rank = 1 + Math.Max(node.Left.Rank, node.Right.Rank);
 }
コード例 #20
0
 public static Tuple <BinarySearchTreeNode, BinarySearchTreeNode> Split(long key, BinarySearchTreeNode root)
 {
     if (key < root.Right.Key)
     {
         var s     = Split(key, root.Left);
         var three = TreeMergeWithRoot(s.Item2, root.Right, root);
         return(new Tuple <BinarySearchTreeNode, BinarySearchTreeNode>(s.Item1, three));
     }
     else
     {
         var s     = Split(key, root.Right);
         var three = TreeMergeWithRoot(s.Item2, root.Left, root);
         return(new Tuple <BinarySearchTreeNode, BinarySearchTreeNode>(s.Item1, three));
     }
 }
コード例 #21
0
 public static BinarySearchTreeNode Insert(long key, BinarySearchTreeNode root)
 {
     BinarySearchTree.Insert(key, root);
     return(Find(key, root));
 }