ICT 143-2 Data Structures and
Algorithms
Mrs. W.B.M.S.C. Wijayakoon
Lecturer(Probationary)
BICT Degree Program
Faculty of Technological Studies, UWU
Lecture – Tree Data Structure
LECTURE - STRUCTURE
▪Tree Data Structure
▪Applications
▪Tree Types
▪Binary Tree
▪General Tree
▪Binary Search Trees
DATA STRUCTURES AND ALGORITHMS 2
Trees
•A tree data structure is a powerful tool for organizing data objects based on keys.
•It is equally useful for organizing multiple data objects in terms of hierarchical relationships
(think of a “family tree”, where the children are grouped under their parents in the tree).
•Further, Tree is one of the most important non-linear data structures in computing. It allows us
to implement faster algorithms( compared with algorithms using linear data structures).
Application areas are :
•Almost all operating systems store files in trees or tree like structures.
•Compilers use a syntax tree to validate the syntax of every program you write
•Binary Search Trees(BSTs) are used to quickly check whether an element is present in a set or
not(Searching Algorithms)
DATA STRUCTURES AND ALGORITHMS 3
Trees
DATA STRUCTURES AND ALGORITHMS 4
Tree Types
•Binary Tree – Each node has at most two children
•General Tree – Each node can have an arbitrary number of
children
•Binary Search Tree
•Red-Black Tree
DATA STRUCTURES AND ALGORITHMS 5
Tree Terminology and Basic Properties
Definition : A tree is recursively defined as a set of one or more nodes where one
node is designated as the root of the tree and all the remaining nodes can be
partitioned into non-empty sets each of which is a sub-tree of the root.
The above figure shows a tree where node A is the root node; nodes B, C, and D are
children of the root node and form sub-trees of the tree rooted at node A.
DATA STRUCTURES AND ALGORITHMS 6
Basic Terminology
•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
DATA STRUCTURES AND ALGORITHMS 7
Basic Terminology
•Root node The root node R is the topmost node in the tree. If R = NULL, then it
means the tree is empty
•Sub-trees If the root node R is not NULL, then the trees T1, T2, and T3 are
called the sub-trees of R.
•Leaf node A node that has no children is called the leaf node or the terminal
node.
•Path A sequence of consecutive edges is called a path. For example, in the
above diagram, the path from the root node A to node I is given as: A, D, and
I.
DATA STRUCTURES AND ALGORITHMS 8
Basic Terminology cont…
•Ancestor node An ancestor of a node is any predecessor node on the path from root
to that node.
•The root node does not have any ancestors. In the tree given in the above diagram,
nodes A, C, and G are the ancestors of node K.
•Descendant node A descendant node is any successor node on any path from the node
to a leaf node.
•Leaf nodes do not have any descendants. In the tree given in above, nodes C, G, J,
and K are the descendants of node A.
•A parent is an ancestor to its children and its children are descendants
DATA STRUCTURES AND ALGORITHMS 9
Basic Terminology cont…
•Level number Every node in the tree is assigned a level number in
such a way that the root node is at level 0, children of the root
node are at level number 1.
•Thus, every node is at one level higher than its parent. So, all child
nodes have a level number given by parent’s level number + 1.
DATA STRUCTURES AND ALGORITHMS 10
Basic Terminology cont…
•Degree: Degree of a node is equal to the number of children that
a node has.
•The degree of a leaf node is zero.
•In-degree: In-degree of a node is the number of edges arriving at
that node.
•Out-degree: Out-degree of a node is the number of edges
leaving that node.
DATA STRUCTURES AND ALGORITHMS 11
Basic Terminology cont…
▪E.g.
Indegree of a node -The number of edges merging into a node. For example,
indegree of the node B is one i.e., one edge merges.
Outdegree of a node -The number of edges coming out from a node. For
example, outdegree of the node A is two i.e., two edges come out of this root
node.
DATA STRUCTURES AND ALGORITHMS 12
Basic Terminology cont…
Depth of a node - the length of the path from the root to that
node
•root: depth 0
Height of a node - the length of the longest path from that node to
a leaf
•any leaf: height 0
Height of a tree: The length of the longest path from the root to a
leaf
DATA STRUCTURES AND ALGORITHMS 13
Trees - Example
DATA STRUCTURES AND ALGORITHMS 14
E.g. A table of contents and its trees
representation
Book
•Book
•C1
• S1.1
• S1.2 C1 C2 C3
•C2
• S2.1
• S2.1.1
S1.1 S1.2 S2.1 S2.2 S2.3
• S2.1.2
• S2.2
• S2.3
•C3 S2.1.1 S2.1.2
DATA STRUCTURES AND ALGORITHMS 15
•Degree: The number of sub tree of a node is called its degree.
•E.g., Degree of book 3, C1 2,C3 0
•Nodes that have degree 0 is called Leaf or Terminal node. Other nodes called
non-terminal nodes. E.g., Leaf nodes :C3,S1.1,S1.2 etc.
•Book is said to be the father (parent) of C1,C2,C3 and C1,C2,C3 are said to be
sons (children)of book.
•Children of the same parent are said to be siblings. E.g., C1,C2,C3 are siblings
(Brothers)
•Length : The length of a path is one less than the number of nodes in the path.(E.g.,
path from book to S1.1 = 3 - 1 = 2
DATA STRUCTURES AND ALGORITHMS 16
•If there is a path from node a to node b, then a is an ancestor of b and b is
descendent of a.
•In above example, the ancestor of S2.1are itself, C2 and book, while it descendent
are itself, S2.1.1 and S2.1.2.
•An ancestor or descendent of a node, other than the node itself is called a proper
ancestor or proper descendent.
•Height of a tree — the maximum depth of a leaf node. [ In above example
height=3]
•Depth of a node —the length of the path between the root and the node. [In above
example node C1 has Depth 1, node S2.1.2 has Depth 3. etc.]
DATA STRUCTURES AND ALGORITHMS 17
General Tree
DATA STRUCTURES AND ALGORITHMS 19
Definition of Tree – General Tree
A tree is a finite set of one or more nodes such that:
There is a specially designated node called the root.
The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn,
where each of these sets is a tree.
We call T1, ..., Tn the subtrees of the root.
DATA STRUCTURES AND ALGORITHMS 20
Definition of Tree – General Tree
•A tree is a collection of nodes.
•The collection can be empty;
•otherwise, a tree consists of a distinguish node r, called root,
and zero or more non-empty (sub)trees T1,T2,T3,….TK
•Each of whose roots are connected by a directed edge from r.
DATA STRUCTURES AND ALGORITHMS 21
Definition of Tree – General Tree
A tree consists of set of nodes and set of edges that connected
pair of nodes.
DATA STRUCTURES AND ALGORITHMS 22
Representation of Tree
List Representation
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
The root comes first, followed by a list of sub-trees
DATA STRUCTURES AND ALGORITHMS 23
A Tree Node
Every tree node:
– object – useful information
– children – pointers to its child nodes
DATA STRUCTURES AND ALGORITHMS 24
Tree - Implementation
•One way to implement a tree would be to have in each node
a reference to each child of the node in addition to its data.
•However, the number of children per node can vary so greatly
and is not known in advanced.
•It might be infeasible to make the children direct links in the
data structure.
•These would be too much wasted space.
DATA STRUCTURES AND ALGORITHMS 25
The solution is simple
Keep the children of each node in a linked list of tree nodes.
Thus, each node keeps two references :
one to its leftmost child and
other one for its right sibling.
DATA STRUCTURES AND ALGORITHMS 26
Example
DATA STRUCTURES AND ALGORITHMS 27
Left Child – Right Sibling representation of a
tree
Try it…
DATA STRUCTURES AND ALGORITHMS 28
Left Child – Right Sibling representation of a
tree
DATA STRUCTURES AND ALGORITHMS 29
Binary Tree
DATA STRUCTURES AND ALGORITHMS 30
Linked Structure for Binary Trees
We represent each node of a binary tree by an object which stores
–Element
–References to its parent and child nodes
DATA STRUCTURES AND ALGORITHMS 31
Properties of Binary Trees
Binary trees come in many deferent shapes and sizes. The
shapes vary depending on the number of nodes and how the
nodes are linked.
DATA STRUCTURES AND ALGORITHMS 32
Tree different arrangements of nine nodes in a
binary tree (cont…)
•Consider node G in the three trees of
above Figure. In tree (a), G has a
depth of 2, in tree (b) it has a depth
of 3, and in (c) its depth is 6.
•The height of a binary tree is the
number of levels in the tree. For
example, the three binary trees in the
above Figure have different heights:
(a) has a height of 3, (b) has a height
of 5, and (c) has a height of 7.
DATA STRUCTURES AND ALGORITHMS 33
Complete Binary Trees
A complete binary tree is a binary tree that satisfies following 3
properties.
•In a complete binary tree, every level, except possibly the last, is
completely filled.
•all nodes appear as far left as possible.
•Each non leaf node has exactly two child nodes.
DATA STRUCTURES AND ALGORITHMS 34
Complete Binary Trees - Example
•In a complete binary tree Tn, there are exactly n nodes and level r of
T can have at most 2r nodes
•Note that in the diagram,
level 0 has 20= 1 node,
level 1 has 21= 2 nodes,
level 2 has 22= 4 nodes,
•level 3 has 6 nodes which is less than
the maximum of 23= 8 nodes.
DATA STRUCTURES AND ALGORITHMS 35
Complete Binary Trees - Example
•In this tree has exactly 13 nodes. They have been purposely labelled from 1 to 13,
•So that it is easy for the reader to find the parent node, the right child node, and the left
child node of the given node.
•The formula can be given as—if K is a parent node, then its left child can be calculated as
2×K and its right child can be calculated as 2 ×K + 1.
•For example, the children of the node 4 are 8 (2 ×4) and 9 (2 ×4 + 1).
•Similarly, the parent of the node K can be calculated as | K/2 |.
•Given the node 4, its parent can be calculated as | 4/2 | = 2.
•The height of a tree Tn having exactly n nodes is given as:
•Hn= | log2 (n + 1) |
•if a tree (T) has 10,00,000 nodes, then its height is 21.
DATA STRUCTURES AND ALGORITHMS 36
Properties of Full Binary Trees
•A binary tree is a full binary tree if and only if:
•Each non leaf node has exactly two child nodes
•All leaf nodes have identical path length
•It is called full since all possible node slots are occupied
DATA STRUCTURES AND ALGORITHMS 37
Full Binary Tree - Example
DATA STRUCTURES AND ALGORITHMS 38
Complete Binary Tree - Example
DATA STRUCTURES AND ALGORITHMS 39
Tree Terminology and Basic Properties
•Properties of Binary Trees
Let T be a complete binary tree with n nodes, and let h denote the
height of T, then T has the following properties:
DATA STRUCTURES AND ALGORITHMS 40
Properties of binary trees
•If a binary tree contains m nodes at level L, then it contains at
most 2m nodes at level L+1.
•A binary tree can contain at most 2L nodes at L
At level 0 B-tree can contain at most 1= 20 nodes
At level 1 B-tree can contain at most 2= 21 nodes
At level 2 B-tree can contain at most 4= 22 nodes
At level L B-tree can contain at most -> 2L nodes
DATA STRUCTURES AND ALGORITHMS 41
Full Binary tree
A full Binary tree of depth d is the Binary tree that contains
exactly 2L nodes at each level between 0 and d ( or 2d nodes at
d)
DATA STRUCTURES AND ALGORITHMS 42
The total number of nodes (Tn) in a full Binary
d+1
tree of depth d is 2 -1
DATA STRUCTURES AND ALGORITHMS 43
Representation of Binary Tree in the Memory
•In the computer’s memory, a binary tree can be maintained
either by using a linked representation or by using a
sequential representation.
•Linked representation of binary trees
•In the linked representation of a binary tree, every node
will have three parts:
•the data element,
•a pointer to the left node, and
•a pointer to the right node.
DATA STRUCTURES AND ALGORITHMS 44
Representation of Binary Tree in the Memory
DATA STRUCTURES AND ALGORITHMS 45
Representation of Binary Tree in the Memory
DATA STRUCTURES AND ALGORITHMS 46
Representation of Binary Tree in the Memory
DATA STRUCTURES AND ALGORITHMS 47
Traversals of Binary Trees
•A traversal of a tree is a systematic way of accessing or “visiting”
all the nodes in the tree.
•There are three basic traversal schemes:
–Pre-order traversal
–In-order traversal
–Post-order traversal
DATA STRUCTURES AND ALGORITHMS 51
Pre-Order Traversal
•A pre-order traversal has three steps for a nonempty tree:
–Process the root.
–Process the nodes in the left subtree with a recursive call.
–Process the nodes in the right subtree with a recursive
call.
DATA STRUCTURES AND ALGORITHMS 52
Pre-Order Traversal
SL – Left Subtree
SR – Left Subtree 1. Visit the node
2. Traverse the
3. Traverse the
Left Subtree
Right Subtree
DATA STRUCTURES AND ALGORITHMS 53
Pre-Order Traversal
•To traverse a non-empty binary tree in pre-order (also
known as depth first order), we perform the following
operations.
•Visit the root ( or print the root)
•Traverse the left in pre-order (Recursive)
•Traverse the right tree in pre-order (Recursive)
DATA STRUCTURES AND ALGORITHMS 54
Pre-Order Traversal
A, B, D, H, I, E, C, F, J, K, G
DATA STRUCTURES AND ALGORITHMS 55
Pre-Order Traversal
Using Pre-Order traversal of a binary tree to solve the expression
evaluation problem
DATA STRUCTURES AND ALGORITHMS 56
Pre-Order Traversal - Algorithm
Step 1: Repeat 2 to 4 while TREE != Null
Step 2: Write TREE → Data
Step 3: PREORDER(TREE→LEFT)
Step 4: PREORDER(TREE→RIGHT)
[END OF LOOP]
Step 5: END
DATA STRUCTURES AND ALGORITHMS 58
In-Order Traversal
•An in-order traversal has three steps for a nonempty
tree:
•Process the nodes in the left subtree with a recursive call.
•Process the root.
•Process the nodes in the right subtree with a recursive call.
DATA STRUCTURES AND ALGORITHMS 59
In-Order Traversal
H,D,I,B,E,A,J,F,K,C,G
DATA STRUCTURES AND ALGORITHMS 61
In-Order Traversal
Using in-Order traversal of binary tree to solve the expression
evaluation problem
DATA STRUCTURES AND ALGORITHMS 62
In-Order Traversal - Algorithm
Step 1: Repeat 2 to 4 while TREE != Null
Step 2: INORDER(TREE→LEFT)
Step 3: Write TREE → Data
Step 4: INORDER(TREE→RIGHT)
[END OF LOOP]
Step 5: END
DATA STRUCTURES AND ALGORITHMS 64
Post-Order Traversal
•A post-order traversal has three steps for a nonempty tree:
–Process the nodes in the left subtree with a recursive call.
–Process the nodes in the right subtree with a recursive call.
–Process the root.
DATA STRUCTURES AND ALGORITHMS 65
Post-Order Traversal
H, I, D, E, B, J, K, F, G, C, A
DATA STRUCTURES AND ALGORITHMS 67
Post-Order Traversal
Pre-order list –
2,5,11,6,7,4,9,5,2
DATA STRUCTURES AND ALGORITHMS 68
Post-Order Traversal - Algorithm
Step 1: Repeat 2 to 4 while TREE != Null
Step 2: POSTORDER(TREE→LEFT)
Step 3: POSTORDER(TREE→RIGHT)
Step 4: Write TREE → Data
[END OF LOOP]
Step 5: END
DATA STRUCTURES AND ALGORITHMS 69
Recursive view used to calculate the size of a
tree: S = SL+SR+1
DATA STRUCTURES AND ALGORITHMS 70
Recursive view used to calculate the size of a
tree: S = SL+SR+1
•Below algorithm finds the size of a tree recursively.
•ie, size of a tree = size of a left sub tree+ 1 + size of a
right subtree.
•For each Node, size() function calculates the size of the
left sub tree and right sub tree and adds 1, to include the
present node.
DATA STRUCTURES AND ALGORITHMS 71
Example
DATA STRUCTURES AND ALGORITHMS 72
•Same as Tree Traversal
If the tree is empty
return
else,
return size of the tree ie, (size(Node->left)+ 1 + size(Node->right))
In return statement we are calling the size of the left subtree
recursively ie, size(Node->left)
In return statement we are calling the size of right subtree recursively
ie, size(Node->right)
DATA STRUCTURES AND ALGORITHMS 73
Pseudo Code Algorithm to calculate the size of
the Binary Tree
//* Return the size of the binary tree rooted at t.
size (BinaryNode t)
if (t = = null)
return 0
else
return 1+size(t->left)+ size(t->right)
endif
endsize
DATA STRUCTURES AND ALGORITHMS 74
Recursive view used to calculate the height of
tree: HT = max(HL+1,HR+1)
DATA STRUCTURES AND ALGORITHMS 75
To compute the height of a node
Routine to compute the height of a node
//* Return the height of the binary tree rooted at t.
height ( BinaryNodet){
if (t = = null)
return 0;
else
return 1+max(height(t->left),height(t->right);
}
DATA STRUCTURES AND ALGORITHMS 76
End…
DATA STRUCTURES AND ALGORITHMS 77