private IEnumerable PreOrderTraverse(NodeTree <TKey, TValue> node) { if (node != null) { yield return(new TreeData <TKey, TValue>(node.Key, node.Value)); foreach (var nodeData in PreOrderTraverse(node.Left)) { yield return(nodeData); } foreach (var nodeData in PreOrderTraverse(node.Right)) { yield return(nodeData); } } }
public void Add(TKey key, TValue value) { if (_root == null) { _root = new NodeTree <TKey, TValue>(key, value); Count++; return; } var current = _root; while (true) { if (key.CompareTo(current.Key) < 0) { if (current.Left == null) { current.Left = new NodeTree <TKey, TValue>(key, value); current.Left.Parent = current; Count++; return; } current = current.Left; } else { if (current.Right == null) { current.Right = new NodeTree <TKey, TValue>(key, value); current.Right.Parent = current; Count++; return; } current = current.Right; } } }
public BinarySearchTree() { _root = null; Count = 0; }