0% found this document useful (0 votes)
12 views72 pages

AV121 DSA KJ Lecture 01 Sets Trees

The document is the lecture plan for a course on data structures and algorithms. It introduces the topics of sets, trees, and maps that will be covered in the course. It provides examples and definitions of common tree structures and operations like nodes, edges, leaves, ancestors and descendants. It also defines set and map abstract data types, their typical operations, implementations and examples.

Uploaded by

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

AV121 DSA KJ Lecture 01 Sets Trees

The document is the lecture plan for a course on data structures and algorithms. It introduces the topics of sets, trees, and maps that will be covered in the course. It provides examples and definitions of common tree structures and operations like nodes, edges, leaves, ancestors and descendants. It also defines set and map abstract data types, their typical operations, implementations and examples.

Uploaded by

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

AV121: Data Structures and Algorithms

Kurian John

Lecture 01
Lecture 01 – Plan

 Introduction

 Review of Topics Covered

 Sets and Trees

April-May 2023 AV121 - Data Structures and Algorithms 2


Review of Topics Covered
 Complexity Analysis (Big-O, Ω, Θ)
 Linked lists
 Stacks
 Queues
 STL
 Sorting Algorithms

April-May 2023 AV121 - Data Structures and Algorithms 3


Why do we need to study Data Structures and Algorithms?

April-May 2023 AV121 - Data Structures and Algorithms 4


Set ADT
 Set
→ A collection of distinguishable objects
→ Duplicates are not allowed
→ Derived from the mathematical concept of finite sets

{5}

{7, 5, 8}

{}

 Used for collecting data where order or multiplicity is not


important – finds applications in different algorithms

April-May 2023 AV121 - Data Structures and Algorithms 5


Set ADT
 Typical Operations
→ Insert (<Element>)
→ Erase (<Element>)
→ Find (<Element>)
→ Size ()
→ isEmpty ()

April-May 2023 AV121 - Data Structures and Algorithms 6


Set ADT
 Typical Operations
→ Insert (<Element>)
→ Erase (<Element>)
→ Find (<Element>)
→ Size ()
→ isEmpty ()

→ Union
→ Intersection
→ Difference

April-May 2023 AV121 - Data Structures and Algorithms 7


Set ADT
 Implementation
→ Bit Vector ith bit is set to 1 if i is an element

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

 Useful when the size of the set is known / fixed


 Time complexity for Insert / Erase / Find?
 Time complexity for Union / Intersection / Difference?

→ List
 When the size of the set is unknown / dynamic

April-May 2023 AV121 - Data Structures and Algorithms 8


Some variants of sets
 Multi-set
→ Generalization of set, that allows duplicates

 Sorted set
→ Extension of set, when elements are ordered (Elements come
from a comparable class)

April-May 2023 AV121 - Data Structures and Algorithms 9


Map ADT
 Stores key-value pairs (k,v)
→ Cannot have duplicate keys
→ Also called Dictionary
 Typical Operations
→ get(k)  Returns the value corresponding to key ‘k’
→ put(k,v)  Insert new pair with key ‘k’ and value ‘v’
→ remove(k)
→ Size()
→ isEmpty()

 Useful where a key is used to identify an object/value


April-May 2023 AV121 - Data Structures and Algorithms 10
Map ADT
 Stores key-value pairs (k,v)
→ Cannot have duplicate keys
→ Also called Dictionary
 Typical Operations
→ get(k)  Returns the value corresponding to key ‘k’
→ put(k,v)  Insert new pair with key ‘k’ and value ‘v’
→ remove(k) Map M { } Put(A, α)
→ Size() M {(A, α)} Put(B, β)
→ isEmpty()

 Useful where a key is used to identify an object/value


April-May 2023 AV121 - Data Structures and Algorithms 11
Map ADT
 Stores key-value pairs (k,v)
→ Cannot have duplicate keys
→ Also called Dictionary
 Typical Operations
→ get(k)  Returns the value corresponding to key ‘k’
→ put(k,v)  Insert new pair with key ‘k’ and value ‘v’
→ remove(k) Map M { } Put(A, α)
→ Size() M {(A, α)} Put(B, β)
→ isEmpty()
M {(A, α),(B, β)} Put(L, λ)

 Useful where a key is used to identify an object/value


April-May 2023 AV121 - Data Structures and Algorithms 12
Map ADT
 Stores key-value pairs (k,v)
→ Cannot have duplicate keys
→ Also called Dictionary
 Typical Operations
→ get(k)  Returns the value corresponding to key ‘k’
→ put(k,v)  Insert new pair with key ‘k’ and value ‘v’
→ remove(k) Map M { } Put(A, α)
→ Size() M {(A, α)} Put(B, β)
→ isEmpty()
M {(A, α),(B, β)} Put(L, λ)
M {(A, α),(B, β), (L, λ)}

 Useful where a key is used to identify an object/value


April-May 2023 AV121 - Data Structures and Algorithms 13
Map ADT
 Stores key-value pairs (k,v)
→ Cannot have duplicate keys
→ Also called Dictionary
 Typical Operations
→ get(k)  Returns the value corresponding to key ‘k’
→ put(k,v)  Insert new pair with key ‘k’ and value ‘v’
→ remove(k) Map M { } Put(A, α)
→ Size() M {(A, α)} Put(B, β)
→ isEmpty()
M {(A, α),(B, β)} Put(L, λ)
M {(A, α),(B, β), (L, λ)}
Get(A)?
Get(L)?

 Useful where a key is used to identify an object/value


 Implementation?
April-May 2023 AV121 - Data Structures and Algorithms 14
Trees

"A picture like this is called a tree. If you want to


know why the tree is growing upside down, ask
the computer scientists who introduced this
convention. (The conventional wisdom is that
they never went out of the lab, and so they
never saw a real tree.)"
Discrete Mathematics: Elementary and Beyond
By László Lovász, József Pelikán, Katalin Vesztergombi

April-May 2023 AV121 - Data Structures and Algorithms 15


Trees
 A rooted tree data structure stores information in nodes
→ There is a first node, or root
→ Each node has variable number of references to successors
→ Each node, other than the root, has exactly one node pointing to
it

Non-linear!
April-May 2023 AV121 - Data Structures and Algorithms 16
Trees
 All nodes will have zero or more child nodes or children
→ I has three children: J, K and L

 For all nodes other than the root node, there is one
parent node
→ H is the parent of I

April-May 2023 AV121 - Data Structures and Algorithms 17


Trees
 The degree of a node is defined as the number of its
children:
→ deg(I) = 3

 Nodes with the same parent are siblings


→ J, K, and L are siblings

April-May 2023 AV121 - Data Structures and Algorithms 18


Trees
 Nodes with degree zero are also called leaf nodes

 All other nodes are said to be internal nodes, that is, they
are internal to the tree

April-May 2023 AV121 - Data Structures and Algorithms 19


Trees
 A path is a sequence of nodes (a0, a1, ..., an)
where ak+1 is a child of ak

The length of this path is n

→ E.g., the path (B, E, G) has length 2

April-May 2023 AV121 - Data Structures and Algorithms 20


Trees
 For each node in a tree, there exists a unique path from
the root node to that node
 The length of this path is the depth of the node
→ E has depth 2
→ L has depth 3

April-May 2023 AV121 - Data Structures and Algorithms 21


Trees
 The height of a tree is defined as the maximum depth of
any node within the tree

 The height of a tree with one node is 0


→ Just the root node

 For convenience, we define the height of the empty tree


to be –1

April-May 2023 AV121 - Data Structures and Algorithms 22


Trees
 If a path exists from node a to node b:
→ a is an ancestor of b
→ b is a descendent of a

 Thus, a node is both an ancestor and a descendant of


itself
→ We can add the adjective strict to exclude equality: a is a strict
descendent of b if a is a descendant of b but a ≠ b

 The root node is an ancestor of all nodes

April-May 2023 AV121 - Data Structures and Algorithms 23


Trees
 The descendants of node B are B, C, D, E, F, and G:

 The ancestors of node I are I, H, and A:

April-May 2023 AV121 - Data Structures and Algorithms 24


Trees
 A tree can be defined recursively:
→ A degree-0 node is a tree
→A node with degree n is a tree if it has n children and all of its
children are disjoint trees (i.e., with no intersecting nodes)

 Given any node a within a tree with root r, the collection


of a and all of its descendants is said to be a subtree of
the tree with root a

April-May 2023 AV121 - Data Structures and Algorithms 25


Trees – Example - HTML
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This is a <u>Heading</u></h1>

<p>This is a paragraph with some


<u>underlined</u> text.</p>
</body>
</html>

April-May 2023 AV121 - Data Structures and Algorithms 26


Trees – Example - HTML
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This is a <u>Heading</u></h1>

<p>This is a paragraph with some


<u>underlined</u> text.</p>
</body>
</html>

April-May 2023 AV121 - Data Structures and Algorithms 27


Trees – Example - HTML
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>This is a <u>Heading</u></h1>

<p>This is a paragraph with some


<u>underlined</u> text.</p>
</body>
</html>

April-May 2023 AV121 - Data Structures and Algorithms 28


Trees – Applications
 Represent hierarchical relationship among elements
→ Family trees
→ File systems
→ Organization charts
→ Language Processing

 Searchable data structure

April-May 2023 AV121 - Data Structures and Algorithms 29


Trees – Traversals
 Traversal is a process by which all nodes of a tree are
visited
→ Traversals start from the root
→ Each node is ‘visited’ once

 Different types of traversals


→ Level order
→ Pre order
→ Post order
→ In order

April-May 2023 AV121 - Data Structures and Algorithms 30


Trees – Traversals
 Level Order
→ Visits the root, then visits each element on the first level, then
visits each element on the second level, and so forth, each time
visiting all the elements on one level before going down to the
next level.
→ If the tree is drawn in the usual manner with its root at the top
and leaves near the bottom, then the level order pattern is the
same left-to-right top-to-bottom pattern that you follow to read
English text

Order: A B H C D G I E F J K

April-May 2023 AV121 - Data Structures and Algorithms 31


Trees – Traversals
 Pre-order
→ Root node is visited first, then visit the sub-trees (Left to right)

April-May 2023 AV121 - Data Structures and Algorithms 32


Trees – Traversals
 Pre-order
→ Root node is visited first, then visit the sub-trees (Left to right)

Order: A B D E C F G

April-May 2023 AV121 - Data Structures and Algorithms 33


Trees – Traversals
 Post-order
→ Traverse the sub-trees first (Left to right)
→ Root node is visited last

April-May 2023 AV121 - Data Structures and Algorithms 34


Trees – Traversals
 Post-order
→ Traverse the sub-trees first (Left to right)
→ Root node is visited last

Order: D E B F G C A

April-May 2023 AV121 - Data Structures and Algorithms 35


Trees – Traversals
 In order
→ Traverse the left sub-tree, root and then the right sub-tree

April-May 2023 AV121 - Data Structures and Algorithms 36


Trees – Traversals
 In order
→ Traverse the left sub-tree, root and then the right sub-tree

Order: D B E A F C G

April-May 2023 AV121 - Data Structures and Algorithms 37


Trees – Representing Mathematical Expressions
 Complicated expressions, such as compound
propositions, combinations of sets, and arithmetic
expressions can be represented by ordered root trees. For
arithmetic expressions:
→ Internal vertices: operations
→ Leaves: operands
(6-4) * (3 + (5-1))

April-May 2023 AV121 - Data Structures and Algorithms 38


Trees – Representing Mathematical Expressions
 Complicated expressions, such as compound
propositions, combinations of sets, and arithmetic
expressions can be represented by ordered root trees. For
arithmetic expressions:
→ Internal vertices: operations
→ Leaves: operands
(6-4) * (3 + (5-1))

April-May 2023 AV121 - Data Structures and Algorithms 39


Trees – Representing Mathematical Expressions
 Traversals?

(6-4) * (3 + (5-1))

April-May 2023 AV121 - Data Structures and Algorithms 40


Trees – Representing Mathematical Expressions
 Traversals?
→ Inorder 6-4 * 3 + 5-1

(6-4) * (3 + (5-1))

April-May 2023 AV121 - Data Structures and Algorithms 41


Trees – Representing Mathematical Expressions
 Traversals?
→ Inorder 6-4 * 3 + 5-1

→ Preorder *– 64+3–51

(6-4) * (3 + (5-1))

April-May 2023 AV121 - Data Structures and Algorithms 42


Trees – Representing Mathematical Expressions
 Traversals?
→ Inorder 6-4 * 3 + 5-1

→ Preorder *– 64+3–51

→ Postorder 64–351-+*

(6-4) * (3 + (5-1))

April-May 2023 AV121 - Data Structures and Algorithms 43


Trees – Representing Mathematical Expressions
 Traversals? Infix
→ Inorder 6-4 * 3 + 5-1

Prefix
→ Preorder *– 64+3–51

Postfix
→ Postorder 64–351-+*

(6-4) * (3 + (5-1))

April-May 2023 AV121 - Data Structures and Algorithms 44


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation
→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is
needed
→ Operands are pushed to a stack
→ Operands popped out and
operation is done whenever an
operator is encountered
(6-4) * (3 + (5-1))

April-May 2023 AV121 - Data Structures and Algorithms 45


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is 6
needed
→ Operands are pushed to a stack
→ Operands popped out and
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 46


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is 4
needed 6
→ Operands are pushed to a stack
→ Operands popped out and
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 47


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is –
4
needed 6
→ Operands are pushed to a stack
→ Operands popped out and
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 48


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is – (6-4) = 2
4
needed 6
→ Operands are pushed to a stack
→ Operands popped out and
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 49


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is 2
needed
→ Operands are pushed to a stack
→ Operands popped out and
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 50


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is 3
needed 2
→ Operands are pushed to a stack
→ Operands popped out and
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 51


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is 5
needed 3
→ Operands are pushed to a stack 2
→ Operands popped out and
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 52


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is 1
needed 5
→ Operands are pushed to a stack 3
→ Operands popped out and 2
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 53


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is – (5-1) = 4
1
needed 5
→ Operands are pushed to a stack 3
→ Operands popped out and 2
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 54


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is 4
needed 3
→ Operands are pushed to a stack 2
→ Operands popped out and
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 55


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is + (4+3) = 7
4
needed 3
→ Operands are pushed to a stack 2
→ Operands popped out and
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 56


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is 7
needed 2
→ Operands are pushed to a stack
→ Operands popped out and
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 57


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is * (7*2) = 14
7
needed 2
→ Operands are pushed to a stack
→ Operands popped out and
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 58


Trees – Representing Mathematical Expressions
 Postfix Expression Evaluation (6-4) * (3 + (5-1))

→ 64–351-+*
 A computer can evaluate this
with just a stack
→ No separate operator
precedence information is 14
needed
→ Operands are pushed to a stack
→ Operands popped out and
operation is done whenever an
operator is encountered

April-May 2023 AV121 - Data Structures and Algorithms 59


Trees – Reconstruction from Traversals
 Traversals 1
→ Inorder

2 3
→ Preorder

4 5 6
→ Postorder

April-May 2023 AV121 - Data Structures and Algorithms 60


Trees – Reconstruction from Traversals
 Traversals 1
→ Inorder 425163

2 3
→ Preorder 124536

4 5 6
→ Postorder 452631

April-May 2023 AV121 - Data Structures and Algorithms 61


Trees – Reconstruction from Traversals
 Traversals
→ Preorder 124536

April-May 2023 AV121 - Data Structures and Algorithms 62


Trees – Reconstruction from Traversals
 Traversals
→ Preorder 124536 1

2 3

4 6

April-May 2023 AV121 - Data Structures and Algorithms 63


Trees – Reconstruction from Traversals
 Traversals
→ Preorder 124536 1

1 2 3

4 6
2 3

4 5 6 5

April-May 2023 AV121 - Data Structures and Algorithms 64


Trees – Reconstruction from Traversals
 Traversals 1
→ Inorder 425163

2 3
→ Preorder 124536

4 5 6

April-May 2023 AV121 - Data Structures and Algorithms 65


Trees – Reconstruction from Traversals
 Traversals 1
→ Inorder 425163

→ Preorder 124536

Root

April-May 2023 AV121 - Data Structures and Algorithms 66


Trees – Reconstruction from Traversals
 Traversals 1
→ Inorder 425163

→ Preorder 124536 4 2 5 6 3

Root

April-May 2023 AV121 - Data Structures and Algorithms 67


Trees – Reconstruction from Traversals
 Traversals 1
→ Inorder 425163

2
→ Preorder 124536 6 3

Root of left 4 5
sub-tree

April-May 2023 AV121 - Data Structures and Algorithms 68


Trees – Reconstruction from Traversals
 Traversals 1
→ Inorder 425163

2 3
→ Preorder 124536

Root of right
4 5 6
sub-tree

April-May 2023 AV121 - Data Structures and Algorithms 69


Trees – Representing
Index 1 2 3 4 5 6 1
 Arrays
Parent 0 1 1 2 2 3
→ Index represents
element and content 2 3
points to parent
 Linked Lists 1 2 3 φ
4 5 6
2 4 5 φ
→ Array of parent
nodes, with each 3 6 φ
node pointing to list 4 φ
of children 5 φ
6 φ 1
 Left-most Child, Right
Sibling
2 3

4 5 6

April-May 2023 AV121 - Data Structures and Algorithms 70


Questions?

April-May 2023 AV121 - Data Structures and Algorithms 71


Lecture 01 – Review

 Sets, Operations
 Trees
 Traversals – Inorder, Preorder, Postorder
 Representing mathematical expressions
 Reconstruction of trees from traversals
 Representing trees

April-May 2023 AV121 - Data Structures and Algorithms 72

You might also like