Trees
Trees
Trees
1
Topics
2
Motivations for a Binary Search
Tree
3
Motivations for a Binary Search
Tree
◼ A Binary Search Tree can be used to
insert, delete elements at O(log N) and
search for elements at O(log N)
◼ There are lots of other interesting
applications of trees because of its
hierarchical nature.
4
Trees
◼ A tree is really special variation of a
data structure called a graph.
◼ Trees have studied extensively in
mathematics.
◼ We are interested in a type of Tree
called Binary (Search) Trees.
◼ There are other types of trees such as
Red Black Trees, 2-3-4 trees, B-Trees
5
What is a tree?
A tree is a data structure representing
hierarchical relationships.
◼ Examples in everyday life:
– genealogical tree
– organisational chart
– parliamentary government
6
Visualising a tree
root leaves
Trees in computing
grow upside down
branches
branches
root leaves 7
Genealogical tree
Siridoris and Baby Nona
intermediate
node
banana strawberry
branches
ROOT NODE
Owner
Jake
Manager Chef
Brad Carol
10
Leaf Nodes Have No Children
Owner
Jake
Manager Chef
Brad Carol
LEAF NODES
11
A Subtree
Owner
Jake
Manager Chef
Brad Carol
LEFT SUBTREE OF
ROOT NODE
12
A Subtree
Owner
Jake
Manager Chef
Brad Carol
RIGHT SUBTREE OF
ROOT NODE
13
Binary search trees
E
Each node has two next
links instead if just one!
These are called left
and right links. B G
A D F H
◼ Left branch holds lower key values than
the current node.
◼ Right branch holds higher key values
than the current node.
14
Binary Search Tree
15
Defining class Node
◼ iData used to determine if this is the right
node for insert, delete, find in class Tree
methods.
dData to be manipulated, stored, etc.
left and right child references other instance of
class “Node”
Node
int iData
double dData
Node *leftChild
Node *rightChild
void displayNode()
16
Node Class
class Node {
public:
int iData; // data item (key)
double dData; // data item
Node* leftChild; // this node's left child
Node* rightChild; // this node's right child
17
Node Class
Node::Node(int piData, double pdData) {
iData = piData;
dData = pdData;
leftChild = NULL;
rightChild = NULL;
}
Node::displayNode() {
cout << “[“ << iData << “, “;
cout << dData << “]” << endl;
}
18
Defining class Tree
Tree
Node* root;
Tree();
Node* find(int key)
void insert(int id, double dd)
void delete(int id)
F=F
A D F H found!
◼ Follow the left link if the current node is
less than the target.
◼ Follow the right link if it’s greater.
◼ If you get to a leaf node and haven’t
found it, then it’s not in the tree! 21
find() method
iterative solution
Node* Tree::find(int key) {
Node* current = root; // start at root
while (current->iData != key) {// while no match,
if (key < current->iData) // go left?
current = current->leftChild;
else // or go right?
current = current->rightChild;
if (current == NULL) // if no child,
return NULL; // didn't find it
}
return current; // found it
} // end find() 22
find() method
recursive solution
Node* Tree::find(int key) {
return (findRecursive(key, root));
}
Node* Tree::findRecursive(int key, Node* cur) {
// Not in the tree so return null
if (cur == NULL)
return cur;
else if (key == cur->iData)
return cur;
else if (key < cur->iData)
return findRecursive(key, cur->leftChild);
else
return findRecursive(key, cur->rightChild);23
}
Traversing
◼ Going through the elements in a data
structure.
24
inOrder
◼ Call itself to traverse the node’s left sub tree
◼ Visit the node
◼ Call itself to traverse the node’s right sub tree
2
◼ Left
◼ Current 1 3
◼ Right
25
inOrder
void Tree::inOrder(Node* localRoot)
{
if (localRoot != NULL)
{
inOrder(localRoot->leftChild);
localRoot->displayNode();
inOrder(localRoot->rightChild);
}
}
26
preOrder
◼ Visit the node
◼ Call itself to traverse the node’s left sub tree
◼ Call itself to traverse the node’s right sub tree
1
◼ Current
◼ Left 2 3
◼ Right
27
Binary Search Tree – Pre Order
41 9 0 3 2 4 37 32 21 73 54 46 72 65 89 74 77
28
preOrder
void Tree::preOrder(Node* localRoot)
{
if(localRoot != NULL)
{
localRoot->displayNode();
preOrder(localRoot->leftChild);
preOrder(localRoot->rightChild);
}
} 29
postOrder
◼ Call itself to traverse the node’s left sub tree
◼ Call itself to traverse the node’s right sub tree
◼ Visit the node
3
◼ Left
1 2
◼ Right
◼ Current
30
Binary Search Tree – Post Order
2 4 3 0 21 32 37 9 46 65 72 54 77 74 89 73 41
31
postOrder
void Tree::postOrder(Node* localRoot)
{
if (localRoot != NULL)
{
postOrder(localRoot->leftChild);
postOrder(localRoot->rightChild);
localRoot->displayNode();
}
}
32
Insert C
E
Insert method C<E
◼ Traverse the
hierarchy to a leaf
node that is a valid B G
parent C>B
◼ The leaf node
becomes the parent A D F H
of the new leaf node
that is inserted. C<D
D is leaf node, so C is
C inserted as D’s child
Must update the left of right link of the inserted
node’s parent.
Trickier if you’re not using recursion!
33
Insert C
Recursive insert E
} 35
Insertion Recursive
Node* Tree::insertRec(int id, double dd, Node* cur) {
// create the new node and return memory address
if (cur == null) {
Node* theNode = new Node(id, dd);
return theNode;
}
else if (id < cur->iData)
cur->leftChild=insertRec(id, dd, cur->leftChild);
else if (id > cur->iData)
cur->rightChild=insertRec(id, dd, cur->rightChild)
return cur;
}
36
Delete method- Three cases...
37
Node to be deleted is a leaf node
D
Delete A
B G
A C E I
F H J
B G
C E I
F H J
H
C G
◼ Find the node to be
deleted. E I
◼ Node to be deleted is
replaced by left most
node in right sub-tree, F H J
called the successor.
◼ Successor’s right child
adopted by successor’s
pre-switch parent
(not shown). 40
Expression trees...
◼ In addition to binary search trees, binary
trees can also be used as expression
trees.
◼ Expression trees are a means of
representing an arithmetic expression.
◼ Infix and postfix representations can be
generated from the same tree using
different traversal mechanisms.
41
An expression tree example...
*
+ -
A B X Y
C G I
45
Binary Tree Complexity Analysis
◼ For a binary tree of height H and
L=H+1 levels, there are 2 L - 1 = N nodes.
47
Linked list N log 2 N
2 1
versus 4 2
balanced 8 3
16 4
binary tree... 32 5
64 6
128 7
256 8
512 9
assumes 1024 10
tree is 2048 11
balanced! 4096 12
8192 14
16384 13
32768 15 48
Two binary trees...
D A
B
B F
C
A C E G
D
B
B F
C
A C E G
D