cosc2007 data structures ii chapter 11 trees v. 2 topics treesort save/restore into/from file...

11
COSC2007 Data Structures II Chapter 11 Trees V

Upload: angelica-spencer

Post on 04-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees

COSC2007 Data Structures II

Chapter 11

Trees V

Page 2: COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees

2

Topics

TreeSort Save/Restore into/from file General Trees

Page 3: COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees

3

Tree-Sort ADT BST can be used to sort array elements

efficiently into search-key order How?

60

7020

10 40

5030

Page 4: COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees

4

Tree-Sort ADT BST can be used to sort array elements

efficiently into search-key order Pseudocode:

treeSort ( A,N)// Sorts the N integers in an array A into ascending orderInsert A's elements into a BST (T)Traverse T inorder. As you visit T's nodes, copy their data

items into successive locations of A. Advantage: Disadvantage:

60

7020

10 40

5030

Page 5: COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees

5

Saving/Restoring BST in & from Files

Two different possible algorithms Save a BST & then restore it to its original

shape ?

Save a BST & then restore it to a balanced shape

WHY? If we know the number of nodes

How?

Page 6: COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees

6

Saving/Restoring BST in & from Files Two different possible algorithms

Save a BST & then restore it to its original shape

Use preorder traversal & searchTreeInsert to save & then restore a BST into its original shape

Save a BST & then restore it to a balanced shape

Balanced BST increases the efficiency of the ADT operation

If we know the number of nodes Using inorder traversal

Page 7: COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees

7

Saving/Restoring BST in & from Files

You can construct the tree directly by reading the sorted data sequentially from the file In-order traversal

The tree need not be complete It doesn't matter where the nodes on the last

level go If N is even, one of the subtrees will have more

nodes than the other. Arbitrarily choose the extra nodes to be on the left subtree

Page 8: COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees

8

Saving/Restoring BST in & from Files

Building Full BST readFull(inputfile, n)// Builds a full BST from n sorted values in a file// return the tree’s rootTreeNode treeNode;if (n>0){

treeNode = a new node with null child references // Construct the left subtree

set treeNode’s left child to readFull (inputFile, n/2)// Get the rootRead item from file into treeNode’s item// Construct the right subtree set treeNode’s right child to readFull (inputFile, n/2)

} // End Ifreturn treeNode;

Page 9: COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees

9

Saving/Restoring BST in & from Files

Building Minimum-Height BST readTree (inputfile, n)// Builds a minimum-heigt BST from n sorted values in a file// will return the tree's rootif ( n > 0){

treeNode = reference to new node with null child pointers / / Construct the left subtree

set TreeNode’s left child to readTree (inputfile, n/2)/ / Get the rootRead item from file into treeNode’s item/ / Construct the right Subtreeset TreeNode’s right child to readTree (inputfile, (n-1)/2)

} / / End Ifreturn treeNode

Page 10: COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees

10

General Trees Nodes can have more than two children n-ary tree:

A tree whose nodes can have up to n children For pointer-based implementation, the node will

point to its oldest (first) child, that in return will point to a list of its siblings

A

DCB

HGFE I

Page 11: COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees

11

General Trees

If the maximum number of children is reasonable and known, direct pointers from the parent nodes can be used

A

DCB

IHGFE