lecture1 introductions and tree data structures 11/12/20151

Post on 04-Jan-2016

225 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Lecture1 introductions and Tree Data Structures

04/20/23 1

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

•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

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

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

– 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

Logical view of an indexed file04/20/23 7

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

top related