a. Find the height of the BST. b. Find node with the largest key. Find the average of the key values of the nodes. c.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I attached the question.

 

public int getHeight(){
        return findHeight(root);
    }

    private int findHeight(Node root){
        if(root==null){
            return 0;
        }
        int leftHeight = findHeight(root.Left);
        int rightHeight = findHeight(root.Right);
        return Math.max(leftHeight,rightHeight)+1;
    }

    public int getLargestKey(){
        enrichLargest(root);
        return max;
    }

    public float getAverage(){
        enrichSum(root);
        enrichCount(root);
        return (float)sum/(float)count;
    }

    private void enrichCount(Node root) {
        if(root!=null){
            count++;
        
        enrichSum(root.Left);
        enrichSum(root.Right);
          }
    }

    private void enrichSum(Node root){
        if(root!=null){
            sum += root.item;
        
        enrichSum(root.Left);
        enrichSum(root.Right);
          }
    }
    private void enrichLargest(Node root){
        if(root!=null){
            max = Math.max(root.item,max);
        
        enrichLargest(root.Left);
        enrichLargest(root.Right);
          }
    }

    public boolean Delete(int key)
    {
        Node parent = null;
        Node curr = root;
        while (curr != null && curr.item != key)
        {
            parent = curr;
            if (key < curr.item) {
                curr = curr.Left;
            } else {
                curr = curr.Right;
            }
        }

        if (curr == null) {
            return false;
        }
        if (curr.Left == null && curr.Right == null) {
            if (curr != root) {
                if (parent.Left == curr) {
                    parent.Left = null;
                }else{
                    parent.Right = null;
                }
            } else{
                root = null;
            }
        }
        else if (curr.Left != null && curr.Right != null) {
            Node successor = getSuccessor(curr.Right);
            int val = successor.item;
            Delete(successor.item);
            curr.item = val;
        }
        else {
            Node child = (curr.Left != null)? curr.Left: curr.Right;
            if (curr != root){
                if (curr == parent.Left) {
                    parent.Left = child;
                } else {
                    parent.Right = child;
                }
            } else {
                root = child;
            }
        }
        return true;
    }

    public Node getSuccessor(Node curr) {
        while (curr.Left != null) {
            curr = curr.Left;
        }
        return curr;
    }
    public void printOrderTraversal(Order order){
        switch(order){
            case IN_ORDER:
                InOrder(root);
                break;
            case PRE_ORDER:
                preOrder(root);
                break;
            case POST_ORDER:
                postOrder(root);
            default:
        }

    }
    public void InOrder(Node theRoot) {
        if (!(theRoot == null))
        {
            InOrder(theRoot.Left);
            theRoot.DisplayNode();
            InOrder(theRoot.Right);
        }
    }

    public void preOrder(Node theRoot) {
        if (!(theRoot == null))
        {
            theRoot.DisplayNode();
            preOrder(theRoot.Left);
            preOrder(theRoot.Right);
        }
    }

    public void postOrder(Node theRoot) {
        if (!(theRoot == null))
        {
            postOrder(theRoot.Left);
            postOrder(theRoot.Right);
            theRoot.DisplayNode();
        }
    }

    public class Node{
        public int item;
        public Node Left;
        public Node Right;
        public Node(int item) {
            this.item = item;
            Left=null;
            Right=null;
        }
        void DisplayNode(){
            System.out.println(this.item);
        }
    }
    public enum Order{
        PRE_ORDER,
        POST_ORDER,
        IN_ORDER
    }

}

Find the height of the BST.
Find node with the largest key.
Find the average of the key values of the nodes.
d.
a.
b.
C.
Print the nodes in the tree in pre-order, in-order, and post-order. (decided by the user after run)
Create the same BST shown in question 2. Run your program for the BST shown in question 1 to
its final state and output the height, the largest key, and average of the keys.
e.
Transcribed Image Text:Find the height of the BST. Find node with the largest key. Find the average of the key values of the nodes. d. a. b. C. Print the nodes in the tree in pre-order, in-order, and post-order. (decided by the user after run) Create the same BST shown in question 2. Run your program for the BST shown in question 1 to its final state and output the height, the largest key, and average of the keys. e.
1 import java.util.*;
2 v public class Main{
3
public static void main(String [] args){
BinarySearchTreel myTree = new BinarySearchTree1 ();
4.
6
7
8
myTree.Insert (1);
9.
10
int height=myTree.getHeight ();
11
12
int largestkey=myTree.getLargestkey ();
13
14
float myAvg=myTree.getAverage ();
15
16
17
Transcribed Image Text:1 import java.util.*; 2 v public class Main{ 3 public static void main(String [] args){ BinarySearchTreel myTree = new BinarySearchTree1 (); 4. 6 7 8 myTree.Insert (1); 9. 10 int height=myTree.getHeight (); 11 12 int largestkey=myTree.getLargestkey (); 13 14 float myAvg=myTree.getAverage (); 15 16 17
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 15 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY