Created
May 18, 2016 19:08
-
-
Save Poplava/1df5380e083f982c8ceab02f410657d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace DataStructuresLab | |
{ | |
class Node<K, V> where K : IComparable where V : IComparable | |
{ | |
private K key; | |
private V data; | |
public Node<K, V> rightNode; | |
public Node<K, V> leftNode; | |
public Node<K, V> parentNode; | |
private Node() | |
{ | |
rightNode = null; | |
leftNode = null; | |
} | |
private Node(Node<K, V> parentNode) : this() | |
{ | |
this.parentNode = parentNode; | |
} | |
public Node(K key, V data, Node<K, V> parentNode) : this(parentNode) | |
{ | |
this.key = key; | |
this.data = data; | |
} | |
public Node(K key, V data) : this(key, data, null) { } | |
public K Key | |
{ | |
get | |
{ | |
return key; | |
} | |
} | |
public V Data | |
{ | |
get | |
{ | |
return data; | |
} | |
} | |
public void Add(K key, V data) | |
{ | |
int compareResult = key.CompareTo(this.key); | |
if (compareResult == 0) | |
{ | |
this.data = data; | |
} | |
else if (compareResult > 0) | |
{ | |
if (rightNode != null) | |
{ | |
rightNode.Add(key, data); | |
} | |
else | |
{ | |
rightNode = new Node<K, V>(key, data, this); | |
} | |
} | |
else | |
{ | |
if (leftNode != null) | |
{ | |
leftNode.Add(key, data); | |
} | |
else | |
{ | |
leftNode = new Node<K, V>(key, data, this); | |
} | |
} | |
} | |
public void Add(Node<K, V> node) | |
{ | |
int compareResult = node.Key.CompareTo(this.key); | |
if (compareResult >= 0) | |
{ | |
if (rightNode != null) | |
{ | |
rightNode.Add(node); | |
} | |
else | |
{ | |
rightNode = node; | |
} | |
} | |
else | |
{ | |
if (leftNode != null) | |
{ | |
leftNode.Add(node); | |
} | |
else | |
{ | |
leftNode = node; | |
} | |
} | |
} | |
public Node<K, V> FindNode(K key) | |
{ | |
int compareResult = key.CompareTo(this.key); | |
if (compareResult == 0) | |
{ | |
return this; | |
} | |
if (compareResult > 0) | |
{ | |
if (rightNode == null) | |
{ | |
return null; | |
} | |
return rightNode.FindNode(key); | |
} | |
if (leftNode == null) | |
{ | |
return null; | |
} | |
return leftNode.FindNode(key); | |
} | |
public V Find(K key) | |
{ | |
Node<K, V> node = FindNode(key); | |
if (node == null) | |
{ | |
return default(V); | |
} | |
return node.Data; | |
} | |
public void Remove() | |
{ | |
if (parentNode.rightNode == this) | |
{ | |
parentNode.rightNode = null; | |
} | |
else | |
{ | |
parentNode.leftNode = null; | |
} | |
if (rightNode != null) | |
{ | |
parentNode.Add(rightNode); | |
} | |
if (leftNode != null) | |
{ | |
parentNode.Add(leftNode); | |
} | |
} | |
} | |
class BinaryTree<K, V> where K : IComparable where V : IComparable | |
{ | |
private Node<K, V> root; | |
public BinaryTree() | |
{ | |
root = null; | |
} | |
public void Add(K key, V data) | |
{ | |
if (root != null) | |
{ | |
root.Add(key, data); | |
} | |
else | |
{ | |
root = new Node<K, V>(key, data); | |
} | |
} | |
public V Find(K key) | |
{ | |
if (root == null) | |
{ | |
return default(V); | |
} | |
return root.Find(key); | |
} | |
public void Remove(K key) | |
{ | |
if (root == null) | |
{ | |
return; | |
} | |
Node<K, V> node = root.FindNode(key); | |
if (node == null) | |
{ | |
return; | |
} | |
node.Remove(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
that waas great!.