Professional Documents
Culture Documents
Robert Horvick
SOFTWARE ENGINEER
@bubbafat www.roberthorvick.com
What are trees?
Binary search trees
Overview Basic operations
- Adding data
- Removing data
Traversals
- Pre-order, post-order, in-order
http://commons.wikimedia.org/wiki/File:Origin_of_Species.svg
Properties of Trees
0 or 1 parent node
root
left right
root
left right
root
left right
root
left right
public T Data;
public BSTNode<T> Left;
public BSTNode<T> Right;
}
Left Right
BSTNode<string> root = new BSTNode<string>("root");
root.Left = left;
root.Right = right;
“left” “right”
12
12
12
14
14
12
12
14 7 14
7
12
12
14
7 7 14
7
7
12 12
14
7
7 7 14
18
7 18
12
12
14
7
7
7 14
18
3
3 7 18
12
12
14
7
7
7 14
18
3
3 7 18
12
12
14
7
7
7 14
18
3
3 7 18
12
12
14
7
7
7 14
18
3
3 7 18
12
12
14
7
7
7 14
18
3
3 7 18
Insertion Complexity
O(log n) O(n)
Balanced Tree
12
7 14
3 7 18
Unbalanced Tree
12
Traversals
Traversal Operations
Visit the left child Visit the right child Process the current
value
Tree Traversals
In-order Post-order
Pre-order
The left child is visited The left and right
The node is visited
before the node, then children are visited
before it’s children
the right child before the node
Pre-order Traversal
Process the current Visit the left child Visit the right child
value
12
7 14
12
3 7 18
12
12 7 14
7
3 7 18
12
12
7 14
7
3
3 7 18
12
12
7 7 14
3
7
3 7 18
12
12
7
7 14
3
7
14
3 7 18
12
12
7
3 7 14
7
14
18 3 7 18
12
12
7
3 7 14
7
14
18 3 7 18
public void PreOrderTraversal( Accept an action to perform when processing a
Action<T> action) node (will be invoked once for each node)
{
PreOrderTraversal(action, Root); Call the private recursive function
}
12
7 14
3 7 18
Example Usage
12 12
7 14 7 14
3 7 18 3 7 18
BinaryTree<int> original = new BinaryTree<int>();
Visit the left child Process the current Visit the right child
value
12
7 14
3
3 7 18
12
3 7 14
7
3 7 18
12
3
7 14
7
7
3 7 18
12
3
7 7 14
7
12
3 7 18
12
3
7
7 14
7
12
14
3 7 18
12
3
7
7 7 14
12
14
18 3 7 18
12
3
7
7 7 14
12
14
18 3 7 18
public void InOrderTraversal( Accept an action to perform when processing a
Action<T> action) node (will be invoked once for each node)
{
InOrderTraversal(action, Root); Call the private recursive function
}
12
7 14 3 7 7 12 14 18
3 7 18
BinaryTree<int> tree = new BinaryTree<int>();
Visit the left child Visit the right child Process the current
value
12
7 14
3
3 7 18
12
3 7 14
7
3 7 18
12
3
7 14
7
7
3 7 18
12
3
7 7 14
7
18
3 7 18
12
3
7
7 14
7
18
14
3 7 18
12
3
7
7 7 14
18
14
12 3 7 18
12
3
7
7 7 14
18
14
12 3 7 18
public void PostOrderTraversal( Accept an action to perform when processing a
Action<T> action) node (will be invoked once for each node)
{
PostOrderTraversal(action, Root); Call the private recursive function
}
action(node.Value);
Process the node before visiting children
}
}
Example Usage
12
7 14
3 7 18
Traversal Complexity
O(n) O(n)
Searching
12
7 14
3 9 18
12
7 14
12
3 9 18
12
12 7 14
7
3 9 18
12
12
7 14
7
7
3 9 18
Traversal Complexity
O(log n) O(n)
Removal
Removal Cases
7 14
3 9 18
12
7 14
Find the node
3 9 18
12
9 18
Removal Cases
7 14
9 18
12
7 14
Find the node
9 18
12
14
Find the node
9 18
12
9 18
Removal Cases
7 17
3 15 18
16
12
7 17
Find the node
3 15 18
16
7 17
Find the node
3 15 18
16
12
7 17
Find the node
3 15 18
16
12
7 17
Find the node
Go to the right child
3 15 18
16
12
16
12
16
15
Find the node
Go to the right child
7 17
Go to the left-most
child
Replace deleted
node with successor 3 18
Move the successor’s
child up
16
Removal Complexity
O(log n) O(n)
Demo
Replace backing store with a binary
search tree
- O(log n) operations
- Sorted output