5
CS3510 -- Assignment 5 12.1-2 The difference between the binary-search-tree property and the min-heap property is that the binary-search-tree requires that the key of left child be less than or equal to its parent and that the key of the right child be greater than or equal to its parent. The min-heap, on the other hand, requires that both the keys of the children be less than or equal to the key of the parent. The difference is that in the bst the right child is greater than or equal the parent and in the min-heap the right child is less than or equal to the parent. The min-heap cannot be used to print out the keys of an n-node tree in sorted order in O(n) time since the min-heap says nothing about the ordering of child nodes with respect to each other. Additional sorting and comparisons would have to be done, which would automatically bump the algorithm to greater than O(n). 12.2-2 Tree-Minimum(x) 1 if left[x] != NIL 2 x = left[x] 3 Tree-Minimum(x) 4 return x Tree-Maximum(x) 1 if right[x] != NIL 2 x = right[x] 3 Tree-Maximum[x] 4 return x 12.2-5 If a node in a binary search tree has two children, then the condition in the if statement of Tree-Successor holds true since the node certainly has a right child. Tree-Successor(x) 1 if right[x] != NIL 2 then return Tree-Minimum(right[x]) 3 ... When we look at the code for Tree-Minimum, we find that we descend the search tree along the left subtrees until we reach a node that has no left child. Therefore, the node returned as the successor has no left child. Since Tree-Predecessor is symmetric to Tree-Successor, the same logic applies for a call to Tree-Maximum.

CS3510 -- Assignment 5simpson/classes/cs3510/homework5/homework5.pdf · CS3510 -- Assignment 5 12.1-2 The difference between the binary-search-tree property and the min-heap property

Embed Size (px)

Citation preview

CS3510 -- Assignment 512.1-2

The difference between the binary-search-tree property and the min-heap property is that the binary-search-tree requires that the key of left child be less than or equal to its parent and that the key of the right child be greater than or equal to its parent. The min-heap, on the other hand, requires that both the keys of the children be less than or equal to the key of the parent. The difference is that in the bst the right child is greater than or equal the parent and in the min-heap the right child is less than or equal to the parent.

The min-heap cannot be used to print out the keys of an n-node tree in sorted order in O(n) time since the min-heap says nothing about the ordering of child nodes with respect to each other. Additional sorting and comparisons would have to be done, which would automatically bump the algorithm to greater than O(n).

12.2-2

Tree-Minimum(x)1 if left[x] != NIL2 x = left[x]3 Tree-Minimum(x)4 return x

Tree-Maximum(x)1 if right[x] != NIL2 x = right[x]3 Tree-Maximum[x]4 return x

12.2-5

If a node in a binary search tree has two children, then the condition in the if statement of Tree-Successor holds true since the node certainly has a right child.

Tree-Successor(x)1 if right[x] != NIL2 then return Tree-Minimum(right[x])3 ...

When we look at the code for Tree-Minimum, we find that we descend the search tree along the left subtrees until we reach a node that has no left child. Therefore, the node returned as the successor has no left child. Since Tree-Predecessor is symmetric to Tree-Successor, the same logic applies for a call to Tree-Maximum.

12.3-4

The link to the BST may be lost if y gets spliced out in the case where z has two children. In this case, the satellite data of z is updated to be the satellite data of y, but the pointer to y is not updated to its new position is left pointing to a node that is no longer part of the the BST and will soon be recycled via the free list. The code could be changed by updating the left, right, and parent nodes of y to be the same as those of z, then returning z to be recycled instead of y. This makes more sense since we would like to recycle the value that has been deleted, not an old version of a node to remain in the tree. This way the node y will stay intact in its new position in the tree. Here is what the new code would look like:

14 if y != z15 then p[y] = [z]16 left[y] = left[z]17 right[y] = right[z]18 return z

13.1-4

Possible degrees of black nodes after all red nodes absorbed:

0 -- the node is a leaf

2 -- the node has two black children (no red nodes to be absorbed)

3 -- the node has one black child and one red child

4 -- the node has two red children, which both have two black children

Once all of the red nodes have been absorbed, the leaves are at height <= black-height.

13.1-6

Given a red-black tree with black-height k, we maximize the number of internal nodes when the binary tree is complete and only half of the nodes are black. In this case,

k = h/2h = 2k

For a complete binary tree, the number of internal nodes is equal to 2^h -1. In this case, h = 2k. Therefore, the largest possible number of internal nodes is 2^(2k) - 1.

The smallest possible number of internal nodes is 2^k - l since we know that any node x contains at least 2^(bh(x)) - 1 internal nodes by the proof of Lemma 13.1.

EXTRA CREDIT

E[n_i*n_i*n_i] = E[n_i ^ 2] * E[n_i] = (2 - (1/n))*(n * (1/n) = 2 - 1/n

GRAPHS

ANALYSIS

Graph #1

This graph measures the time for finding an element in a BST with random insertions and random lookups. The search time does indeed appear to be lg(n).

Graph #2

Comparing the BST lookup times with the HashTable lookup times was somewhat difficult since the HashTable lookup times are much more random and spread out. Also, I was unable to test a hash table with more than 130000 entries because of memory constraints. However, a load factor of 30 results in a lookup time that averages to about the 200-250 range, which is where the long-term run time of the BST appears to be headed.