Download presentation
Presentation is loading. Please wait.
Published byHarry Holland Modified over 9 years ago
1
Lecture1 introductions and Tree Data Structures 11/12/20151
2
Definition A File Structure is a combination of representations for data in files and of operations for accessing the data. A File Structure allows applications to read, write and modify data. It might also support finding the data that matches some search criteria or reading through the data in some particular order. 211/12/2015
3
Computer Data can be stored in three kinds of locations: -Primary Storage ==> Memory [Computer Memory] -Secondary Storage :Cd, DVD, Hard drives, flash memory -Tertiary Storage ==>USB Flash drivers, smart card, external hard drive Data Storage 11/12/20153
4
Goal of file structure Minimize number of trips to the disk in order to get desired information. Grouping related information so that we are likely to get everything we need with only one trip to the disk. 11/12/20154
5
History of file structure design In the beginning… it was the tap – Sequential access If we need to access a file sequentially—that is, one record after another, from beginning to end—we use a If we need to access a file sequentially—that is, one record after another, from beginning to end—we use a sequential file structure. 11/12/20155
6
– Random access If we need to access a specific record without having to retrieve all records before it, we use a file structure that allows random access. Indexed file :Is a file structure allow the Random access, thus to access a record in a file randomly, we need to know the address of the record. History of file structure design 11/12/20156
7
Logical view of an indexed file 11/12/20157
8
History of file structure design As file grows we have the same problem we had with a large primary file Tree structures emerged for main memory (1960`s) 11/12/20158
9
Trees Data Structures Tree Nodes Each node can have 0 or more children A node can have at most one parent Binary tree Tree with 0–2 children per node TreeBinary Tree 11/12/20159
10
Trees Terminology Root no parent Leaf no child Interior non-leaf Height distance from root to leaf Root node Leaf nodes Interior nodes Height 11/12/201510
11
Binary Search Trees Key property Value at node Smaller values in left subtree Larger values in right subtree Example X > Y X < Z Y X Z 11/12/201511
12
Binary Search Trees Examples Binary search trees Not a binary search tree 5 10 30 22545 5 10 45 22530 5 10 30 2 25 45 11/12/201512
13
Binary Tree Implementation Class Node { int data; // Could be int, a class, etc Node *left, *right; // null if empty void insert ( int data ) { … } void delete ( int data ) { … } Node *find ( int data ) { … } … } 11/12/201513
14
Iterative Search of Binary Tree Node *Find( Node *n, int key) { while (n != NULL) { if (n->data == key) // Found it return n; if (n->data > key) // In left subtree n = n->left; else // In right subtree n = n->right; } return null; } Node * n = Find( root, 5); 11/12/201514
15
Recursive Search of Binary Tree Node *Find( Node *n, int key) { if (n == NULL) // Not found return( n ); else if (n->data == key) // Found it return( n ); else if (n->data > key) // In left subtree return Find( n->left, key ); else // In right subtree return Find( n->right, key ); } Node * n = Find( root, 5); 11/12/201515
16
Example Binary Searches Find ( root, 2 ) 5 10 30 22545 5 10 30 2 25 45 10 > 2, left 5 > 2, left 2 = 2, found 5 > 2, left 2 = 2, found root 11/12/201516
17
Example Binary Searches Find (root, 25 ) 5 10 30 22545 5 10 30 2 25 45 10 < 25, right 30 > 25, left 25 = 25, found 5 < 25, right 45 > 25, left 30 > 25, left 10 < 25, right 25 = 25, found 11/12/201517
18
Types of Binary Trees Degenerate – only one child Complete – always two children Balanced – “mostly” two children more formal definitions exist, above are intuitive ideas Degenerate binary tree Balanced binary tree Complete binary tree 11/12/201518
19
Binary Trees Properties Degenerate Height = O(n) for n nodes Similar to linked list Balanced Height = O( log(n) ) for n nodes Useful for searches Degenerate binary tree Balanced binary tree 11/12/201519
20
Binary Search Properties Time of search Proportional to height of tree Balanced binary tree O( log(n) ) time Degenerate tree O( n ) time Like searching linked list / unsorted array 11/12/201520
21
Binary Search Tree Construction How to build & maintain binary trees? Insertion Deletion Maintain key property (invariant) Smaller values in left subtree Larger values in right subtree 11/12/201521
22
Binary Search Tree – Insertion Algorithm 1. Perform search for value X 2. Search will end at node Y 3. If X < Y, insert new leaf X as new left subtree for Y 4. If X > Y, insert new leaf X as new right subtree for Y Observations O( log(n) ) operation for balanced tree Insertions may unbalance tree 11/12/201522
23
Example Insertion Insert ( 20 ) 5 10 30 22545 10 < 20, right 30 > 20, left 25 > 20, left Insert 20 on left 20 11/12/201523
24
Binary Search Tree – Deletion Algorithm 1. Perform search for value X 2. If X is a leaf, delete X 3. Else // must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree Observation O( log(n) ) operation for balanced tree Deletions may unbalance tree 11/12/201524
25
Example Deletion (Leaf) Delete ( 25 ) 5 10 30 22545 10 < 25, right 30 > 25, left 25 = 25, delete 5 10 30 245 11/12/201525
26
Example Deletion (Internal Node) Delete ( 10 ) 5 10 30 22545 5 5 30 22545 2 5 30 22545 Replacing 10 with largest value in left subtree Replacing 5 with largest value in left subtree Deleting leaf 11/12/201526
27
Example Deletion (Internal Node) Delete ( 10 ) 5 10 30 22545 5 25 30 22545 5 25 30 245 Replacing 10 with smallest value in right subtree Deleting leafResulting tree 11/12/201527
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.