Why is my if statement in my contains method unreachable? [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
When I run my program my 'if' statement is compiled as unreachable code which in turn, causes my contains method to continuously print false even though the number exists within the tree. I can't figure out why could any one help me please? I'm fairly new to programming.
Here is my node class.
class Node<T> where T : IComparable
{
private T data;
private int balanceFactor = 0; //added for AVLTree
public Node<T> Left, Right;
public Node(T item)
{
data = item;
Left = null;
Right = null;
}
public T Data
{
set { data = value; }
get { return data; }
}
}
Here is my binary search tree class.
class BSTree<T> : BinTree<T> where T : IComparable
{ //root declared as protected in Parent Class – Binary Tree
public BSTree()
{
root = null;
}
public void InsertItem(T item)
{
insertItem(item, ref root);
}
private void insertItem(T item, ref Node<T> tree)
{
if (tree == null)
tree = new Node<T>(item);
else if (item.CompareTo(tree.Data) < 0)
insertItem(item, ref tree.Left);
else if (item.CompareTo(tree.Data) > 0)
insertItem(item, ref tree.Right);
}
public int Height()
//Return the max level of the tree
{
return Height(root);
}
protected int Height(Node<T> current)
{
if (current == null) return 0;
return 1 + Math.Max(Height(current.Left), Height(current.Right));
}
public int Count()
//Return the number of nodes in the tree
{
return Height(root);
}
public int Count(ref Node<T> current)
//Return the number of nodes in the tree
{
int counter = 0;
if (current == null)
{
return 0;
}
else if (current.Left != null)
{
counter += Count(ref current.Left);
counter++;
}
if (current.Right != null)
{
counter += Count(ref current.Right);
counter++;
}
return counter;
}
public Boolean Contains(T item)
//Return true if the item is contained in the BSTree, false //otherwise.
{
Node<T> current = root;
if (current.Data.CompareTo(item) == 0)
{
{
return true;
}
if (current.Data.CompareTo(item) > 0)
{
current = current.Left;
}
if (current.Data.CompareTo(item) < 0)
{
current = current.Right;
}
}
return false;
}
private T leastItem(Node<T> tree)
{
if (tree.Left == null)
return tree.Data;
else
return leastItem(tree.Left);
}
}
Lastly my main class.
class Program
{
static void Main(string[] args)
{
BSTree<int> treeBranch = new BSTree<int>();
treeBranch.InsertItem(77);
treeBranch.InsertItem(20);
treeBranch.InsertItem(37);
treeBranch.InsertItem(15);
treeBranch.InsertItem(22);
treeBranch.InsertItem(30);
Console.WriteLine(treeBranch.Count());
Console.WriteLine(treeBranch.Height());
Console.WriteLine(treeBranch.Contains(15));
string InOrder = "in order :";
treeBranch.InOrder(ref InOrder);
Console.WriteLine(InOrder);
Console.WriteLine("\n");
Console.ReadKey();
}
}

if (current.Data.CompareTo(item) == 0)
{
{ <==== here
return true;
}
if (current.Data.CompareTo(item) > 0)
{
current = current.Left;
}
if (current.Data.CompareTo(item) < 0)
{
current = current.Right;
}
}
There's no condition to return true;. Change your code to this:
if (current.Data.CompareTo(item) == 0)
{
return true;
}
else if (current.Data.CompareTo(item) > 0)
{
current = current.Left;
}
else if (current.Data.CompareTo(item) < 0)
{
current = current.Right;
}

To many { and } in this code
if (current.Data.CompareTo(item) == 0)
{
{
return true;
}

Related

Remove function of BST in C#

I am trying to write the remove function for BST in C#. I have some NUnit tests that need to pass. All of them pass except two. I have two scenarios for which the test fails.
I need to remove 10 for these two trees. But it's failing:
100, 10, 5
100, 10, 20
So I'm guessing my code doesn't splice the 10 out. And the error is in the RemoveNonRootNode method. And specifically with the "else if" statements that are looking at 1 child situation.
Here is my code. I appreciate some help!
public class BST
{
private BSTNode m_top;
private class BSTNode
{
private int m_data;
private BSTNode m_left;
private BSTNode m_right;
public int Data
{
get { return m_data; }
set { m_data = value; }
}
public BSTNode Left
{
get { return m_left; }
set { m_left = value; }
}
public BSTNode Right
{
get { return m_right; }
set { m_right = value; }
}
public BSTNode(int data)
{
m_data = data;
}
}
public void AddValue(int v)
{
if (m_top == null)
{
m_top = new BSTNode(v);
}
else
{
BSTNode cur = m_top;
while (true)
{
if (v < cur.Data)
{
if (cur.Left == null)
{
cur.Left = new BSTNode(v);
return;
}
else
cur = cur.Left;
}
else if (v > cur.Data)
{
if (cur.Right == null)
{
cur.Right = new BSTNode(v);
return;
}
else
cur = cur.Right;
}
else
throw new DuplicateValueException("Value " + v + " is already in the tree!");
}
}
}
public void Print()
{
Console.WriteLine("=== Printing the tree ===");
if (m_top == null)
Console.WriteLine("Empty tree!");
else
PrintInternal(m_top);
}
private void PrintInternal(BSTNode cur)
{
if (cur.Left != null)
PrintInternal(cur.Left);
Console.WriteLine(cur.Data);
if (cur.Right != null)
PrintInternal(cur.Right);
}
public bool Find(int target)
{
return FindNode(target) != null;
}
private BSTNode FindNode(int target)
{
BSTNode cur = m_top;
while (cur != null)
{
if (cur.Data == target)
return cur;
else if (target < cur.Data)
cur = cur.Left;
else if (target > cur.Data)
cur = cur.Right;
}
return null;
}
public void Remove(int target)
{
// if the tree is empty:
if (m_top == null)
return;
if (m_top.Data == target)
{
RemoveRootNode(target);
}
else
{
RemoveNonrootNode(target);
}
}
private void RemoveNonrootNode(int target)
{
BSTNode cur = m_top;
BSTNode parent = null;
//First, find the target node that we need to remove
// we'll have the 'parent' reference trail the cur pointer down the tree
// so when we stop, cur is the node to remove, and parent is one above it.
while (cur!= null && cur.Data != target)
{
parent = cur;
if (target > cur.Data)
cur = cur.Right;
else
cur = cur.Left;
}
// Next, we figure out which of the cases we're in
// Case 1: The target node has no children
if (cur.Left == null && cur.Right == null)
{
if (parent.Left==cur)
parent.Left = null;
else
parent.Right = null;
}
// Case 2: The target node has 1 child
// (You may want to split out the left vs. right child thing)
else if (cur.Left == null)
{
BSTNode cur2 = cur;
cur = cur.Right;
cur2 = null;
return;
}
else if (cur.Right == null)
{
BSTNode cur2 = cur;
cur = cur.Right;
cur2 = null;
return;
}
// Case 3: The target node has 2 children
BSTNode removee = FindAndRemoveNextSmallerValue(target, cur);
cur.Data = removee.Data;
return;
}
private void RemoveRootNode(int target)
{
// If we're here, it's because we're removing the top-most node (the 'root' node)
// Case 1: Root has no children
if (m_top.Left == null && m_top.Right == null)
{
m_top = null; // Therefore, the tree is now empty
return;
}
// Case 2: Root has 1 child
else if (m_top.Left == null)
{
// 1 (right) child, OR zero children (right may also be null)
m_top = m_top.Right; // Right is null or another node - either way is correct
return;
}
else if (m_top.Right == null)
{
// If we're here, Left is not null, so there MUST be one (Left) Child
m_top = m_top.Left;
return;
}
// Case 3: Root has two children - this is where it gets interesting :)
else
{
// 2 children - find (and remove) next smaller value
// use that data to overwrite the current data.
BSTNode removee = FindAndRemoveNextSmallerValue(target, m_top);
m_top.Data = removee.Data;
return;
}
}
/// <summary>
/// This method takes 1 step to the left, then walks as far to the right
/// as possible. Once that right-most node is found, it's removed & returned.
/// Note that the node MAY be immediately to the left of the <B>startHere</B>
/// parameter, if startHere.Left.Right == null
/// </summary>
/// <param name="smallerThanThis"></param>
/// <param name="startHere"></param>
/// <returns></returns>
private BSTNode FindAndRemoveNextSmallerValue(int smallerThanThis, BSTNode startHere)
{
BSTNode parent = startHere;
BSTNode child = startHere.Left;
if (child.Data == smallerThanThis)
{
child = null;
}
while (child.Right != null)
{
parent = child;
child = child.Right;
}
parent = child;
child = null;
return parent;
}
// Given the value of a node, find (and remove) the predessor node in the tree
// returns the value of the predecessor node, or Int32.MinValue if no such value was found
public int TestFindAndRemoveNextSmallest(int sourceNode)
{
BSTNode startAt = this.FindNode(sourceNode);
// sourceNode should == startAt.Data, unless startAt is null)
BSTNode removed = FindAndRemoveNextSmallerValue(sourceNode, startAt);
if (removed != null)
return removed.Data;
else
return Int32.MinValue;
}
}
At first sight, there seems to be this bug:
else if (cur.Left == null)
{
BSTNode cur2 = cur;
cur = cur.Right;
cur2 = null;
return;
}
else if (cur.Right == null)
{
BSTNode cur2 = cur;
// cur = cur.Right; // THIS SHOULD BE .Left, because .Right is NULL
cur = cur.Left; // THIS IS THE FIX
cur2 = null;
return;
}
But your actual problem is; updating the cur reference to something else does not change the pointer to the same object (cur) held by its parent. Actually, you did it right in Case 1, but somehow missed it in Case 2. Therefore; the full fix is: (only fixing the failing test. Promising no more).
// Case 2: The target node has 1 child
// (You may want to split out the left vs. right child thing)
else if (cur.Left == null)
{
if (parent.Left == cur)
{
parent.Left = cur.Right;
}
else
{
parent.Right = cur.Right;
}
return;
}
else if (cur.Right == null)
{
if(parent.Left == cur)
{
parent.Left = cur.Left;
}
else
{
parent.Right = cur.Left;
}
return;
}

C# - Binary Search Tree Contains/Is Present Method

I'm really struggling to get this method to work, I was wondering if you could help me out. I've been using the ref keyword, so I'll keep using it. I've been searching the web and it's been some help, but I've tried everything I can possibly think of. Both my count and height methods work, however, I'm just really struggling to get this Contain method to work. Many examples on the web have showed both a public and private method of contains (I understand as to why) but I'm sure it could be done within one method? Surely, right? Also, please ignore the RemoveItem method, unless you wish to give me a head-start, that's at your discretion. I know it's tricky as I've looked up on it earlier during the week.
Node class-
class Node<T> where T : IComparable
{
private T data;
public Node<T> Left, Right;
public Node(T item)
{
data = item;
Left = null;
Right = null;
}
public T Data
{
set { data = value; }
get { return data; }
}
}
BinTree Class-
class BinTree<T> where T : IComparable
{
protected Node<T> root;
public BinTree() //creates an empty tree
{
root = null;
}
public BinTree(Node<T> node) //creates a tree with node as the root
{
root = node;
}
//I've deleted my preOrder, inOrder and postOrder methods just to save you some time
}
BSTree Class-
class BSTree<T> : BinTree<T> where T : IComparable
{
public BSTree()
{
root = null;
}
private void insertItem(T item, ref Node<T> tree)
{
if (tree == null)
{
tree = new Node<T>(item);
}
else if (item.CompareTo(tree.Data) < 0)
{
insertItem(item, ref tree.Left);
}
else if (item.CompareTo(tree.Data) > 0)
insertItem(item, ref tree.Right);
}
public void InsertItem(T item)
{
insertItem(item, ref root);
}
public int Height(ref Node<T> tree)
//Return the max level of the tree
{
if (tree == null)
return 0;
return (1 + Math.Max(Height(ref tree.Left), Height(ref tree.Right)));
}
public int Count(ref Node<T> tree)
//Return the number of nodes in the tree
{
int counter = 0;
if (tree == null)
{
return 0;
}
else if (tree.Left != null)
{
counter += Count(ref tree.Left);
counter++;
}
if (tree.Right != null)
{
counter += Count(ref tree.Right);
counter++;
}
return counter;
}
public Boolean Contains(T item, ref Node<T> tree)
//Return true if the item is contained in the BSTree, false //otherwise.
{
if (tree == null)
{
return false;
}
if (item.CompareTo(tree.Data) < 0)
{
return Contains(ref tree.Left);
if (item.CompareTo(tree.Data) > 0)
{
return Contains(ref tree.Right);
return true;
}
}
}
public void RemoveItem(T item)
{
}
}
Thank you in advance.
To check if a node is in the tree, you have a few options:
The node you want is in the left sub-tree
The node you want is in the right sub-tree
The node you want is where you are right now
So your Contains method should look more like this:
public Boolean Contains(T item, ref Node<T> tree)
{
if (tree == null)
{
return false;
}
if (tree.data == item)
{
return true;
}
if (item.CompareTo(tree.Data) < 0)
{
return Contains(item, ref tree.Left);
}
if (item.CompareTo(tree.Data) > 0)
{
return Contains(item, ref tree.Right);
}
}
public static bool Contains(Node root, int value)
{
if(root == null)
return false;
if(root.Value == value)
return true;
if(value < root.Value)
return Contains( root.Left,value);
else
return Contains( root.Right,value);
}

"Sorting" a Tree Data Structure

I made a Tree Data Structure and I want the Elements to sort like this:
10
/ \
5 12
/ \ / \
3 7 11 18
If the value of the added element is smaller than the value of the other element, it should be linked left, and if bigger, right. My problem is, that I just can't get the sorting method right.
class Tree
{
private class TElement
{
public int _value;
public TElement _left;
public TElement _right;
public TElement(int value)
{
_value = value;
}
}
private TElement RootElement;
public void Add(int value)
{
TElement newElement = new TElement(value);
TElement current = new TElement(value);
current = RootElement;
if (RootElement == null)
{
RootElement = newElement;
return;
}
SortNewElement(RootElement, RootElement, newElement, RootElement);
}
private void SortNewElement(TElement left, TElement right, TElement newElement, TElement RootElement)
{
if (newElement._value < RootElement._value && RootElement._left == null)
{
left._left = newElement;
return;
}
if (newElement._value > RootElement._value && RootElement._right == null)
{
right._right = newElement;
return;
}
if (newElement._value < left._value && left._left == null)
{
left._left = newElement;
return;
}
if (newElement._value > right._value && right._right == null)
{
right._right = newElement;
return;
}
SortNewElement(left._left, right._right, newElement, RootElement);
}
}
I know it doesn't work because it's trying to get the linked nodes of a null element.
From what i can understand from your question you are just trying to insert a new node in a binary search tree. Its inorder traversal will be a sorted array.
You can do so by the following simple pseudo code
insert_new( Node* node, value)
{
if(value > node->value)
{
if(node->right != null)
{
insert_new(node->right,value);
}
else
{
node->right = new Node(value);
return;
}
}
else
{
if(node->left != null)
{
insert_new(node->left,value)
}
else
{
node->left = new Node(value);
return;
}
}
}
class element{
public:
int value;
*element left;
*element right;
element(int value)
value = value;
public add(&element a)
if (a != null)
{
if (left!=null){
left = a;
}
else{
if (left.value > a.value){
right = left;
left= a;
}
else{
right=a;
}
}

Binary Search Tree in C# Implementation

class Node
{
public int data;
public Node left, right;
public Node(int data)
{
this.data = data;
left = null;
right = null;
}
}
class BinaryTreeImp
{
Node root;
static int count = 0;
public BinaryTreeImp()
{
root = null;
}
public Node addNode(int data)
{
Node newNode = new Node(data);
if (root == null)
{
root = newNode;
}
count++;
return newNode;
}
public void insertNode(Node root,Node newNode )
{
Node temp;
temp = root;
if (newNode.data < temp.data)
{
if (temp.left == null)
{
temp.left = newNode;
}
else
{
temp = temp.left;
insertNode(temp,newNode);
}
}
else if (newNode.data > temp.data)
{
if (temp.right == null)
{
temp.right = newNode;
}
else
{
temp = temp.right;
insertNode(temp,newNode);
}
}
}
public void displayTree(Node root)
{
Node temp;
temp = root;
if (temp == null)
return;
displayTree(temp.left);
System.Console.Write(temp.data + " ");
displayTree(temp.right);
}
static void Main(string[] args)
{
BinaryTreeImp btObj = new BinaryTreeImp();
Node iniRoot= btObj.addNode(5);
btObj.insertNode(btObj.root,iniRoot);
btObj.insertNode(btObj.root,btObj.addNode(6));
btObj.insertNode(btObj.root,btObj.addNode(10));
btObj.insertNode(btObj.root,btObj.addNode(2));
btObj.insertNode(btObj.root,btObj.addNode(3));
btObj.displayTree(btObj.root);
System.Console.WriteLine("The sum of nodes are " + count);
Console.ReadLine();
}
}
This is the code for implementation.The code works fine but if in the displayTree function , i replace it with
public void displayTree(Node root)
{
Node temp;
temp = root;
while(temp!=null)
{
displayTree(temp.left);
System.Console.Write(temp.data + " ");
displayTree(temp.right);
}
}
an infinite loop is caused. I don't understand what is causing this.Also i would like to know if there is a better way of implementing a BST in C#.
I'm not sure why you need this loop, but answering your question:
while(temp!=null)
{
displayTree(temp.left);
System.Console.Write(temp.data + " ");
displayTree(temp.right);
}
this code checks if temp is not null, but it will never become null, cause inside the loop you act only on the leafs of the temp. That's why you have an infinit loop.
You don't need a while loop nor a temp variable, let recursion do the work for you:
public void displayTree(Node root)
{
if(root == null) return;
displayTree(root.left);
System.Console.Write(root.data + " ");
displayTree(root.right);
}
temp is set to root at the beginning, and after that its value never changes
what about rewriting your function as
public void displayTree(Node root)
{
if (root == null)
return;
displayTree(root.left);
Console.Write(...);
displayTree(root.right);
}
try this
public void displayTree(Node root)
{
Node temp;
temp = root;
if (temp != null)
{
displayTree(temp.left);
Console.WriteLine(temp.data + " ");
displayTree(temp.right);
}
}
I was just thinking that you as well also could use recursion for the add function. It could look something like this
private void Add(BinaryTree node, ref BinaryTree rootNode)
{
if (rootNode == null)
{
rootNode = node;
}
if (node.value > rootNode.value)
{
Add(node, ref rootNode.right);
}
if (node.value < rootNode.value)
{
Add(node, ref rootNode.left);
}
}
See https://msdn.microsoft.com/en-us/library/ms379572%28v=vs.80%29.aspx.
See the example code in the section "Traversing the Nodes of a BST"
Also... don't forget to check out SortedDictionary, etc. They may have the BST that you need all ready to go! https://msdn.microsoft.com/en-us/library/f7fta44c.aspx
Complete Binary Search Tree ... With Code to check whether Tree is balanced or not
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace BinarySearchTree
{
public class Node
{
public Node(int iData)
{
data = iData;
leftNode = null;
rightNode= null;
}
public int data{get; set;}
public Node leftNode{get; set;}
public Node rightNode{get; set;}
};
public class Program
{
public static Node root = null;
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine("Hello, world!");
root = new Node(20);
InsertNode(root, new Node(10));
InsertNode(root, new Node(15));
InsertNode(root, new Node(13));
InsertNode(root, new Node(11));
InsertNode(root, new Node(12));
InsertNode(root, new Node(25));
InsertNode(root, new Node(22));
InsertNode(root, new Node(23));
InsertNode(root, new Node(27));
InsertNode(root, new Node(26));
if(CheckIfTreeIsBalanced(root))
{
Console.WriteLine("Tree is Balanced!");
}
else
{
Console.WriteLine("Tree is Not Balanced!");
}
PrintTree(root);
}
public static void PrintTree(Node root)
{
if(root == null) return;
Node temp = root;
PrintTree(temp.leftNode);
System.Console.Write(temp.data + " ");
PrintTree(temp.rightNode);
}
public static bool CheckIfTreeIsBalanced(Node root)
{
if(root != null)
{
if(root.leftNode != null && root.rightNode!= null)
{
if(root.leftNode.data < root.data && root.rightNode.data > root.data)
{
return CheckIfTreeIsBalanced(root.leftNode)&&CheckIfTreeIsBalanced(root.rightNode);
}
else
{
return false;
}
}
else if(root.leftNode != null)
{
if(root.leftNode.data < root.data)
{
return CheckIfTreeIsBalanced(root.leftNode);
}
else
{
return false;
}
}
else if(root.rightNode != null)
{
if(root.rightNode.data > root.data)
{
return CheckIfTreeIsBalanced(root.rightNode);
}
else
{
return false;
}
}
}
return true;
}
public static void InsertNode(Node root, Node newNode )
{
Node temp;
temp = root;
if (newNode.data < temp.data)
{
if (temp.leftNode == null)
{
temp.leftNode = newNode;
}
else
{
temp = temp.leftNode;
InsertNode(temp,newNode);
}
}
else if (newNode.data > temp.data)
{
if (temp.rightNode == null)
{
temp.rightNode = newNode;
}
else
{
temp = temp.rightNode;
InsertNode(temp,newNode);
}
}
}
}
}
Output :
Hello, world!
Tree is Balanced!
10 11 12 13 15 20 22 23 25 26 27

Fundamental Data Structures in C#

I would like to know how people implement the following data structures in C# without using the base class library implementations:-
Linked List
Hash Table
Binary Search Tree
Red-Black Tree
B-Tree
Binomial Heap
Fibonacci Heap
and any other fundamental data structures people can think of!
I am curious as I want to improve my understanding of these data structures and it'd be nice to see C# versions rather than the typical C examples out there on the internet!
There's a series of MSDN articles on this subject. However, I haven't really read the text myself. I believe that the collections framework by .NET has a broken interface and cannot be extended very well.
There's also C5, a libray that I am investigating right now.
For the reason mentioned above, I've had the project to implement my own collections library for .NET but I've stopped this project after the first benchmark revealed that even a straightforward, non-thread-safe generic Vector implementation is slower than the native List<T>. Since I've taken care not to produce any inefficient IL code, this means that .NET is simply not suited (yet) for writing on-par replacements for intrinsic data structures, and that the .NET framework has to use some behind-the-scenes knowledge to optimize the builtin collection classes.
Here is a generic Binary Search Tree. The only thing I didn't do was implement IEnumerable<T> so you could traverse the tree using a enumerator. However that should be fairly straight forward.
Special thanks to Scott Mitchel for his BSTTree article, I used it as a reference on the delete method.
The Node Class:
class BSTNode<T> where T : IComparable<T>
{
private BSTNode<T> _left = null;
private BSTNode<T> _right = null;
private T _value = default(T);
public T Value
{
get { return this._value; }
set { this._value = value; }
}
public BSTNode<T> Left
{
get { return _left; }
set { this._left = value; }
}
public BSTNode<T> Right
{
get { return _right; }
set { this._right = value; }
}
}
And the actual Tree class:
class BinarySearchTree<T> where T : IComparable<T>
{
private BSTNode<T> _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<T> newNode = new BSTNode<T>();
int compareResult = 0;
newNode.Value = value;
if (_root == null)
{
this._count++;
_root = newNode;
}
else
{
BSTNode<T> current = _root;
BSTNode<T> parent = null;
while (current != null)
{
compareResult = current.Value.CompareTo(newNode.Value);
if (compareResult > 0)
{
parent = current;
current = current.Left;
}
else if (compareResult < 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 > 0)
{
parent.Left = newNode;
}
else
{
parent.Right = newNode;
}
}
}
public virtual BSTNode<T> FindByValue(T value)
{
BSTNode<T> 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 > 0)
{
current = current.Left;
}
else
{
current = current.Right;
}
}
return null;
}
}
public virtual void Delete(T value)
{
BSTNode<T> current = this._root;
BSTNode<T> parent = null;
int result = current.Value.CompareTo(value);
while (result != 0 && current != null)
{
if (result > 0)
{
parent = current;
current = current.Left;
}
else if (result < 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 > 0)
{
parent.Left = current.Left;
}
else if (result < 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 > 0)
{
parent.Left = current.Right;
}
else if (result < 0)
{
parent.Right = current.Right;
}
}
}
else
{
BSTNode<T> furthestLeft = current.Right.Left;
BSTNode<T> 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 > 0)
{
parent.Left = furthestLeft;
}
else if (result < 0)
{
parent.Right = furthestLeft;
}
}
else
{
this._root = furthestLeft;
}
}
this._count--;
}
}
}
I would recommend two resources for the data structures you mention:
First, there is the .NET Framework Source Code (information can be found on ScottGu's blog here).
Another useful resource is the Wintellect's Power Collections found on Codeplex here.
Hope this helps!
NGenerics
"A class library providing generic data structures and algorithms not implemented in the standard .NET framework."
Check out Rotor 2 or use reflector too see how Microsoft did it!!!
also you can check Microsoft reference source

Categories