Skip to content

Instantly share code, notes, and snippets.

@SergeyTeplyakov
Created December 23, 2013 21:25
Show Gist options
  • Save SergeyTeplyakov/8104967 to your computer and use it in GitHub Desktop.
Save SergeyTeplyakov/8104967 to your computer and use it in GitHub Desktop.
TreeNode sample from the PPP book by Robert C. Martin
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);
}
}
}
@W1zarDddD
Copy link

W1zarDddD commented Mar 2, 2024

How do you like the book? I'm thinking whether to buy it or not. I can’t make up my mind. I plan to ask write my essay for me for the university, I use https://ca.edubirdie.com/write-my-essay-for-me so that I have time to read. I just want to choose the right book that will really help me in programming. Now I’m studying to become whatever I want to become in the future. This situation has probably happened to many people in their lives. I know that this book offers a deep understanding of the basic principles of programming and teaches how to apply them in practice. But I don’t know if it’s suitable for me for initial development in this direction!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment