Created
December 23, 2013 21:25
-
-
Save SergeyTeplyakov/8104967 to your computer and use it in GitHub Desktop.
TreeNode sample from the PPP book by Robert C. Martin
This file contains hidden or 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; | |
public class TreeMap | |
{ | |
private TreeMapNode topNode = null; | |
public void Add(IComparable key, object value) | |
{ | |
if (topNode == null) | |
topNode = new TreeMapNode(key, value); | |
else | |
topNode.Add(key, value); | |
} | |
public object Get(IComparable key) | |
{ | |
return topNode == null ? null : topNode.Find(key); | |
} | |
internal class TreeMapNode | |
{ | |
private static readonly int LESS = 0; | |
private static readonly int GREATER = 1; | |
private IComparable key; | |
private object value; | |
private TreeMapNode[] nodes = new TreeMapNode[2]; | |
public TreeMapNode(IComparable key, object value) | |
{ | |
this.key = key; | |
this.value = value; | |
} | |
public object Find(IComparable key) | |
{ | |
if (key.CompareTo(this.key) == 0) return value; | |
return FindSubNodeForKey(SelectSubNode(key), key); | |
} | |
private int SelectSubNode(IComparable key) | |
{ | |
return (key.CompareTo(this.key) < 0) ? LESS : GREATER; | |
} | |
private object FindSubNodeForKey(int node, IComparable key) | |
{ | |
return nodes[node] == null ? null : nodes[node].Find(key); | |
} | |
public void Add(IComparable key, object value) | |
{ | |
if (key.CompareTo(this.key) == 0) | |
this.value = value; | |
else | |
AddSubNode(SelectSubNode(key), key, value); | |
} | |
private void AddSubNode(int node, IComparable key, object value) | |
{ | |
if (nodes[node] == null) | |
nodes[node] = new TreeMapNode(key, value); | |
else | |
nodes[node].Add(key, value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment