cosc2007 data structures ii chapter 11 trees iv. 2 topics adt bst implementations efficiency...

36
COSC2007 Data Structures II Chapter 11 Trees IV

Upload: jared-gilbert

Post on 05-Jan-2016

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

COSC2007 Data Structures II

Chapter 11

Trees IV

Page 2: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

2

Topics

ADT BST Implementations Efficiency

TreeSort Save/Restore into/from file General Trees

Page 3: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

3

BST Traversalpublic class TreeNode // node in the tree{ private Object item; // data portion private TreeNode leftChildPtr; // pointer to left child private TreeNode rightChildPtr; // pointer to right child

TreeNode() {};

TreeNode(Object nodeItem, TreeNode left , TreeNode right ) { }…….} // end TreeNode class

public abstract class BinaryTreeBasis {

protected TreeNode root;……. // constructor and methods

} // end BinaryTreeBasis class

60

7020

10 40

5030

Page 4: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

4

BST Traversalpublic class TreeNode // node in the tree{ private Object item; // data portion private TreeNode leftChildPtr; // pointer to left child private TreeNode rightChildPtr; // pointer to right child

TreeNode() {}; TreeNode(Object nodeItem, TreeNode left , TreeNode right ) { }…….} // end TreeNode class

public abstract class BinaryTreeBasis {

protected TreeNode root;……. // constructor and methods

} // end BinaryTreeBasis class

60

7020

10 40

5030

Page 5: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

5

ADT BST Implementation Implementation of BST Operations

// Assumption: A tree contains at most one item with a given// search key at any time.//public class BinarySearchTree extends BinaryTreeBasis//inherits isEmpty(), makeEmpty(), getRootItem(){ public:BinarySearchTree(){} //default constructor public BinarySearchTree(KeyedItem rootItem) {

super (rootItem);}

void insert(Keyeditem newItem) {

root =insertItem(root, newItem);} // end insert

60

7020

10 40

5030

Page 6: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

6

ADT BST Implementation Implementation of BST Operations

// Assumption: A tree contains at most one item with a given// search key at any time.//public class BinarySearchTree extends BinaryTreeBasis//inherits isEmpty(), makeEmpty(), getRootItem(){ public:BinarySearchTree(){} //default constructor public BinarySearchTree(KeyedItem rootItem) {

super (rootItem);}

void insert(Keyeditem newItem) {

root =insertItem(root, newItem);} // end insert

60

7020

10 40

5030

Page 7: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

7

ADT BST Implementation

public void delete(Comparable searchKey) throws TreeException

{ root =deleteItem(root, searchKey);

} // end delete

public void delete(KeyedItem item) throws TreeException{

root =deleteItem(root, item.getKey());} // end delete

public KeyedItem retrieve(Comparable searchKey) { return retrieveItem(root, searchKey);} // end retrieve

Page 8: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

8

ADT BST ImplementationProtected TreeNode insertItem(TreeNode tNode, KeyedItem newItem){ TreeNode newSubtree; if (tNode == NULL) { // position of insertion found; insert after leaf // create a new node tNode = new TreeNode(newItem, null, null);

return tNode;}

KeyedItem nodeItem = (KeyedItem)tNode.getItem();

//search for the insertion position if (newItem.getKey().compareTo(nodeItem.getKey())<0) { // search the left subtree newSubtree=insertItem(tNode.getLeft(), newItem);

tNode.setleft (newSubtree);return tNode;

} else { // search the right subtree

newSubtree=insertItem(tNode.getRight(), newItem);tNode.setRight (newSubtree);return tNode;

}} // end insertItem

Page 9: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

9

ADT BST Implementationprotected TreeNode deleteItem(TreeNode tNode, Comparable searchKey)

// Calls: deleteNode.{ TreeNode newSubtree; if (tNode == NULL) throw TreeException("TreeException: delete failed"); // empty tree

else {KeyedItem nodeItem = (KeyedItem)tNode.getItem(); if (searchKey.compareTo(nodeItem.getKey())==0) {

// item is in the root of some subtree tNode = deleteNode(tNode); // delete the item // else search for the item else if (searchKey.compareTo(nodeItem.getKey())<0) { // search the left subtree newSubtree = deleteItem(tNode.getLeft(), searchKey);

tNode.setLeft (newSubtree);}

else { // search the right subtree newSubtree = deleteItem(tNode.getRight(), searchKey);

tNode.setLeft (newSubtree);} end if

} //end ifreturn tNode;

} // end deleteItem

Page 10: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

10

ADT BST Implementationprotected TreeNode deleteNode(TreeNode tNode) {// Algorithm note: There are four cases to consider:// 1. The root is a leaf. 2. The root has no left child.// 3. The root has no right child.4. The root has two children.// Calls: findleftmost and deleteleftMost. KeyedItem replacementItem;

// test for a leaf if ((tNode.getLeft() == null) && (tNode.getRight() == null) ){ return null; } // end if leaf // test for no left child else if (tNode.getLeft() == null) { return tNode.getRight(); } // end if no left child

Page 11: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

11

ADT BST Implementation // test for no right child

else if (tNode.getRight() == null) {return tNode.getLeft();

} // end if no right child

// there are two children: retrieve and delete the inorder successor else {

replacementItem =findLeftmost(tNode.getRight()); tNode.setItem (replacementItem);

tNode.setRight(deleteLeftmost(tNode.getRight()));return tNode;

} // end if two children} // end deleteNode

Page 12: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

12

ADT BST Implementationprotected KeyedItem findLeftmost(TreeNode tNode) {

if (tNode.getLeft()== null)return (KeyedItem)tNode.getItem ();

elsereturn findLeftmost(tNode.getLeft());

} // end findLeftmost

protected TreeNode deleteLeftmost(TreeNode tNode) {

if (tNode.getLeft()== null)return tNode.getRight ();

else {tNode.setLeft (deleteLeftmost (tNode.getLeft()));return tNode;

}

} // end deleteLeftmost

Page 13: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

13

ADT BST Implementationprotected KeyedItem retrieveItem(TreeNode tNode, Comparable searchKey ) {

KeyedItem treeItem;if (tNode == null)

treeItem = null; else {

KeyedItem nodeItem = (KeyedItem)tNode.getItem();if (searchKey.compareTo(nodeItem.getKey())==0)

// item is in the root of some subtree treeItem = (KeyedItem)tNode.getItem(); else if (searchKey.compareTo(nodeItem.getKey())<0) // search the left subtree retrieveItem(tNode.getLeft(), searchKey); else // search the right subtree retrieveItem(tNode.getRight(), searchKey);

} //end if return treeItem;

} // end retrieveItem

Page 14: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

14

Efficiency of BST Operations For the retrieval, insertion, and deletion

operations, compare the searchKey to the search keys in the nodes along a path through the tree

The path terminates At the node that contains the search key Or, if the searchKey isn't present, at an empty subtree

Page 15: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

15

Efficiency of BST Operations

The maximum height of N-Node tree: When ?

Complete trees & full trees have minimum height

The height of an N-node BST ranges from log2 ( N + 1 ) to N

Questions

Page 16: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

16

6

2

4

3

1

8

Search for 4

Efficiency of BST Search

Page 17: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

17

In general, the search is confined to nodesalong a single path from the root to a leaf

h = height of the tree Search time

= O(h)

Page 18: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

18

Suppose the tree is balanced, which means minimum path length is close to maximum path length

h = height of the tree

= O(log N)

where N is the size

(number of nodes) in

the tree

Search time

= O(h) = O(log N)

Page 19: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

19

6

2

4

3

1

8

Insert 5

Efficiency of BST Insertion

Page 20: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

20

6

2

4

3

1

8

5

What’s the time bound for insertion?

h = height of the tree

Efficiency of BST Insertion

Page 21: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

21

6

2

4

3

1

8

What’s the time bound for insertion?

5

Tinsert(N)

= ?

h = height of the tree

Efficiency of BST Insertion

Page 22: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

22

6

2

4

3

1

8

What’s the time bound for insertion?

5

Tinsert(N) = Tsearch(N) + c = ?

h = height of the tree

Efficiency of BST Insertion

Page 23: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

23

Comparison of Time Complexity

Operation Average case Worse Case

Retrieval O(logn) O(n)Insertion O(logn) O(n)Deletion O(logn) O(n)Traversal O(n) O(n)

Page 24: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

24

Review The traversal of a binary tree is ______.

O(n) O(1) O(n2) O(log2n)

Page 25: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

25

Review In an array based representation of a

complete binary tree, which of the following represents the right child of node tree[i]? tree[i+2] tree[i–2] tree[2*i+1] tree[2*i+2]

Page 26: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

26

Review In an array based representation of a

complete binary tree, which of the following represents the parent of node tree[i]? tree[i–2] tree[(i–1)/2] tree[2*i–1] tree[2*i–2]

Page 27: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

27

Review A data element within a record is called a

______. field tree Collection key

Page 28: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

28

Review The maximum number of comparisons for

a retrieval operation in a binary search tree is the ______. length of the tree height of the tree number of nodes in the tree number of leaves in the tree

Page 29: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

29

Review The maximum height of a binary tree of n

nodes is ______. n n / 2 (n / 2) – 2 log2(n + 1)

Page 30: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

30

Review The minimum height of a binary tree of n

nodes is ______. n n / 2 (n / 2) – 2 log2(n + 1)

Page 31: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

31

Review A full binary tree with height 4 has ______

nodes. 7 8 15 31

Page 32: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

32

Review A complete binary tree

1. is sometimes a full binary tree

2. is never a full binary tree

3. is sometimes a binary tree

4. is always a full binary tree

Page 33: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

33

Review A proper binary tree

1. is sometimes a full binary tree

2. is never a full binary tree

3. is sometimes a binary tree

4. is always a full binary tree

Page 34: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

34

Review Select the one true statement.

A. Every binary tree is either complete or full.

B. Every complete binary tree is also a full binary tree.

C. Every full binary tree is also a complete binary tree.

D. No binary tree is both complete and full.

Page 35: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

35

Review (True/False) The ADT binary search tree is value-

oriented. A binary tree cannot be empty. The root of a tree is at level 0. Inorder traversal visits a node before it

traverses either of its subtrees.

Page 36: COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees

36

Review (True/False) Insertion in a search-key order produces a

maximum-height BST Insertion in random order produces a

near-minimum-height BST