Search Menu


Problems 2

Problem : We provided a rather simple function for adding a new element to a binary search tree. We neglected, however, to show how to remove an element. Write a function that will remove an element from a binary search tree.

The implementation of this is rather tricky, so the algorithm is abstracted away from code to make it easier to understand. First walk through the tree using the same method as in the binary search until you either find a tree rooted by the element we want to remove, or you walk off the tree. If you walk off the tree, then the tree never contained the element to begin with, and we don't have to do anything. Otherwise, we are at a tree with the element to remove as its root. The next part of the algorithm consists of fixing the tree whose root we want to remove. The algorithm basically consists of a shift operation, where a shift means moving the data element from a child up to the data element for the parent. A right shift means doing shifting from the right child. Similarly, a left shift means shifting data from the left child. If there is a left child, perform a left shift. Then walk along the left branch and start the second part of the algorithm again. If there is no left child, then if there is a right child, do a right shift and perform the second half of the algorithm on the right subtree. Continue until you have reached a leaf, at which point free that node, and change the appropriate pointer in the parent.

Problem : You are given a set of phone numbers and the names of people to whom they belong. You are to create a binary search tree to quickly look up people based on their phone numbers. What sort of data will you put into the nodes and by what piece of the data will the tree be ordered?

You will need a structure that contains a name. A char * to hold a string will work. To hold the phone number, a character array of size 7 will work nicely (if you needed area codes too, then you would need 10 characters). The sorting in the binary search tree should be based on this second data element, the phone number, since we are going to be accessing (searching) the information based on phone number.

Problem : Assuming the data is given to you in random order, how might you create the binary search tree and what is the complexity of this operation?

First define a new_node function for the modified ~tree_t~ that you made. Then construct the tree by inserting new nodes into the growing tree one at a time using an insert algorithm. The complexity of the insert is O(log(n)) and since we do this n times, the overall complexity is O(nlog(n)) .

Marketing Management / Edition 15

Diagnostic and Statistical Manual of Mental Disorders (DSM-5®) / Edition 5