0% found this document useful (0 votes)
14 views67 pages

4 TREES

Uploaded by

Govind 025
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)
14 views67 pages

4 TREES

Uploaded by

Govind 025
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/ 67

DATA STRUCTURE

DR. ACHAL KAUSHIK

9/8/2024 1
Paper Code(s): CIC-209 L P C
Paper: Data Structures 4 - 4
Marking Scheme:
1. Teachers Continuous Evaluation: 25 marks
2. Term end Theory Examinations: 75 marks
Instructions for paper setter:
1. There should be 9 questions in the term end examinations question paper.
2. The first (1st) question should be compulsory and cover the entire syllabus. This question should be objective, single line answers or short
answer type question of total 15 marks.
3. Apart from question 1 which is compulsory, rest of the paper shall consist of 4 units as per the syllabus. Every unit shall have two questions
covering the corresponding unit of the syllabus. However, the student shall be asked to attempt only one of the two questions in the unit.
Individual questions may contain upto 5 sub-parts / sub-questions. Each Unit shall have a marks weightage of 15.
4. The questions are to be framed keeping in view the learning outcomes of the course / paper. The standard / level of the questions to be asked
should be at the level of the prescribed textbook.
5. The requirement of (scientific) calculators / log-tables / data – tables may be specified if required.
Course Objectives :
1. To introduce basics of Data structures (Arrays, strings, linked list etc.)
2. To understand the concepts of Stacks, Queues and Trees, related operations and their implementation
3. To understand sets, heaps and graphs
4. To introduce various Sorting and searching Algorithms
Course Outcomes (CO)
CO 1 To be able to understand difference between structured data and data structure
CO 2 To be able to create common basic data structures and trees
CO 3 To have a knowledge of sets, heaps and graphs
CO 4 To have basic knowledge of sorting and searching algorithms
Course Outcomes (CO) to Programme Outcomes (PO) mapping (scale 1: low, 2: Medium, 3: High)
PO01 PO02 PO03 PO04 PO05 PO06 PO07 PO08 PO09 PO10 PO11 PO12

CO 1 3 2 2 2 3 - - - 2 2 2 3
CO 2 3 2 2 2 3 - - - 2 2 2 3
CO 3 3 2 2 2 3 - - - 2 2 2 3
CO 4 3 2 2 2 3 - - - 2 2 2 3
UNIT – I
Overview of data structure, Basics of Algorithm Analysis including Running Time Calculations, Abstract Data Types, Arrays, Arrays and Pointers,
Multidimensional Array, String processing, General Lists and List ADT, List manipulations, Single, double and circular lists. Stacks and Stack ADT,
Stack Manipulation, Prefix, infix and postfix expressions, recursion. Queues and Queue ADT, Queue manipulation.

UNIT – II
Sparse Matrix Representation (Array and Link List representation) and arithmetic (addition, subtraction and multiplication), polynomials and
polynomial arithmetic.
Trees, Properties of Trees, Binary trees, Binary Tree traversal, Tree manipulation algorithms, Expression trees and their usage, binary search trees,
AVL Trees, Heaps and their implementation, Priority Queues, B-Trees, B* Tree, B+ Tree

UNIT – III
Sorting concept, order, stability, Selection sorts (straight, heap), insertion sort (Straight Insertion, Shell sort), Exchange Sort (Bubble, quicksort),
Merge sort (External Sorting) (Natural merge, balanced merge and polyphase merge). Searching – List search, sequential search, binary search,
hashing methods, collision resolution in hashing.

UNIT – IV
Disjoint sets representation, union find algorithm, Graphs, Graph representation, Graph Traversals and their implementations (BFS and DFS).
Minimum Spanning Tree algorithms, Shortest Path Algorithms

Textbook(s):
1. Richard Gilberg , Behrouz A. Forouzan, “Data Structures: A Pseudocode Approach with C, 2nd Edition, Cengage Learning, Oct 2004
2. E. Horowitz, S. Sahni, S. Anderson-Freed, "Fundamentals of Data Structures in C", 2nd Edition, Silicon Press (US), 2007.

References:
1. Mark Allen Weiss, “Data Structures and Algorithm Analysis in C”, 2nd Edition, Pearson, September, 1996
2. Robert Kruse, “Data Structures and Program Design in C”, 2nd Edition, Pearson, November, 1990
3. Seymour Lipschutz, “Data Structures with C (Schaum's Outline Series)”, McGrawhill, 2017
4. A. M. Tenenbaum, “Data structures using C”. Pearson Education, India, 1st Edition 2003.
5. Weiss M.A., “Data structures and algorithm analysis in C++”, Pearson Education, 2014.
Data Structure
• Data Structure is a way of collecting and organizing data in such a
way that we can perform operations on these data in an effective
way
• In simple language, Data Structures are structures programmed to
store ordered data, so that various operations can be performed on
it easily

9/8/2024 4
TREES

• Structure is mainly used to represent data containing a hierarchical


relationship between elements
• Tree consists of
❑finite set of elements, called NODES
❑Finite set of directed lines, connecting the nodes BRANCHES
❑Number of branches associated with a node is DEGREE of the node
❖Branch directed towards the node is INDEGREE
❖Branch directed away from the node is OUTDEGREE
❑Sum of indegree and outdegee is the degree of the node

9/8/2024 5
Tree: Degree

• First node is ROOT


• ROOT by definition has indegree zero
• Other nodes in a tree must have an
❑Indegree of exactly one (predecessor)
❑Outdegree zero, one or more (successor)

• LEAF is any node with an outdegee of zero


• Node that is not a root or leaf is known as INTERNAL Node

9/8/2024 6
Binary Tree Representation

B E

C D F G

9/8/2024 7
Tree: Nomenclature
• N is node in Tree T
• Left successor S1 (left child)
• Right successor S2 (right child)
• P is called parent of S1 and S2 (siblings)
• Predecessor, Ancestor (Parent)
• Descendant (child)

9/8/2024 8
Tree: Levels
• Each node in T, is assigned a level number
❑Distance from the node

• The root R of the tree T is assigned the level number 0


• And every other node is assigned a level number which is 1 more
than the level number of its parent
• Nodes having same level number are belongs to same generation

9/8/2024 9
Tree: Height (Depth)
• Depth (Height) = LEVEL + 1
• The depth of the tree T is evaluated from the branch of T having
maximum number of nodes

9/8/2024 10
Example

• LEVEL 0 A

• LEVEL 1 F
B E

• LEVEL 2

C D G H I HEIGHT: 3
ROOT: A SIBLINGS: {B, E, F} {C, D} {G, H, I}

PARENTS: A, B, F LEAVES: C, D, E, G, H, I

CHILDREN: B, E, F, C, D, G, H, I INTERNAL NODES: B, F

9/8/2024 11
BINARY TREES

• Binary tree is a tree in which no node can have more than two subtrees
(maximum outdegree = 2)
• Any node N in a binary tree T has either 0, 1 or 2 successors

9/8/2024 12
BINARY TREES
• Binary tree T is defined as
❑T is empty (NULL or Empty TREE), or
❑T contains a distinguished node R, called the Root of T
❑and the remaining nodes of T from an ordered pair of disjoint binary
trees T1 and T2
• T1 and T2 are respectively called left and right subtrees of R

9/8/2024 13
Binary Tree: Height
• Maximum height Hmax = N
• Minimum height Hmin = ⌊ log2N + 1 ⌋
• Minimum Nodes Nmin = H
• Maximum Nodes Nmax = 2 H – 1

B E

C D F G

9/8/2024 14
Complete Binary Trees
• T can have at most two children
• Level r of T can have at most 2r nodes
• The tree is complete if all its level except possibly the last, have the
maximum number of possible nodes (left to right)
• Depth DN = ⌊ log2N + 1 ⌋

9/8/2024 15
Extended Binary Trees: 2 Trees
• If each node N has either 0 or 2 children
• Nodes with 2 children are called internal nodes
• Nodes with 0 children are called external nodes

9/8/2024 16
Binary Tree Representation
• Linear Array
• Linked List

9/8/2024 17
Sequential representation of Tree

• The root R of T is stored in TREE[1]


• If a node N occupies Tree[k]
❑then its left child is stored in TREE[2*k]
❑and its right child is stored in Tree[2*k + 1]

• For index 0 it is (2*k + 1) & (2*k + 2)


• The sequential representation of a tree with depth d will require an
array with approximately 2d + 1 elements
• The sequential representation is usually inefficient unless, the
Binary tree T is complete or nearly complete

9/8/2024 18
Binary Tree Traversal
• Each node of the tree be processed once and only once in a
predetermined sequence
• Two Approaches:
• Depth first traversal (STACK)
❑Preorder traversal
❑Inorder traversal
❑Postorder traversal

• Breadth first traversal (QUEUE)

9/8/2024 19
Depth first Traversal
• Preorder (NLR)
1. Process the root N
2. Traverse the left subtree of N in preorder
3. Traverse the right subtree of N in preorder

Traversal: ABCDEFG
Algorithm preOrder (root)
1. If (root is not null)
1. process (root)
A
2. preOrder(leftSubtree)
3. preOrder(rightSubtree) B E
2. end if C D F G
end preOrder
9/8/2024
20
Depth first Traversal

• Inorder (LNR)
1. Traverse the left subtree of R in inorder
2. Process the root R
3. Traverse the right subtree of R in inorder

Traversal: CBDAFEG
Algorithm inOrder (root)
1. If (root is not null)
1. inOrder(leftSubtree)
2. process (root) A
3. inOrder(rightSubtree)
B E
2. end if
C D F G
end inOrder
9/8/2024 21
Depth first Traversal
• Postorder (LRN)
1. Traverse the left subtree of R in postorder
2. Traverse the right subtree of R in postorder
3. Process the root R

Traversal: CDBFGEA
Algorithm postOrder (root)
1. If (root is not null)
1. postOrder(leftSubtree)
2. postOrder(rightSubtree) A
3. process (root) B E
2. end if C D F G
end postOrder
9/8/2024 22
Pre-Order
• STEPS: (NON-RECURSIVE/ ITERATIVE)
• Initially push $ onto STACK
set PTR = ROOT
• Repeat while PTR != $
•{
• Process PTR
• if PTR->RIGHT !=NULL
• PUSH PTR->RIGHT
• end if
• if PTR->LEFT !=NULL A
• PTR= PTR->LEFT
• else B E
• POP: PTR = stackTOP
• end if C D F G
•}
TRAVERSAL: A B C D E F G
• STACK: NULL E D G
9/8/2024 23
Algebraic Expression

• Application of Binary tree is Expression Tree


❑Each leaf is an operand
❑The root and internal nodes are operators
❑Subtrees are subexpresssions, with the root being an operator

• Depth first traversal represents


❑Infix traversal results in Infix expression
❑Prefix traversal results in Prefix expression /
❑Postfix traversal results in Postfix expression
- +

a b * e
E = (a – b ) / (( c * d ) + e)
c d
9/8/2024 24
Traversal

• Infix
• Add open parenthesis at the beginning of each expression and a close
parenthesis at the end of each expression
❑Print open parenthesis when we start a tree or a subtree
❑Print close parenthesis when we processed all of its children

• Prefix same as preorder traversal +

• Postfix same as postorder traversal * d

a +

b c

9/8/2024 25
Breadth-first Traversal

• Process all of the children of a node (level n) before proceeding with the
next level (level n + 1)
• Move in horizontal fashion, first across the root level, then across level 1,
then across level 2 and so forth…
Traversal: ABECDFG
• STEPS:
A
1. Take an Empty Queue.
2. Start from the root, insert the root into the Queue
B E
3. Now while Queue is not empty,
A. Extract the node from the Queue and
B. insert all its children into the Queue C D F G
C. Print the extracted node.
9/8/2024 26
Tree?
• PreOrder A B D E F C G H J L K
• Inorder D B F E A G C L J H K

9/8/2024 27
Tree?
• Inorder D B F E A G C L J H K
• Postorder D F E B G L J K H C A

9/8/2024 28
Tree

• PreOrder A B D E F C G H J L K A
• Inorder D B F E A G C L J H K
B C

Or
D E G H

• Inorder D B F E A G C L J H K
F J K
• Postorder D F E B G L J K H C A

9/8/2024 29
Efficient Binary Trees
• Binary search trees (BST)
• AVL trees
• Heap
• B-tree
• B+-trees

9/8/2024 30
Binary Search Trees (BST)

• Ordered binary tree where nodes are arranged in an order


• All the nodes in the left subtree
❑have a value less than that of the root node

• All the nodes in the right subtree


❑ have a value either equal to or greater than that of the root node

• All the nodes are ordered so


❖ searching time is greatly reduced
❖ Insertion and deletion operations are also speed up

9/8/2024 31
Insert Node to BST

• All BST insertion take place at a leaf node or leaflike node (node
that has only one null subtree)
• Node with equal values are found in the right subtree.

9/8/2024 32
Binary tree

• Binary Tree

•39 27 45 18 29 40 54 28 43 41 54 60

9/8/2024 33
BST

• Binary Search Tree

•39 27 45 18 29 40 54 28 43 41 54 60

9/8/2024 34
BST

• Binary Search Tree


• 39 27 45 18 29 40 54 28 43 41 54 60

39

27 45

18 29 40 54

28 43 60

41

9/8/2024 35
Insert Node to BST

addBST(root, newNode)
1. if (empty tree)
i. set root to newNode
ii. return newNode
2. end if
3. // Locate null subtree for insertion
4. if (newNode < root)
i. return addBST(left subtree, newNode)
5. else
i. Return addBST (right subtree, newNOde)
6. end if
end BST

9/8/2024 36
Find Smallest Node in a BST
findSmallestBST(root)
• if (left subtree empty)
• Return (root)
• end if
• Return findSmallestBST(left subtree)
end findSmallestBST

9/8/2024 37
Find Largest Node in a BST
findLargestBST(root)
1. if (right subtree empty)
1. Return (root)
2. end if
3. Return findLargestBST(right subtree)
end findLargestBST

9/8/2024 38
Search BST
searchBST (root, key)
1. if (empty tree)
i. Not Found
2. end if
3. if (key < root)
i. return searchBST (left subtree, key)
4. else if (key > root)
i. return searchBST (right subtree, key)
5. else
i. Found key
ii. Return root
6. end if
end searchBST
9/8/2024 39
Delete Node from BST
• Delete a node from BST have 4 possible cases:
1. Node to be deleted has no children
❖Delete the node

2. Node to be deleted has only right subtree


❖Delete the node and attach the right subtree to the deleted node’s parent

3. Node to be deleted has only left subtree


❖Delete the node and attach the left subtree to the deleted node’s parent

4. Node to be deleted has two subtrees


❖Replace the node with
• Either the largest node in the left subtree of the deleted node
• Or the smallest node in the right subtree of the deleted node

9/8/2024 40
Delete Node from BST
deleteBST (root, key)
1. if empty (tree)
i. return false
2. end if
3. if (key < root)
i. return deleteBST(left subtree, key)
4. else if (key > root)
i. return deleteBST (right subtree, key)
5. else
//delete node found- test for leaf node
i. if (no left subtree)
a. make right subtree the root
b. return true
9/8/2024 41
Delete Node from BST cont..
ii. else if (no right subtree)
a. make left subtree the root
b. return true
iii. else
//Node to be deleted not a leaf find the largest node on left subtree
a. Save root in deleteNode
b. Set largest to largestBST // (left subtree)
c. Move data in largest to deleteNode
d. Return deleteBST (left subtree of deleteNode, key of largest)
e. end if

6. end if
end deleteBST

9/8/2024 42
AVL Trees
• G.M.Adelson-Velskii and E.M.Landis : Balanced Binary tree
• Height of the subtrees differ by no more than 1
• | HL – HR | <= 1

9/8/2024 43
AVL Trees: Insertion
• Four cases require rebalancing
1. Left of Left (LL Rotation)- a subtree of a tree that is left high has
also become left high
2. Right of Right (RR Rotation)- a subtree of a tree that is right
high has also become right high
3. Right of Left (LR Rotation)- a subtree of a tree that is left high
has become right high
4. Left of Right (RL Rotation)- a subtree of a tree that is right high
has become left high

9/8/2024 44
AVL Trees: Insertion

1. LL Rotation- The new node is inserted in the left subtree of the left subtree
of the critical node
2. RR Rotation- The new node is inserted in the right subtree of the right
subtree of the critical node
3. LR Rotation- The new node is inserted in the right subtree of the left
subtree of the critical node
4. RL Rotation- The new node is inserted in the left subtree of the right
subtree of the critical node

9/8/2024 45
Example
• Mar May Nov Aug Apr Jan Dec July Feb June Oct Sep

• Step 1:
Mar

• Step 2:
Mar Mar

May May
• Step 3: BF(Mar) = -2
• RR Rotation
Nov
9/8/2024 46
Mar May Nov Aug Apr Jan Dec July Feb June Oct Sep

• Step 3A: RR Rotation

May

Mar Nov May


• Step 4:

Mar Nov

9/8/2024 Aug 47
Mar May Nov Aug Apr Jan Dec July Feb June Oct Sep

• Step 5: BF(Mar)= +2
May
• LL Rotation

Mar Nov

Aug

Apr

9/8/2024 48
Mar May Nov Aug Apr Jan Dec July Feb June Oct Sep

• Step 5A:
• LL Rotation May

Aug Nov

Apr Mar

9/8/2024 49
Mar May Nov Aug Apr Jan Dec July Feb June Oct Sep

• Step 6: BF(May) = +2
• LR Rotation May

Aug Nov

Apr Mar

Jan

9/8/2024 50
Mar May Nov Aug Apr Jan Dec July Feb June Oct Sep

• Step 6A:
May
• L Rotation
Mar Nov
Aug
Apr Jan

• Step 6B:
• R Rotation Mar
Aug May
Apr
Jan Nov

9/8/2024 51
Mar May Nov Aug Apr Jan Dec July Feb June Oct Sep

• Step 7:
Mar
Aug May
Apr
Jan Nov
• Step 8:
Dec

Mar
Aug May
Apr
Jan Nov

9/8/2024 Dec Jul 52


Mar May Nov Aug Apr Jan Dec July Feb June Oct Sep

Mar
Aug May
• Step 9: BF(Aug)= -2 Apr
Jan Nov
• RL Rotation
Dec Jul

Feb
• Step 9A:
• RL Rotation Mar
Dec May
Aug
Jan Nov
Apr
9/8/2024 Feb Jul 53
Mar May Nov Aug Apr Jan Dec July Feb June Oct Sep
• Step 10: BF(Mar)= +2
Mar
• LR Rotation
Dec May
Aug
Jan Nov
Apr
Feb Jul

Jun
• Step 10A:
• LR Rotation
Jan
Dec Mar
Aug Feb
Jul May
Apr
9/8/2024
Jun Nov
54
Mar May Nov Aug Apr Jan Dec July Feb June Oct Sep
• Step 11: BF(May)= -2
Jan
• RR Rotation
Dec Mar
Aug Feb
Jul May
Apr
Jun Nov

Jan Oct
• Step 11A:
Dec Mar
• RR Rotation
Aug Feb Jul Nov
Apr
Jun May Oct

9/8/2024 55
Mar May Nov Aug Apr Jan Dec July Feb June Oct Sep

• Step 12:

Jan

Dec Mar

Aug Feb Nov


Jul
Apr
Jun May Oct

Sep
9/8/2024 56
AVL Tree
•B HAGW N P RSU M D E LI

9/8/2024 57
HEAP

• Tree is Complete or nearly complete


• Max heap: Root is maximum
• Left and Right subtree have smaller value than that of the root node

• Min heap: Root is minimum


• Left and Right subtree have larger values than that of the root node

9/8/2024 58
Max-Heap Insertion: Re-heap Up

• Insert at heap takes place at leaf node (last leaf level at the first empty
position)
If inserted node value is
❑smaller than its parent – Heap
Else
❑– Reheap Up

• Reheap up repair the structure by exchanging the parent and child


key

9/8/2024 59
Addition of 25
42
• Original Tree: not a heap 21 32
16 12 20 30
42 13 5 10 25
21 32
16 25 20 30 42
13 5 10 12 25 32
16 21 20 30
13 5 10 12

9/8/2024 60
Max-Heap Deletion: Re-heap
Down

• Delete at heap takes place at Root node


Move the data of the last leaf node at the root
If root > Subtree
❑Larger than its children – Heap
Else
❑– Reheap Down

• Reheap Down repair the structure by exchanging the parent and


Larger child key value

9/8/2024 61
Deletion of 42
42
• Original Tree: not a heap 25 32
16 21 20 30
12 13 5 10 12
21 32
16 32
25 20 30
25 12
13 5 10 16 21 20 30
32 13 5 10
25 30
16 21 20 12
9/8/2024
13 5
62
10
Heap Implementation

• Either through Dynamic tree structure or using Arrays


• Mostly Array implementation used as complete or nearly complete
❑Relationship between node and children is fixed

• For a node located at index i, its children are found at:


❑Left child: 2i + 1
❑Right child: 2i + 2

• Parent of a node located at index i location at ⌊ (i - 1)/ 2 ⌋


• Given index for a left child, j, its right Sibling (if found) at j + 1
• Given index for a right child, k, its left Sibling (if found) at k - 1

9/8/2024 63
Max-heap operations
• Initialization:
• Create an empty array to represent the max-heap.
• Insertion (Heapify Up):
• To insert an element, add it to the end of the array.
• Compare the newly added element with its parent.
• If the element is greater than its parent, swap them.
• Repeat this process until the element is in its correct position.

• Extraction (Heapify Down):


• To extract maximum element, remove the root (the first element in the array).
• Replace the root with the last element in the array.
• Compare this element with its children.
• Swap it with the larger child if needed.
• Repeat this process until the element is in its correct position.

9/8/2024 64
Example
• Max heap
• 34, 45, 42, 56, 78, 4, 99, 32, 55, 90, 88

9/8/2024 65
Priority Queue
• Abstract data type where each element has an associated priority
• Elements with
• higher priority are dequeued (removed) before those with lower priority
• Priority queues are used in various applications like
• scheduling tasks, shortest path algorithms, and more
• A priority queue implemented using a binary heap is a common and
efficient data structure

9/8/2024 66
Priority Queue (Min-Heap)
Example:
• Suppose you have a to-do list with tasks and associated priorities
• Implement a priority queue using a min-heap to manage these
tasks:
1.Initially, priority queue (min-heap) is empty
2.You add tasks along with their priorities to the min-heap:
1. Task: "Finish the report" (Priority: 4)
2. Task: "Attend a meeting" (Priority: 2)
3. Task: "Check emails" (Priority: 6)
4. Task: "Prepare presentation" (Priority: 3)
5. Task: "Submit expense report" (Priority: 1)

9/8/2024 67

You might also like