Ejemplo n.º 1
0
 public void InOrderTreeWalk(RBNode node)
 {
     if (node != null)
     {
         InOrderTreeWalk(node.left);
         Console.WriteLine(node.key + " " + node.data + " is red? " + node.isRed);
         InOrderTreeWalk(node.right);
     }
 }
Ejemplo n.º 2
0
 public void Driver(string[] checkList, RBNode root)
 {
     for (int i = 0; i < checkList.Length; i++)
     {
         var timer = new Stopwatch();
         timer.Start();
         var node = TreeSearch(root, long.Parse(checkList[i]));
         timer.Stop();
         Console.WriteLine(node.key + " found in " + timer.Elapsed);
     }
 }
Ejemplo n.º 3
0
 public RBNode TreeSearch(RBNode node, long key)
 {
     if (node == null || key == node.key)
     {
         return(node);
     }
     if (key < node.key)
     {
         return(TreeSearch(node.left, key));
     }
     else
     {
         return(TreeSearch(node.right, key));
     }
 }
Ejemplo n.º 4
0
        public void Insert(RBTree tree, RBNode node)
        {
            RBNode y = null;
            RBNode x = tree.root;

            tree.size++;

            while (x != null)
            {
                y = x;
                if (node.key < x.key)
                {
                    x = x.left;
                }
                else
                {
                    x = x.right;
                }
            }
            node.parent = y;
            if (y == null)
            {
                tree.root = node;
            }
            else if (node.key < y.key)
            {
                y.left = node;
            }
            else
            {
                y.right = node;
            }
            node.left  = null;
            node.right = null;
            node.isRed = true;
            RBInsertFixup(tree, node);
        }
Ejemplo n.º 5
0
        public RBTree TreeMake(List <string> l1)
        {
            char[]        splitter = { '\n', '\r', ',' };
            List <string> list1    = new List <string>();
            List <string> list2    = new List <string>();


            for (int i = 0; i < l1.Count; i++)
            {
                string[] temp    = l1[i].Split(splitter);
                string   holding = "";
                for (int j = 0; j < temp.Length; j++)
                {
                    if (j == 0)
                    {
                        list1.Add(temp[j]);
                    }
                    else
                    {
                        holding += (temp[j] + " ");
                    }
                }
                list2.Add(holding);
            }
            var    a1     = list1.ToArray();
            var    a2     = list2.ToArray();
            RBTree rBTree = new RBTree();

            for (int i = 0; i < a1.Length; i++)
            {
                RBNode node = new RBNode(long.Parse(a1[i]), a2[i]);
                Insert(rBTree, node);
            }
            //l1.Clear();
            return(rBTree);
        }
Ejemplo n.º 6
0
        public void RBInsertFixup(RBTree tree, RBNode node)
        {
            while (node != tree.root && node.parent.isRed == true)
            {
                if (node.parent == node.parent.parent.left)
                {
                    RBNode y = node.parent.parent.right;
                    if (y != null && y.isRed == true)
                    {
                        node.parent.isRed        = false;
                        y.isRed                  = false;
                        node.parent.parent.isRed = true;
                        node = node.parent.parent;
                    }
                    else
                    {
                        if (node == node.parent.right)
                        {
                            node = node.parent;
                            LeftRotate(tree, node);
                        }

                        else
                        {
                            node.parent.isRed        = false;
                            node.parent.parent.isRed = true;
                            RightRotate(tree, node.parent.parent);
                        }
                    }
                }

                else
                {
                    RBNode y = node.parent.parent.left;
                    if (y != null && y.isRed == true)
                    {
                        node.parent.isRed        = false;
                        y.isRed                  = false;
                        node.parent.parent.isRed = true;
                        node = node.parent.parent;
                    }
                    else
                    {
                        if (node == node.parent.left)
                        {
                            node = node.parent;
                            RightRotate(tree, node);
                        }

                        else
                        {
                            node.parent.isRed        = false;
                            node.parent.parent.isRed = true;
                            LeftRotate(tree, node.parent.parent);
                        }
                    }
                }
                //tree.root.isRed = false;
            }
            tree.root.isRed = false;
        }