CSC
221-13a-A4

# Calculate the search cost for each node when it is

• Notes
• 3

This preview shows pages 2–3. Sign up to view the full content.

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]: 3[2] 5[1] 7[3] 9[2] Sum of the search cost over all the nodes in the tree is: 2+1+3+2 = 8. Average search cost: 8 / 4 = 2.

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.
• Fall '09
• search cost, average search cost

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern