introduction to data structure & algorithms - sethuonline.com | sathyabama university chennai
TRANSCRIPT
Data Structure & Algorithms
Agenda
• Data Structure Overview• Binary Trees • Binary Search Trees • Binary Search Tree Traversal • Binary Tree, BST Questions • Graph Overview • Problems & Questions
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
Analogy
Algorithm Analysis
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.
Types Of Data Structure
Data Structure
Linear DS Non Linear DS
Binary Trees
Binary Trees
Binary Trees Jargons
Binary Trees Jargons
Binary Tree Vs Tree
Binary Tree Terminology
Binary Tree Terminology
Binary Tree Terminology
Binary Tree Terminology
Binary Tree Terminology
Binary Tree Terminology
Binary Tree Terminology
Binary Tree Terminology
Binary Search Tree
Binary Search Tree Examples
Binary Search Tree Examples
Binary Search Tree Examples
Binary Search Tree Examples
Binary Search Tree Examples
Building a Binary Search Tree
Removal Binary Search Tree
Removal Binary Search Tree
Removal Binary Search Tree
Binary Search Tree Traversal
BST Traversal
BFS DFS
Binary Search Tree Traversal
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
Questions
2) The number of possible ordered trees with 3 nodes A,B,C is
a) 16 (b)12 c)6 (d)10
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
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
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
Questions
(b) Inorder traversal visits the nodes in ascending order as it is left root right in which left<=root<=right
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
Questions
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
Questions The post order traversal of a binary tree is DEBFCA. Find out the preorder traversal.(a) ABFCDE (b) ADBFEC (c) ABDECF
d) ABDCEF
Questions
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
Questions
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
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
Questions How many nodes in a tree have no ancestors.(A)0 B) 1 (C) 2 (D) n
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
Questions
Introduction to Graph
Introduction to Graph
Introduction to Graph
Introduction to Graph
Introduction to Graph
Introduction to Graph
Introduction to Graph
• Bob has three friends not friends with Rama that can be suggested to Rama
Introduction to Graph
• Lee has one friend(Swati) not friends with Rama so totally 4 friends can be suggested to Rama
Introduction to Graph
• Lee has one friend(Swati) not friends with Rama so totally 4 friends can be suggested to Rama
Introduction to Graph
• Directed Graph
Introduction to Graph