introduction to data structure & algorithms - sethuonline.com | sathyabama university chennai

61
Data Structure & Algorithms

Upload: sethuraman-r

Post on 11-Feb-2017

403 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Data Structure & Algorithms

Page 2: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

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

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

Analogy

Page 5: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Algorithm Analysis

Page 6: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

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

Types Of Data Structure

Data Structure

Linear DS Non Linear DS

Page 8: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Trees

Page 9: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Trees

Page 10: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Trees Jargons

Page 11: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Trees Jargons

Page 12: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Tree Vs Tree

Page 13: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Tree Terminology

Page 14: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Tree Terminology

Page 15: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Tree Terminology

Page 16: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Tree Terminology

Page 17: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Tree Terminology

Page 18: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Tree Terminology

Page 19: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Tree Terminology

Page 20: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Tree Terminology

Page 21: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Search Tree

Page 22: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Search Tree Examples

Page 23: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Search Tree Examples

Page 24: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Search Tree Examples

Page 25: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Search Tree Examples

Page 26: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Search Tree Examples

Page 27: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Building a Binary Search Tree

Page 28: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Removal Binary Search Tree

Page 29: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Removal Binary Search Tree

Page 30: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Removal Binary Search Tree

Page 31: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Search Tree Traversal

BST Traversal

BFS DFS

Page 32: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Binary Search Tree Traversal

Page 33: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

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

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

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

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

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

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

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

Questions

Page 41: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

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

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

Questions

Page 44: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

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

Questions

Page 46: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

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

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

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

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

Questions

Page 51: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Introduction to Graph

Page 52: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Introduction to Graph

Page 53: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Introduction to Graph

Page 54: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Introduction to Graph

Page 55: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Introduction to Graph

Page 56: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Introduction to Graph

Page 57: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

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

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

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

Introduction to Graph

• Directed Graph

Page 61: Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama University Chennai

Introduction to Graph