Lecture 2.2.1 Queues
Lecture 2.2.1 Queues
ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE
AND ENGG.
2
Tree
• Collection of nodes or
Finite set of nodes
• This collection can be empty
• Nodes and Edges
• Root
• Parent, Child, Siblings, Grand parent,
Grand child, Ancestors, Decendents
• Every node except the root has one
parent
• Subtree
• Degree of a node is # of its sub trees
Tree
Why Trees?
1. 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:
2. Trees (with some ordering e.g., BST) provide moderate access/search (quicker than Linked
List and slower than arrays).
3. Trees provide moderate insertion/deletion (quicker than Arrays and slower than
Unordered Linked Lists).
4. Like Linked Lists and unlike Arrays, Trees don’t have an upper limit on number of nodes as
nodes are linked using pointers.
5
Tree levels
Two different approaches
Tree levels
Tree
• A path from node n1 to nk is
defined as a sequence of nodes
n1, n2, …….., nk
• The length of this path is the
number of edges on the path
• There is a path of length zero
from every node to itself
• There is exactly one path from
the root to each node in a tree
Tree
12
Some Terminology (cont’d)
13
Example
A is the root node Property: (# edges) = (#nodes) - 1
B is the parent of D and E
C is the sibling of B
D and E are the children of B Level
D, E, F, G, I are external nodes, or leaves
A, B, C, H are internal nodes A 1
The level of E is 3
The height (depth) of the tree is 4 B C
The degree of node B is 2 2
The degree of the tree is 3
The ancestors of node I is A, C, H H
The descendants of node C is F, G, H, I 3
D E F G
I
4
14
Binary Tree
Total # of Nodes?
A complete binary
tree is either a full
binary tree or one
that is full except for a
segment of missing
leaves on the right
side of the bottom
level.
Tree Traversals
• Pre-Order
• NLR
• In-Order
• LNR
• Post-Order
• LRN
Tree Traversals
Pre-Order(NLR)
1, 3, 5, 9, 6, 8
Tree Traversals
In-Order(LNR)
5, 3, 9, 1, 8, 6
Tree Traversals
Post-Order(LRN)
5, 9, 3, 8, 6, 1
– List Representation
• we can write of Figure 5.2 as a list in which each of the subtrees is also a list
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
• The root comes first,
followed by a list of sub-trees
23
[1]
Representation of Trees [2]
A
B
[3] C
(1) waste space
(2) insertion/deletion [4] D
A [1] A [5]
problem E
[2] B [6] F
[3] -- [7]
B G
[4] C [8]
A H
[5] -- [9]
C I
[6] --
[7] -- B C
D [8] D
[9] --
. . D E F G
E
[16] E
H
CHAPTER 5
I 24
Linked Representation
typedef struct node *tree_pointer;
typedef struct node {
int data;
tree_pointer left_child, right_child;
};
data
25
Cont’d
26
Properties of Binary Trees
• Properties of binary trees
• Lemma 5.1 [Maximum number of nodes]:
1. The maximum number of nodes on level i of a binary tree is 2i-1, i 1.
2. The maximum number of nodes in a binary tree of depth k is 2k-1, k1.
• Lemma 5.2 [Relation between number of leaf nodes and degree-2 nodes]:
For any nonempty binary tree, T, if n0 is the number of leaf nodes and n2 is the
number of nodes of degree 2, then n0 = n2 + 1.
• These lemmas allow us to define full and complete binary trees
27
Preoder, Inorder, Postorder
• In Preorder, the root Preorder Traversal:
1. Visit the root
is visited before (pre) 2. Traverse left subtree
the subtrees traversals 3. Traverse right subtree
CS 103 28
Illustrations for Traversals
• Assume: visiting a node 1
• Preorder:
5 8 9
1 3 5 4 6 7 8 9 10 11 12
10
4 6
• Inorder:
4 5 6 3 1 8 7 9 11 10 12 11 12
• Postorder:
4 6 5 3 8 11 12 10 9 7 1
CS 103 29
Illustrations for Traversals (Contd.)
• Assume: visiting a node 15
8 20
is printing its data
• Preorder: 15 8 2 6 3 7 2 11 27
11 10 12 14 20 27 22 30 6 22 30
10 12
• Inorder: 2 3 6 7 8 10 11
3 7 14
12 14 15 20 22 27 30
• Postorder: 3 7 6 2 10 14
12 11 8 22 30 27 20 15
30
Code for the Traversal Techniques
• The code for visit void preOrder(Tree *tree){
if (tree->isEmpty( )) return;
is up to you to visit(tree->getRoot( ));
preOrder(tree->getLeftSubtree());
provide, depending preOrder(tree->getRightSubtree());
}
on the application void inOrder(Tree *tree){
• A typical example if (tree->isEmpty( )) return;
inOrder(tree->getLeftSubtree( ));
for visit(…) is to visit(tree->getRoot( ));
inOrder(tree->getRightSubtree( ));
print out the data }
part of its input void postOrder(Tree *tree){
if (tree->isEmpty( )) return;
node postOrder(tree->getLeftSubtree( ));
postOrder(tree->getRightSubtree( ));
visit(tree->getRoot( ));
}
31
Application of Traversal Sorting a BST
• Observe the output of the inorder traversal of the BST example
two slides earlier
• It is sorted
• This is no coincidence
• As a general rule, if you output the keys (data) of the nodes of a
BST using inorder traversal, the data comes out sorted in
increasing order
32
Other Kinds of Binary Trees
(Full Binary Trees)
• Full Binary Tree: A full binary tree is a binary tree where all the
leaves are on the same level and every non-leaf has two children
• The first four full binary trees are:
33
Examples of Non-Full Binary Trees
• These trees are NOT full binary trees: (do you know why?)
34
Canonical Labeling of
Full Binary Trees
• Label the nodes from 1 to n from the top to the bottom, left to right
1
1 1 3
2
2 3
4 5 6 7
1
2 3 Relationships between labels
5 6 7 of children and parent:
4
i
8 9 10 11 2i+1
12 13 14 15 2i
35
Other Kinds of Binary Trees
(Almost Complete Binary trees)
1
1 1
1
1 2 3
2 2 3
2 1
1 4 5 6
4 2 3
2 3
4 5
4 5 6 7
36
References
1. Li`pschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
2. Data structure and algorithm by Narasimha Karumanchi.
3. www.tutorialspoint.com
4. www.geeksforgeeks.com
37
Books Recommended
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and
Algorithms in C++”, Wiley Student Edition.
• Aho, Alfred V., Ullman, Jeffrey D., Hopcroft ,John E. “Data Structures and Algorithms”,
Addison Wesley
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C++”, Prentice Hall
of India
THANK YOU
39