8. 2Trees
8. 2Trees
8. 2Trees
Trees
Part I: General Tree Concepts
3
Why Tree?
• One reason to use trees might be because you want to store information
that naturally forms a hierarchy.
• For example, the file system on a computer
• If we organize keys in form of a tree (with some ordering e.g., BST), we can
search for a given key in moderate time (quicker than Linked List and
slower than arrays).
• Self-balancing search trees like AVL and Red-Black trees guarantee an
upper bound of O(Logn) for search.
• We can insert/delete keys in moderate time (quicker than Arrays and
slower than Unordered Linked Lists)
4
Tree
Tree consists of a distinguished node r, called the root, and
zero or more non-empty (sub)trees T1, T2, ... , Tk, each of
whose roots are connected by a directed edge from r.
5
Definitions
• tree - a non-empty collection of vertices & edges
• vertex (node) - can have a name and carry other associated
information
• path - list of distinct vertices in which successive vertices are
connected by edges
• any two vertices must have one and only one path
between them else its not a tree
• a tree with N nodes has N-1 edges
6
Definitions
• root - starting point (top) of the tree
• parent (ancestor) - the vertex “above” this vertex
• child (descendent) - the vertices “below” this vertex
• Siblings - Nodes with the same parent
• Every node except the root has one parent
7
Definitions
• leaves (terminal nodes) - have no children
• level - the number of edges between this node and the root
• Path - from node n1 to nk, it is defined as a sequence of
nodes n1, n2, ... , nk such that ni is the parent of ni+1 for 1 ≤
i<k
• Length - of the path is the number of edges on the path
8
Definitions
• Depth of a node - the length of the path from the root to
that node
• root: depth 0
9
Definitions
• Depth of a tree: is the length between the deepest leaf to
root.
• equal to the depth of the deepest leaf
10
A tree
11
Tree structure
Trees - Example
Level root
E
0
A S T
2
Leaves or terminal nodes
Depth of T: 2
3 M P L E
Height of T: 1
13
Properties of tree
• A tree can contain no nodes or it can contain one special
node called the root with zero or more subtrees.
• Every edge of the tree is directly or indirectly originated
from the root.
• Every child has only one parent, but one parent can have
many children.
Application
15
Tree Implementation
Class TreeNode
{
Object element;
TreeNode firstChild;
TreeNode nextSibling;
}
The solution is simple: Keep the children of each node in a linked list of
tree nodes.
16
Binary Tree
• A binary tree is a tree in which no node can have more than
two children.
• consists of
• a root
• left subtree (may be empty)
• right subtree (may be empty)
17
Binary Tree
S
P O
I N
S M A
B D
18
Height of a Full Binary Tree
L0
L1
L2
L3
19
Nodes and Levels in a Complete Binary Tree
Number of the nodes in a tree with M levels:
1 + 2 + 22 + …. 2M = 2 (M+1) - 1 = 2*2M - 1
N = 2*2M - 1, 2*2M = N + 1
2M = (N+1)/2
M = log( (N+1)/2 )
20
Binary Tree Implementation
Class BinaryNode {
Object Element; // the data in the node
}
Binary Tree node is similar in structure to node of doubly linked
list
21
Binary Tree – Operations
• InsertLeft
• an error occurs if v already has a left child
• InsertRight
• an error occurs if v already has a right child
• Remove
• Remove node and replace with child
• an error occurs if v has 2 children
• Traverse
22
Binary Tree – Preorder Traversal
Root C
Left
O T
Right
M E
R
P U L
A N
First letter - at the root
D
Last letter – at the rightmost node
23
Preorder Algorithm
preorderVisit(tree)
{
if (current != null)
{
process (current);
preorderVisit (left_tree);
preorderVisit (right_tree);
}
}
24
Binary Tree – Inorder Traversal
Left U
Root
Right P T
O R
E
C M A
L D
First letter - at the leftmost node
Last letter – at the rightmost node N
25
Inorder Algorithm
inorderVisit(tree)
{
if (current != null)
{
inorderVisit (left_tree);
process (current);
inorderVisit (right_tree);
}
}
26
Binary Tree – Postorder Traversal
Left
Right D
Root P N
M A
U
C O L
T R
First letter - at the leftmost node
E
Last letter – at the root
27
Postorder Algorithm
postorderVisit(tree)
{
if (current != null)
{
postorderVisit (left_tree);
postorderVisit (right_tree);
process (current);
}
}
28
Traversals
Traverse it using 1
•Preorder (NLR)
•Inorder (LNR)
3 7
•Postorder (LRN) 5 8 9
4 6 10
11 12
CS 103 29
Traverse it using
•Preorder (NLR)
•Inorder (LNR)
•Postorder (LRN)
Another example
Application of Binary Tree
• One of the principal uses of binary trees is in the area of
compiler design like Expression Tree
• An important application of binary trees is their use in
searching like Binary Search Tree – BST
32
Expression Trees
The stack contains references to tree nodes (bottom is to the
left)
+ 3
1 2 *
1 2 + 3
(1+2)*3
Post-fix notation: 1 2 + 3 * 1 2
33
Expression Trees
In-order traversal:
*
(1 + 2) * ( 3)
+ 3 Post-order traversal:
1 2+3*
1 2
34
Review Questions
Question 1:
How height and depth of the tree are calculated?
Question 2:
What are different schemes of traversing a binary tree?
35