Exemple #1
0
        static void Main(string[] args)
        {
            RedBlackNode root = null;

            root = LeftLeaningRBBST.insert(root, 'S', "1");
            root = LeftLeaningRBBST.insert(root, 'E', "2");
            root = LeftLeaningRBBST.insert(root, 'A', "3");
            root = LeftLeaningRBBST.insert(root, 'R', "4");
            root = LeftLeaningRBBST.insert(root, 'C', "5");
            root = LeftLeaningRBBST.insert(root, 'H', "6");
            root = LeftLeaningRBBST.insert(root, 'E', "7");
            root = LeftLeaningRBBST.insert(root, 'X', "8");
            root = LeftLeaningRBBST.insert(root, 'A', "9");
            root = LeftLeaningRBBST.insert(root, 'M', "10");
            root = LeftLeaningRBBST.insert(root, 'P', "11");
            root = LeftLeaningRBBST.insert(root, 'L', "12");
            root = LeftLeaningRBBST.insert(root, 'E', "13");

            Console.WriteLine(IsBST.fnIsBST(root));
            Console.Read();
        }
Exemple #2
0
        public static RedBlackNode insert(RedBlackNode node, char k, string val)
        {
            if (node == null)
            {
                return(new RedBlackNode(k, val, RED));
            }

            int cmp = k.CompareTo(node.Key);

            if (cmp < 0)
            {
                node.Left = insert(node.Left, k, val);
            }
            else if (cmp > 0)
            {
                node.Right = insert(node.Right, k, val);
            }
            else
            {
                node.Value = val;
            }

            if (!isRedNode(node.Left) && isRedNode(node.Right))
            {
                node = LeftRotate(node);
            }

            if (isRedNode(node.Left) && isRedNode(node.Left.Left))
            {
                node = RightRotate(node);
            }

            if (isRedNode(node.Left) && isRedNode(node.Right))
            {
                FlipColors(node);
            }

            return(node);
        }
Exemple #3
0
        public static string search(char k, RedBlackNode root)
        {
            RedBlackNode node = root;

            while (node != null)
            {
                int cmp = k.CompareTo(node.Key);
                if (k < 0)
                {
                    node = node.Left;
                }
                else if (k > 0)
                {
                    node = node.Right;
                }
                else
                {
                    return(node.Value);
                }
            }

            return(null);
        }
Exemple #4
0
 private static void FlipColors(RedBlackNode h)
 {
     h.Left.Color  = BLACK;
     h.Right.Color = BLACK;
     h.Color       = RED;
 }