0% found this document useful (0 votes)
22 views

Lecture 2.2.1 Queues

Uploaded by

borab25865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Lecture 2.2.1 Queues

Uploaded by

borab25865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

UNIVERSITY INSTITUTE OF

ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE
AND ENGG.

Bachelor of Engineering (Computer Science & Engineering)


DATA STRUCTURES 21CSH-211

TREES DISCOVER . LEARN . EMPOWER


1
TREES IN DATA STRUCTURE

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

• Height of a node is the length of


a longest path from this node to
a leaf
• All leaves are at height zero
• Height of a tree is the height of
its root (maximum level)
Tree

• Depth of a node is the length of


path from root to this node
• Root is at depth zero
• Depth of a tree is the depth of
its deepest leaf that is equal to
the height of this tree
Binary Trees
• There is no node with degree greater than two
Terminology

• node: the item of information plus the branches to each node.


• degree: the number of subtrees of a node
• degree of a tree: the maximum of the degree of the nodes in the
tree.
• terminal nodes (or leaf): nodes that have degree zero
• nonterminal nodes: nodes that don’t belong to terminal nodes.
• children: the roots of the subtrees of a node X are the children
of X
• parent: X is the parent of its children.

12
Some Terminology (cont’d)

• Siblings: children of the same parent are said to be siblings.


• Ancestors of a node: all the nodes along the path from the root
to that node.
• The level of a node: defined by letting the root be at level one. If
a node is at level l, then it children are at level l+1.
• Height (or depth): the maximum level of any node in the tree

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

Binary tree Not a binary tree


Binary Tree

Full binary tree Complete binary tree


Full Binary Tree

A binary tree is said to be full if all its leaves are at the


same level and every internal node has two
children.
The full binary tree of height h has
l = 2h leaves
and
m = 2h – 1 internal nodes.

Total # of Nodes?

Find height of tree if # of nodes are given


Complete Binary Tree

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

left_child data right_child


left_child right_child

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, k1.
• 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

• In Inorder, the root is


Inorder Traversal:
visited in-between left 1. Traverse left subtree
2. Visit the root
and right subtree traversal 3. Traverse right subtree

• In Preorder, the root


Postorder Traversal:
is visited after (pre) 1. Traverse left subtree
2. Traverse right subtree
the subtrees traversals 3. Visit the root

CS 103 28
Illustrations for Traversals
• Assume: visiting a node 1

is printing its label 3 7

• 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)

• Almost Complete Binary Tree: An almost complete binary tree of n


nodes, for any arbitrary nonnegative integer n, is the binary tree
made up of the first n nodes of a canonically labeled full binary

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

You might also like