Data structures (introduction)
Click here to load reader
-
Upload
arvind-devaraj -
Category
Technology
-
view
5.611 -
download
0
Transcript of Data structures (introduction)
![Page 1: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/1.jpg)
Data Structures
Arvind Devaraj
![Page 2: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/2.jpg)
Data Structure Selection
• What data structure will you use in the following Scenarios
![Page 3: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/3.jpg)
Ten people are standing in a row.
Every third person is eliminated.
Which person remains
Which Data structure ? Array, List, Tree, Stack, Queue ?????
SCENARIO-1
![Page 4: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/4.jpg)
SCENARIO 2
Recent 5 missed calls must be displayed
Which Data structure ? Array, List, Tree, Stack, Queue ?????
![Page 5: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/5.jpg)
SCENARIO 3
• Need to maintain database of student names…support for – Insert, Delete, Search– (also) next and previous person– (also) find who got maximum marks
Which Data structure ? Array, List, Tree, Stack, Queue ?????
![Page 6: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/6.jpg)
SCENARIO 4
Each person is assigned a priority number when he enters an office. The person who has the highest priority is served
Each process is having priority. The OS schedules the one having the highest priority
Which Data structure ? Array, List, Tree, Stack, Queue ?????
![Page 7: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/7.jpg)
Arrays (vs List)
+ Fast Access + Memory consumption is minimal- Insertion and deletion is complicated
![Page 8: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/8.jpg)
Linked Lists
• A way of making variable length arrays– In which insertions and deletions are easy
• But nothing comes for free– Finding an element can be slow– Extra space is needed for the links
![Page 9: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/9.jpg)
Linked List Example
Jill Joe Tom
first
Public static main (String[] argv) {Student first;
…}
Public class Student {String name;public Student next;
}
![Page 10: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/10.jpg)
Trees
• Linked list with multiple next elementsBinary trees are useful for relationships like “<“ ,
“>”• Insertions and deletions are easy• Useful for fast searching of large
collections
![Page 11: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/11.jpg)
Binary Tree Example
Jill
Joe
Tom
root Public class Student {int String name;public Student left;public Student right;
}
![Page 12: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/12.jpg)
Write a program
• height of binary tree• Count leaves• Find minimum value• Locate a particular node• Find successor of a node• Check if it’s a Binary Search tree
![Page 13: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/13.jpg)
![Page 14: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/14.jpg)
int height( BinaryTree Node t) {
if t is a null treereturn -1;
hl = height( left subtree of t);hr = height( right subtree of t);h = 1 + maximum of hl and hr;
return h;}
![Page 15: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/15.jpg)
Hashtables
• Find an element nearly as fast as in an array– With easy insertion and deletion– But without the ability to keep things in order
• Fairly complex to implement– But Java defines a class to make it simple
• Helpful to understand how it works– “One size fits all” approaches are inefficient
![Page 16: Data structures (introduction)](https://reader038.fdocuments.us/reader038/viewer/2022100600/554f7bb0b4c905435d8b470e/html5/thumbnails/16.jpg)
Choosing a Data Structure
• What operations do you need to perform?• Hashing finds single elements quickly
– But cannot preserve order
• Stacks and linked lists preserve order easily– But they can only read one element at any time
• Balanced trees are best when you need both– Need to read in blocks (for disks) – B-Tree is used