Lecture1 introductions and Tree Data Structures 11/12/20151.
-
Upload
harry-holland -
Category
Documents
-
view
225 -
download
0
Transcript of Lecture1 introductions and Tree Data Structures 11/12/20151.
![Page 1: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/1.jpg)
Lecture1 introductions and Tree Data Structures
04/20/23 1
![Page 2: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/2.jpg)
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.
204/20/23
![Page 3: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/3.jpg)
•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
04/20/23 3
![Page 4: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/4.jpg)
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.
04/20/23 4
![Page 5: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/5.jpg)
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 If we need to access a file sequentially—that is, one record after another, from beginning to end—we record after another, from beginning to end—we use a use a sequential file structure.
04/20/23 5
![Page 6: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/6.jpg)
– Random accessIf 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
04/20/23 6
![Page 7: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/7.jpg)
Logical view of an indexed file04/20/23 7
![Page 8: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/8.jpg)
History of file structure designAs file grows we have the same problem we had with a large primary file
Tree structures emerged for main memory (1960`s)
04/20/23 8
![Page 9: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/9.jpg)
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
Tree Binary Tree04/20/23 9
![Page 10: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/10.jpg)
Trees
Terminology Root no parent Leaf no child Interior non-leaf Height distance from root to leaf
Root node
Leaf nodes
Interior nodes Height
04/20/23 10
![Page 11: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/11.jpg)
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
04/20/23 11
![Page 12: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/12.jpg)
Binary Search Trees Examples
Binary search trees
Not a binary search tree
5
10
30
2 25 45
5
10
45
2 25 30
5
10
30
2
25
45
04/20/23 12
![Page 13: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/13.jpg)
Binary Tree Implementation
Class Node {int data; // Could be int, a class, etcNode *left, *right; // null if empty
void insert ( int data ) { … }void delete ( int data ) { … }Node *find ( int data ) { … }
…}
04/20/23 13
![Page 14: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/14.jpg)
Iterative Search of Binary TreeNode *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);
04/20/23 14
![Page 15: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/15.jpg)
Recursive Search of Binary TreeNode *Find( Node *n, int key) {
if (n == NULL) // Not foundreturn( n );
else if (n->data == key) // Found itreturn( n );
else if (n->data > key) // In left subtreereturn Find( n->left, key );
else // In right subtreereturn Find( n->right, key );
}Node * n = Find( root, 5);
04/20/23 15
![Page 16: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/16.jpg)
Example Binary Searches Find ( root, 2 )
5
10
30
2 25 45
5
10
30
2
25
45
10 > 2, left
5 > 2, left
2 = 2, found
5 > 2, left
2 = 2, found
root
04/20/23 16
![Page 17: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/17.jpg)
Example Binary Searches Find (root, 25 )
5
10
30
2 25 45
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
04/20/23 17
![Page 18: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/18.jpg)
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
04/20/23 18
![Page 19: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/19.jpg)
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
04/20/23 19
![Page 20: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/20.jpg)
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
04/20/23 20
![Page 21: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/21.jpg)
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
04/20/23 21
![Page 22: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/22.jpg)
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
04/20/23 22
![Page 23: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/23.jpg)
Example Insertion
Insert ( 20 )
5
10
30
2 25 45
10 < 20, right
30 > 20, left
25 > 20, left
Insert 20 on left
20
04/20/23 23
![Page 24: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/24.jpg)
Binary Search Tree – Deletion Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal nodea) Replace with largest value Y on left subtree OR smallest value Z on right subtreeb) Delete replacement value (Y or Z) from subtree
Observation O( log(n) ) operation for balanced tree Deletions may unbalance tree
04/20/23 24
![Page 25: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/25.jpg)
Example Deletion (Leaf)
Delete ( 25 )
5
10
30
2 25 45
10 < 25, right
30 > 25, left
25 = 25, delete
5
10
30
2 45
04/20/23 25
![Page 26: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/26.jpg)
Example Deletion (Internal Node) Delete ( 10 )
5
10
30
2 25 45
5
5
30
2 25 45
2
5
30
2 25 45
Replacing 10 with largest value in left
subtree
Replacing 5 with largest value in left
subtree
Deleting leaf
04/20/23 26
![Page 27: Lecture1 introductions and Tree Data Structures 11/12/20151.](https://reader035.fdocuments.net/reader035/viewer/2022070401/56649f1f5503460f94c37213/html5/thumbnails/27.jpg)
Example Deletion (Internal Node) Delete ( 10 )
5
10
30
2 25 45
5
25
30
2 25 45
5
25
30
2 45
Replacing 10 with smallest value in right
subtree
Deleting leaf Resulting tree
04/20/23 27