Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai
-
Upload
sethuraman-r -
Category
Software
-
view
405 -
download
2
Transcript of Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai
![Page 1: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/1.jpg)
Data Structure & Algorithms
![Page 2: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/2.jpg)
Agenda
• Data Structure Overview• Binary Trees • Binary Search Trees • Binary Search Tree Traversal • Binary Tree, BST Questions • Graph Overview • Problems & Questions
![Page 3: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/3.jpg)
What Is Data Structure?
Program Contains DataWhere the data to be stored?(Capacity)How the data efficiently handle & used? (Efficiently)Data structure just a plain container
![Page 4: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/4.jpg)
Analogy
![Page 5: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/5.jpg)
Algorithm Analysis
![Page 6: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/6.jpg)
Algorithm & Datastructure
A Program is a combination of Algorithm and Data Structure
Simple Example:Addition of two Numbers Int A,B,CC= A + B
Program Algorithm Data Structure
Addition Logic is an AlgorithmA & B are Data Structure ‘ elements.
![Page 7: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/7.jpg)
Types Of Data Structure
Data Structure
Linear DS Non Linear DS
![Page 8: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/8.jpg)
Binary Trees
![Page 9: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/9.jpg)
Binary Trees
![Page 10: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/10.jpg)
Binary Trees Jargons
![Page 11: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/11.jpg)
Binary Trees Jargons
![Page 12: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/12.jpg)
Binary Tree Vs Tree
![Page 13: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/13.jpg)
Binary Tree Terminology
![Page 14: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/14.jpg)
Binary Tree Terminology
![Page 15: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/15.jpg)
Binary Tree Terminology
![Page 16: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/16.jpg)
Binary Tree Terminology
![Page 17: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/17.jpg)
Binary Tree Terminology
![Page 18: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/18.jpg)
Binary Tree Terminology
![Page 19: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/19.jpg)
Binary Tree Terminology
![Page 20: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/20.jpg)
Binary Tree Terminology
![Page 21: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/21.jpg)
Binary Search Tree
![Page 22: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/22.jpg)
Binary Search Tree Examples
![Page 23: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/23.jpg)
Binary Search Tree Examples
![Page 24: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/24.jpg)
Binary Search Tree Examples
![Page 25: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/25.jpg)
Binary Search Tree Examples
![Page 26: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/26.jpg)
Binary Search Tree Examples
![Page 27: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/27.jpg)
Building a Binary Search Tree
![Page 28: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/28.jpg)
Removal Binary Search Tree
![Page 29: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/29.jpg)
Removal Binary Search Tree
![Page 30: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/30.jpg)
Removal Binary Search Tree
![Page 31: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/31.jpg)
Binary Search Tree Traversal
BST Traversal
BFS DFS
![Page 32: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/32.jpg)
Binary Search Tree Traversal
![Page 33: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/33.jpg)
Questions
1) Consider the following tree:
If the post order traversal gives a b – c d * + then the label of the nodes 1,2,3,4,5,6,7 respectively are a) +, -, *, a, b, c, d (b) a, b, c, d, -, *, +(c) a, -, b, +, c, *, d (d) -, a, b, +, *, c, d
![Page 34: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/34.jpg)
Questions
2) The number of possible ordered trees with 3 nodes A,B,C is
a) 16 (b)12 c)6 (d)10
![Page 35: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/35.jpg)
Questions
3) The depth of a complete binary tree with n nodes is (log is to the base two)a) Log (n+1) -1 (b) Log (n)(c)Log (n-1) +1 (d) Log (n) +1
![Page 36: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/36.jpg)
Questions (a)Explanation: If the depth of a complete binary tree is d, the number of nodes n= 2d+1-1n+1=2d+1 Applying logarithm to base 2 on both sides we havelog 2 (n+1) = d+1(log 2 2)Therefore d= log 2 (n+1) -1 Total no. of nodes n = (2^(d+1))-1
log (n+1) = d+1 d = log(n+1)-1
![Page 37: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/37.jpg)
Questions
Which of the following traversal technique lists the nodes of a binary search tree in ascending order?(a)Post-order b) In-order© Pre-order (d) None of the above
![Page 38: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/38.jpg)
Questions
(b) Inorder traversal visits the nodes in ascending order as it is left root right in which left<=root<=right
![Page 39: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/39.jpg)
Questions
The number of binary trees with 3 nodes which when traversed in post order gives the sequence A,B,C is(a)3 (b) 9 (c) 7 d) 5
![Page 40: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/40.jpg)
Questions
![Page 41: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/41.jpg)
Questions A binary search tree contains the values 1,2,3,4,5,6,7 and 8. The tree is traversed in preorder and the values are printed out. Which of the following sequences is a valid output
(a)5 3 1 2 4 7 8 6 (b) 5 3 1 2 6 4 9 7(c)5 3 2 4 1 6 7 8 d) 5 3 1 2 4 7 6 8
![Page 42: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/42.jpg)
Questions The post order traversal of a binary tree is DEBFCA. Find out the preorder traversal.(a) ABFCDE (b) ADBFEC (c) ABDECF
d) ABDCEF
![Page 43: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/43.jpg)
Questions
![Page 44: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/44.jpg)
Questions If a node having two children is deleted from a binary tree, it is replaced by its(A)Inorder predecessor B) Inorder successor(C)Preorder predecessor (D) None of the above
![Page 45: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/45.jpg)
Questions
![Page 46: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/46.jpg)
Questions .The number of leaf nodes in a complete binary tree of depth d isA) 2d (B) 2d–1+1 (C) 2d+1+1 (D) 2d+1
![Page 47: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/47.jpg)
Questions . The pre-order and post order traversal of a Binary Tree generates the same output. The tree can have maximum(A)Three nodes (B) Two nodesC) One node (D) Any number of nodes
![Page 48: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/48.jpg)
Questions How many nodes in a tree have no ancestors.(A)0 B) 1 (C) 2 (D) n
![Page 49: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/49.jpg)
Questions In order to get the information stored in a Binary Search Tree in the descending order, one should traverse it in which of the following order?
(A) left, root, right (B) root, left, rightC) right, root, left (D) right, left, root
![Page 50: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/50.jpg)
Questions
![Page 51: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/51.jpg)
Introduction to Graph
![Page 52: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/52.jpg)
Introduction to Graph
![Page 53: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/53.jpg)
Introduction to Graph
![Page 54: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/54.jpg)
Introduction to Graph
![Page 55: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/55.jpg)
Introduction to Graph
![Page 56: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/56.jpg)
Introduction to Graph
![Page 57: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/57.jpg)
Introduction to Graph
• Bob has three friends not friends with Rama that can be suggested to Rama
![Page 58: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/58.jpg)
Introduction to Graph
• Lee has one friend(Swati) not friends with Rama so totally 4 friends can be suggested to Rama
![Page 59: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/59.jpg)
Introduction to Graph
• Lee has one friend(Swati) not friends with Rama so totally 4 friends can be suggested to Rama
![Page 60: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/60.jpg)
Introduction to Graph
• Directed Graph
![Page 61: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai](https://reader035.fdocuments.in/reader035/viewer/2022081511/589e198b1a28ab605b8b5461/html5/thumbnails/61.jpg)
Introduction to Graph