public int Next() { TreeAgainNode current = stack.Pop(); FindNext(current.Right); return(current.Data); }
private TreeAgainNode FlipColor(TreeAgainNode node) { node.Color = TreeAgainNodeColor.Red; node.Left.Color = TreeAgainNodeColor.Black; node.Right.Color = TreeAgainNodeColor.Black; return(node); }
private void FindNext(TreeAgainNode node) { if (node == null) { return; } stack.Push(node); FindNext(node.Left); }
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); }
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); }
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; }
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)); }
private TreeAgainNode FlipColor(TreeAgainNode node) { node.Color = TreeAgainNodeColor.Red; node.Left.Color = TreeAgainNodeColor.Black; node.Right.Color = TreeAgainNodeColor.Black; return node; }
public void Add(int x) { root = DoAdd(root, x); }
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; }
public TreeAgainIterator(TreeAgainNode root) { stack = new Stack<TreeAgainNode>(); FindNext(root); }
public TreeAgainIterator(TreeAgainNode root) { stack = new Stack <TreeAgainNode>(); FindNext(root); }
private bool IsRed(TreeAgainNode node) { return(node != null && node.Color == TreeAgainNodeColor.Red); }
private bool IsRed(TreeAgainNode node) { return (node != null && node.Color == TreeAgainNodeColor.Red); }
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); }