예제 #1
0
        public int Next()
        {
            TreeAgainNode current = stack.Pop();

            FindNext(current.Right);
            return(current.Data);
        }
예제 #2
0
        private TreeAgainNode FlipColor(TreeAgainNode node)
        {
            node.Color       = TreeAgainNodeColor.Red;
            node.Left.Color  = TreeAgainNodeColor.Black;
            node.Right.Color = TreeAgainNodeColor.Black;

            return(node);
        }
예제 #3
0
        private void FindNext(TreeAgainNode node)
        {
            if (node == null)
            {
                return;
            }

            stack.Push(node);
            FindNext(node.Left);
        }
예제 #4
0
        private TreeAgainNode RotateRight(TreeAgainNode node)
        {
            TreeAgainNode old = node;

            node       = node.Left;
            node.Color = old.Color;
            old.Left   = node.Right;
            node.Right = old;
            old.Color  = TreeAgainNodeColor.Red;

            return(node);
        }
예제 #5
0
        private TreeAgainNode CheckBalance(TreeAgainNode node)
        {
            if (IsRed(node.Right) && !IsRed(node.Left))
            {
                node = RotateLeft(node);
            }

            if (IsRed(node.Left) && IsRed(node.Left.Left))
            {
                node = RotateRight(node);
            }

            if (IsRed(node.Left) && IsRed(node.Right))
            {
                node = FlipColor(node);
            }

            return(node);
        }
예제 #6
0
        private TreeAgainNode CheckBalance(TreeAgainNode node)
        {
            if (IsRed(node.Right) && !IsRed(node.Left))
            {
                node = RotateLeft(node);
            }

            if (IsRed(node.Left) && IsRed(node.Left.Left))
            {
                node = RotateRight(node);
            }

            if (IsRed(node.Left) && IsRed(node.Right))
            {
                node = FlipColor(node);
            }

            return node;
        }
예제 #7
0
        private TreeAgainNode DoAdd(TreeAgainNode node, int x)
        {
            if (node == null)
            {
                node = new TreeAgainNode(x);
                return(node);
            }

            if (x < node.Data)
            {
                node.Left = DoAdd(node.Left, x);
            }
            else
            {
                node.Right = DoAdd(node.Right, x);
            }

            return(CheckBalance(node));
        }
예제 #8
0
        private TreeAgainNode FlipColor(TreeAgainNode node)
        {
            node.Color = TreeAgainNodeColor.Red;
            node.Left.Color = TreeAgainNodeColor.Black;
            node.Right.Color = TreeAgainNodeColor.Black;

            return node;
        }
예제 #9
0
 public void Add(int x)
 {
     root = DoAdd(root, x);
 }
예제 #10
0
        private TreeAgainNode RotateRight(TreeAgainNode node)
        {
            TreeAgainNode old = node;
            node = node.Left;
            node.Color = old.Color;
            old.Left = node.Right;
            node.Right = old;
            old.Color = TreeAgainNodeColor.Red;

            return node;
        }
예제 #11
0
 public TreeAgainIterator(TreeAgainNode root)
 {
     stack = new Stack<TreeAgainNode>();
     FindNext(root);
 }
예제 #12
0
 public TreeAgainIterator(TreeAgainNode root)
 {
     stack = new Stack <TreeAgainNode>();
     FindNext(root);
 }
예제 #13
0
 private bool IsRed(TreeAgainNode node)
 {
     return(node != null && node.Color == TreeAgainNodeColor.Red);
 }
예제 #14
0
        private void FindNext(TreeAgainNode node)
        {
            if (node == null)
            {
                return;
            }

            stack.Push(node);
            FindNext(node.Left);
        }
예제 #15
0
 private bool IsRed(TreeAgainNode node)
 {
     return (node != null && node.Color == TreeAgainNodeColor.Red);
 }
예제 #16
0
        private TreeAgainNode DoAdd(TreeAgainNode node, int x)
        {
            if (node == null)
            {
                node = new TreeAgainNode(x);
                return node;
            }

            if (x < node.Data)
            {
                node.Left = DoAdd(node.Left, x);
            }
            else
            {
                node.Right = DoAdd(node.Right, x);
            }

            return CheckBalance(node);
        }
예제 #17
0
 public void Add(int x)
 {
     root = DoAdd(root, x);
 }