Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a generic Binary Search Tree. The only thing I didn't do was implement IEnumerable&lt;T&gt; so you could traverse the tree using a enumerator. However that should be fairly straight forward.</p> <p>Special thanks to Scott Mitchel for his BSTTree article, I used it as a reference on the delete method.</p> <p>The Node Class:</p> <pre><code> class BSTNode&lt;T&gt; where T : IComparable&lt;T&gt; { private BSTNode&lt;T&gt; _left = null; private BSTNode&lt;T&gt; _right = null; private T _value = default(T); public T Value { get { return this._value; } set { this._value = value; } } public BSTNode&lt;T&gt; Left { get { return _left; } set { this._left = value; } } public BSTNode&lt;T&gt; Right { get { return _right; } set { this._right = value; } } } </code></pre> <p>And the actual Tree class:</p> <pre><code> class BinarySearchTree&lt;T&gt; where T : IComparable&lt;T&gt; { private BSTNode&lt;T&gt; _root = null; private int _count = 0; public virtual void Clear() { _root = null; _count = 0; } public virtual int Count { get { return _count; } } public virtual void Add(T value) { BSTNode&lt;T&gt; newNode = new BSTNode&lt;T&gt;(); int compareResult = 0; newNode.Value = value; if (_root == null) { this._count++; _root = newNode; } else { BSTNode&lt;T&gt; current = _root; BSTNode&lt;T&gt; parent = null; while (current != null) { compareResult = current.Value.CompareTo(newNode.Value); if (compareResult &gt; 0) { parent = current; current = current.Left; } else if (compareResult &lt; 0) { parent = current; current = current.Right; } else { // Node already exists throw new ArgumentException("Duplicate nodes are not allowed."); } } this._count++; compareResult = parent.Value.CompareTo(newNode.Value); if (compareResult &gt; 0) { parent.Left = newNode; } else { parent.Right = newNode; } } } public virtual BSTNode&lt;T&gt; FindByValue(T value) { BSTNode&lt;T&gt; current = this._root; if (current == null) return null; // Tree is empty. else { while (current != null) { int result = current.Value.CompareTo(value); if (result == 0) { // Found the corrent Node. return current; } else if (result &gt; 0) { current = current.Left; } else { current = current.Right; } } return null; } } public virtual void Delete(T value) { BSTNode&lt;T&gt; current = this._root; BSTNode&lt;T&gt; parent = null; int result = current.Value.CompareTo(value); while (result != 0 &amp;&amp; current != null) { if (result &gt; 0) { parent = current; current = current.Left; } else if (result &lt; 0) { parent = current; current = current.Right; } result = current.Value.CompareTo(value); } if (current == null) throw new ArgumentException("Cannot find item to delete."); if (current.Right == null) { if (parent == null) this._root = current.Left; else { result = parent.Value.CompareTo(current.Value); if (result &gt; 0) { parent.Left = current.Left; } else if (result &lt; 0) { parent.Right = current.Left; } } } else if (current.Right.Left == null) { if (parent == null) this._root = current.Right; else { result = parent.Value.CompareTo(current.Value); if (result &gt; 0) { parent.Left = current.Right; } else if (result &lt; 0) { parent.Right = current.Right; } } } else { BSTNode&lt;T&gt; furthestLeft = current.Right.Left; BSTNode&lt;T&gt; furthestLeftParent = current.Right; while (furthestLeft.Left != null) { furthestLeftParent = furthestLeft; furthestLeft = furthestLeft.Left; } furthestLeftParent.Left = furthestLeft.Right; furthestLeft.Left = current.Left; furthestLeft.Right = current.Right; if (parent != null) { result = parent.Value.CompareTo(current.Value); if (result &gt; 0) { parent.Left = furthestLeft; } else if (result &lt; 0) { parent.Right = furthestLeft; } } else { this._root = furthestLeft; } } this._count--; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload