protected override bool MoveNextInternal() { if (SentinelEx.NotEqualNull(_current.RightChild)) { _stack.Push(_current); _current = _current.RightChild as TNode; while (SentinelEx.NotEqualNull(_current.LeftChild)) { _stack.Push(_current); _current = _current.LeftChild as TNode; } return(true); } else { while (_stack.Count > 0 && _stack.Peek().RightChild == _current) { _current = _stack.Pop(); //向上找到右行点 } if (_stack.Count == 0) { return(false); } _current = _stack.Pop(); return(true); } }
public static int GetDegree(this IBinaryTreeNode node) { Contract.Requires <ArgumentNullException>(node != null); Contract.Ensures(Contract.Result <int>() >= 0 && Contract.Result <int>() <= 2); if (SentinelEx.NotEqualNull(node.LeftChild)) { if (SentinelEx.NotEqualNull(node.RightChild)) { return(2); } else { return(1); } } else if (SentinelEx.NotEqualNull(node.RightChild)) { return(1); } else { return(0); } }
private static IEnumerable <Tuple <IMultiwayTreeNode, TLevel> > LevelOrderTraverse <TLevel>( IMultiwayTreeNode partialroot, TLevel seed, Func <TLevel, IMultiwayTreeNode, int, TLevel> levelfunc) { var queue = new Queue <Tuple <IMultiwayTreeNode, TLevel> >(); queue.Enqueue(new Tuple <IMultiwayTreeNode, TLevel>(partialroot, seed)); int index; while (queue.Count > 0) { index = 0; var current = queue.Dequeue(); if (SentinelEx.NotEqualNull(current.Item1.Children)) { foreach (var node in current.Item1.Children) { queue.Enqueue(new Tuple <IMultiwayTreeNode, TLevel>(node, levelfunc == null ? current.Item2 : levelfunc(current.Item2, partialroot, index++))); } } yield return(current); } }
protected override void InitPosition() { _current = _root; while (SentinelEx.NotEqualNull(_current.LeftChild)) { _stack.Push(_current); _current = _current.LeftChild as TNode; } }
internal RecursiveEnumerator(TNode root) { if (SentinelEx.NotEqualNull(root)) { _root = root; _version = root.Version; _stack = new Stack <TNode>(); } else { _null = true; } }
public static bool IsAncestorOf(this BinaryTreeNode node, BinaryTreeNode target) { Contract.Requires <ArgumentNullException>(node != null); Contract.Requires <ArgumentNullException>(target != null); var p = target.Parent; while (SentinelEx.NotEqualNull(p)) { if (p == node) { return(true); } p = p.Parent; } return(false); }
protected override bool MoveNextInternal() { if (SentinelEx.NotEqualNull(_current.RightChild)) { _stack.Push(_current.RightChild as TNode); } if (SentinelEx.NotEqualNull(_current.LeftChild)) { _current = _current.LeftChild as TNode; return(true); } if (_stack.Count == 0) { return(false); } _current = _stack.Pop(); return(true); }
public bool MoveNext() { if (_version != _root.Version) { throw new InvalidOperationException("在枚举过程中树被修改过"); } if (_queue.Count == 0) { return(false); } _current = _queue.Dequeue(); if (SentinelEx.NotEqualNull(_current.LeftChild)) { _queue.Enqueue(_current.LeftChild as TNode); } if (SentinelEx.NotEqualNull(_current.RightChild)) { _queue.Enqueue(_current.RightChild as TNode); } return(true); }
protected override bool MoveNextInternal() { if (_stack.Count == 0) { return(false); } if (_stack.Peek().LeftChild != _current && _stack.Peek().RightChild != _current) { TNode temp = _stack.Peek(); while (SentinelEx.NotEqualNull(temp)) { if (SentinelEx.NotEqualNull(temp.LeftChild)) { if (SentinelEx.NotEqualNull(temp.RightChild)) { _stack.Push(temp.RightChild as TNode); } _stack.Push(temp.LeftChild as TNode); } else { if (SentinelEx.NotEqualNull(temp.RightChild)) { _stack.Push(temp.RightChild as TNode); } else { break; } } temp = _stack.Peek(); } } _current = _stack.Pop(); return(true); }
public static int GetDegree(this IMultiwayTreeNode node) { Contract.Requires <ArgumentNullException>(node == null); Contract.Ensures(Contract.Result <int>() >= 0); return(node.Children?.Count(cnode => SentinelEx.NotEqualNull(cnode)) ?? 0); }