AV121 DSA KJ Lecture 01 Sets Trees
AV121 DSA KJ Lecture 01 Sets Trees
Kurian John
Lecture 01
Lecture 01 – Plan
Introduction
{5}
{7, 5, 8}
{}
→ Union
→ Intersection
→ Difference
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
→ List
When the size of the set is unknown / dynamic
Sorted set
→ Extension of set, when elements are ordered (Elements come
from a comparable class)
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
All other nodes are said to be internal nodes, that is, they
are internal to the tree
Order: A B H C D G I E F J K
Order: A B D E C F G
Order: D E B F G C A
Order: D B E A F C G
(6-4) * (3 + (5-1))
(6-4) * (3 + (5-1))
→ Preorder *– 64+3–51
(6-4) * (3 + (5-1))
→ Preorder *– 64+3–51
→ Postorder 64–351-+*
(6-4) * (3 + (5-1))
Prefix
→ Preorder *– 64+3–51
Postfix
→ Postorder 64–351-+*
(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
→ 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
→ 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
→ 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
→ 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
→ 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
→ 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
→ 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
→ 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
→ 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
→ 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
→ 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
→ 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
→ 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
2 3
→ Preorder
4 5 6
→ Postorder
2 3
→ Preorder 124536
4 5 6
→ Postorder 452631
2 3
4 6
1 2 3
4 6
2 3
4 5 6 5
2 3
→ Preorder 124536
4 5 6
→ Preorder 124536
Root
→ Preorder 124536 4 2 5 6 3
Root
2
→ Preorder 124536 6 3
Root of left 4 5
sub-tree
2 3
→ Preorder 124536
Root of right
4 5 6
sub-tree
4 5 6
Sets, Operations
Trees
Traversals – Inorder, Preorder, Postorder
Representing mathematical expressions
Reconstruction of trees from traversals
Representing trees