This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: • Calculate the search cost for each node when it is inserted – A binary node element has at least two data fields: Key and SearchCost – For each node, count and store the number of comparisons required when searching for the node (i.e., the number of comparisons = the search cost for the node = 1+ the depth of the node) • If the number of nodes is less than 2 4 , print on the screen the resulting tree along with the information about the nodes (for each node, print both its key and search cost). Otherwise, print this tree and information about its nodes into a file. See the example below. • Remove a node designated by a user from the binary search tree and print the resulting tree on the screen if the number of nodes is less than 2 4 . You should update the search costs of the tree nodes if the costs have been changed upon node removal. • Calculate the average search cost for all nodes in the tree – Sum up the search cost over all the nodes in your tree as you traverse the tree in one of three traversals – divide the sum by the total number of nodes in the tree, and the result is the average search cost for all the tree nodes • Print total number of nodes and the average search cost 2. Example Input data: 5, 3, 9, 7 Create a binary search tree: Key = 5 SearchCost = 1 Key = 3 SearchCost = 2 Key = 9 SearchCost = 2 Key = 7 SearchCost = 3 Total number of nodes is 4 To generate output on the screen we use the in-order traversal. Here each node is represented as Key[SearchCost]:...
View Full Document
- Winter '09
- search cost, average search cost