public class DoublyNode<T>
{
private T value;
private DoublyNode<T> previous;
private DoublyNode<T> next;
public T Value
{
get { return value; }
set { this.value = value; }
}
public DoublyNode<T> Previous
{
get { return previous; }
set { this.previous = value; }
}
public DoublyNode<T> Next
{
get { return next; }
set { this.next = value; }
}
public DoublyNode(T value)
{
this.value = value;
}
}
public class DoublyLinkedList<T>
{
private DoublyNode<T> head;
private DoublyNode<T> tail;
public DoublyLinkedList()
{
this.head = null;
this.tail = null;
}
public DoublyNode<T> Head
{
get { return head; }
}
public DoublyNode<T> Tail
{
get { return tail; }
}
public DoublyNode<T> Search(T value)
{
DoublyNode<T> cursor = head;
while (cursor != null && !EqualityComparer<T>.Default.Equals(cursor.Value, value))
{
cursor = cursor.Next;
}
return cursor;
}
public void InsertStart(T value)
{
DoublyNode<T> node = new DoublyNode<T>(value);
if (head != null)
{
node.Next = head;
head.Previous = node;
head = node;
}
else
{
tail = node;
head = node;
return;
}
}
public void InsertEnd(T value)
{
DoublyNode<T> node = new DoublyNode<T>(value);
if (tail != null)
{
node.Previous = tail;
tail.Next = node;
tail = node;
}
else
{
tail = node;
head = node;
return;
}
}
public void DeleteFirst()
{
if (head != null)
{
head = head.Next;
head.Previous = null;
}
}
public void DeleteEnd()
{
if (tail != null)
{
tail = tail.Previous;
tail.Next = null;
}
}
public bool InsertAfter(T insert, T value)
{
if (insert == null || value == null)
return false;
DoublyNode<T> node = Search(insert);
if (node == null) //didn't find exisinting node
return false;
DoublyNode<T> newNode = new DoublyNode<T>(value);
newNode.Previous = node;
if (node.Next != null)
node.Next.Previous = newNode;
newNode.Next = node.Next;
node.Next = newNode;
return true;
}
public bool InsertBefore(T insert, T value)
{
if (insert == null || value == null)
return false;
DoublyNode<T> node = Search(insert);
if (node == null) //didn't find exisinting node
return false;
DoublyNode<T> newNode = new DoublyNode<T>(value);
newNode.Next = node;
if (node.Previous != null)
node.Previous.Next = newNode;
newNode.Previous = node.Next;
node.Previous = newNode;
return true;
}
public bool Remove(T value)
{
DoublyNode<T> node = Search(value);
if (node == null)
return false;
if (node.Previous == null)
head = head.Next;
else
node.Previous.Next = node.Next;
if (node.Next == null)
tail = tail.Previous;
else
node.Next.Previous = node.Previous;
return true;
}
}
Monthly Archives: August 2011
Singly Linked List (Going to update with comments and intro)
public class SinglyNode<T>
{
private T value;
private SinglyNode<T> next;
public T Value
{
get { return value; }
set { this.value = value; }
}
public SinglyNode<T> Next
{
get { return next; }
set { this.next = value; }
}
public SinglyNode(T value)
{
this.value = value;
}
}
public class SinglyLinkedList<T>
{
private SinglyNode<T> head;
public SinglyLinkedList()
{
this.head = null;
}
public SinglyNode<T> Head
{
get { return head; }
}
public void Insert(T value)
{
SinglyNode<T> node = new SinglyNode<T>(value);
node.Next = head;
head = node;
}
public void Delete()
{
if (head != null)
head = head.Next;
}
}
Basic trie data structure implementation (C#)
Today I want to talk about a cool little data structure called a “trie”. The trie gets its name from the word re/trie/val. A trie is a data structure for building memory efficient dictionaries with fast lookups. Tries are the foundation for one of the fastest known sorting algorithms (burstsort), it is also used for spell checking, and is used in applications that use text completion.
Let’s say I wanted to store the following words in a trie: to, tea, ted, ten, team, a, i, in, inn.

Note that the parent node is always null or empty.
To find the word /team/, we would traverse the trie starting at t > e > a > m*
To find the word /in/, we would go i > n*
If we were to search for the word /te/, it would return false because it doesn’t end on a node with a marker.
* indicates a marker for a complete word.
A complete word is indicated by a red star in the diagram. It is also marked by a boolean property in our implementation.
Our first class is the node which represents each node on the trie.
public class Node
{
char _letter;
bool _last;
Dictionary<char, Node> _children;
protected Node() { }
public Node(char c)
{
_children = new Dictionary<char, Node>();
_last = false;
_letter = c;
}
public char Letter
{
get { return this._letter; }
set { this._letter = value; }
}
public bool Last
{
get { return this._last; }
set { this._last = value; }
}
public Dictionary<char, Node> Children
{
get { return this._children; }
set { this._children = value; }
}
public Node ChildNode(char c)
{
if (Children != null)
{
if (Children.ContainsKey(c))
{
return Children1;
}
}
return null;
}
public override bool Equals(object obj)
{
if (obj == null || this.GetType() != obj.GetType())
return false;
return Equals(obj);
}
public bool Equals(Node obj)
{
if (obj != null
&& obj.Letter == this.Letter)
{
return true;
}
return false;
}
public override int GetHashCode()
{
int hash = 13;
hash = (hash * 7) + this.Letter.GetHashCode();
return hash;
}
}
*Edit: updated node class to use a dictionary for faster searches.
Our next class is our actual Trie, it contains each node and only has two operations – inserting and searching.
public class Trie
{
private Node _root;
public Trie()
{
_root = new Node(' ');
}
public void Insert(string s)
{
char[] word = s.ToLower().ToCharArray();
Node current = _root;
if (word.Length == 0)
{
current.Last = true;
}
for (int i = 0; i < s.Length; i++)
{
Node child = current.ChildNode(word[i]);
if (child != null)
{
current = child;
}
else
{
current.Children.Add(word[i], new Node(word[i]));
current = current.ChildNode(word[i]);
}
if (i == word.Length - 1)
{
current.Last = true;
}
}
}
public bool Search(string s)
{
char[] word = s.ToLower().ToCharArray();
Node current = _root;
while (current != null)
{
for (int i = 0; i < word.Length; i++)
{
if (current.ChildNode(word[i]) == null)
return false;
else
current = current.ChildNode(word[i]);
}
if (current.Last == true)
return true;
else
return false;
}
return false;
}
}
Implementation in a windows form application.
public partial class MainForm : Form
{
Trie trie;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
trie = new Trie();
}
private void findButton_Click(object sender, EventArgs e)
{
if (trie.Search(findTextBox.Text.Trim()))
{
results.Text = String.Format("\"{0}\" was found in the trie.", findTextBox.Text);
}
else
{
results.Text = String.Format("\"{0}\" was not found in the trie.", findTextBox.Text);
}
findTextBox.Text = "";
}
private void insertButton_Click(object sender, EventArgs e)
{
string phrase = insertTextBox.Text.Trim();
string[] words = phrase.Trim().Split(' ', ',', ';', '.');
foreach (string word in words)
{
trie.Insert(word);
}
trie.Insert(phrase);
results.Text = String.Format("\"{0}\" was successfully inserted into trie.", insertTextBox.Text);
insertTextBox.Text = "";
}
}
Here’s the implementation in action.
Inserting words into trie.


Searching the trie for a word.

If a word isn’t in the trie then we would get a “false”.

Removing entire directory using SSH
This is how to recursively delete a specified directory, including all files and all sub directories.
rm -rf DirectoryToDelete
rm : remove
-rf : recursively delete a specified directory, including all files and all sub directories. (note the f makes it delete without asking confirmation)
Installing wordpress plugins using SSH
This is a short tutorial about using SSH to install a wordpress plugin.
For this example I’m going to show you how I installed
- change directories to where your wp-content/plugins folder is located.
- use the wget command to download the zip folder.
- unzip your newly downloaded plugin
- remove that zip folder from your wp-content/plugins folder.
Code:
cd /wp-content/plugins wget http://downloads.wordpress.org/plugin/syntaxhighlighter.zip unzip syntaxhighlighter.zip rm syntaxhighlighter.zip
So you can use this method to securely manage your wordpress plugins.