Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Implement a method to compute the sum of keys at even depth . For example: If the tree is empty, then the sumAtEvenDepth is

Java

Implement a method to compute the sum of keys at even depth. For example:

If the tree is empty, then the sumAtEvenDepth is 0.

If the level-order traversal of the tree is 41 21 61 11 31 51, then: 41 is at depth 0; 21 and 61 are at depth 1; 11, 31 and 51 are at depth 2. Thus, sumAtEvenDepth is 134, since there are nodes 41, 11, 31 and 51 at even depth.

Your method should run in time linear to the size of the tree. You may create a recursive helper function or use a loop. DO NOT change the class structure or function declaration.

public class BST { private static class Node { public Node(int key) { this.key = key; } private int key; private Node left; private Node right; } private Node root; // the root of the tree, null for the empty tree // TODO: implement your method here  public int sumAtEvenDepth(Node n) { ... } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Distributed Relational Database Architecture Connectivity Guide

Authors: Teresa Hopper

4th Edition

0133983064, 978-0133983067

More Books

Students also viewed these Databases questions

Question

What is meant by Career Planning and development ?

Answered: 1 week ago

Question

What are Fringe Benefits ? List out some.

Answered: 1 week ago

Question

4. Who should be invited to attend?

Answered: 1 week ago

Question

7. How will you encourage her to report back on the findings?

Answered: 1 week ago

Question

Were the decisions based on appropriate facts?

Answered: 1 week ago