Main.java x 1 import java.util.*; 2 , public class Main 3 Av public static void main(String [] args){ BinarySearchTree1 myTree = new BinarySearchTree1(); 5 myTree. Insert (1); myTree.getHeight(); myTree.getlargestKey (); myTree.getAverage () ; 7 8. 10 11 12 13 3

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Fix the main file errors.

    public  void Insert(int newItem)
    {
        Node parent = null;
        Node newNode = new Node(newItem);
        Node current = root;
        while (current != null) {
            parent = current;
            if (newNode.item < current.item) {
                current = current.Left;
            } else {
                current = current.Right;
            }
        }
        if (root == null) {
            root = newNode;
        } else
        {
            if (newNode.item < parent.item) {
                parent.Left = newNode;
            } else {
                parent.Right = newNode;
            }
        }
    }
    private 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;
    }

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

    private 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
    }

}

Files
Main.java x
1 import java.util.*;
2 v public class Main
Main.java
3
public static void main(String [] args){
BinarySearchTree1 myTree = new BinarySearchTree1 ();
BinarySearchTree1.java
myTree. Insert (1);
myTree.getHeight();
myTree.getlargestKey ();
myTree.getAverage ();
7
8
10
11
12
13 3
...
...
Transcribed Image Text:Files Main.java x 1 import java.util.*; 2 v public class Main Main.java 3 public static void main(String [] args){ BinarySearchTree1 myTree = new BinarySearchTree1 (); BinarySearchTree1.java myTree. Insert (1); myTree.getHeight(); myTree.getlargestKey (); myTree.getAverage (); 7 8 10 11 12 13 3 ... ...
import java.util.;
2
public class BinarysearchTreel
4 v i
5
public Node root;
private int max;
7
private int count=0;
private int sum=0;
public BinarysearchTzee1 () i
10
r0ot = null;
11
public Node getroot () I
12
13
return root;
14
15 v
public Node search (int KEY) {
16
Node x = r0ot;
17 v
while (x != null &a x.item != KEY)
18 ,
if (KEY < x.item) {
X = x.Left:
} else i
x = x.Right;
19
20 v
21
22
23
24
return x; / x.item == key or x == null
25
public Node SearchRec (Node root, int key)
26
27 ,
if (root == null)
return null;
28
29
if ( root.item == key)
31
return root;
32
if (root.item > key)
33
return SearchRec (root. Left, key):
34
else
return SearchRec (root.Right, key):
35
36
37
public void Insert (int newItem)
38
59 v
40
Node parent = null;
41
Node newNode = new Node(newItem);
Transcribed Image Text:import java.util.; 2 public class BinarysearchTreel 4 v i 5 public Node root; private int max; 7 private int count=0; private int sum=0; public BinarysearchTzee1 () i 10 r0ot = null; 11 public Node getroot () I 12 13 return root; 14 15 v public Node search (int KEY) { 16 Node x = r0ot; 17 v while (x != null &a x.item != KEY) 18 , if (KEY < x.item) { X = x.Left: } else i x = x.Right; 19 20 v 21 22 23 24 return x; / x.item == key or x == null 25 public Node SearchRec (Node root, int key) 26 27 , if (root == null) return null; 28 29 if ( root.item == key) 31 return root; 32 if (root.item > key) 33 return SearchRec (root. Left, key): 34 else return SearchRec (root.Right, key): 35 36 37 public void Insert (int newItem) 38 59 v 40 Node parent = null; 41 Node newNode = new Node(newItem);
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Binomial Heap
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education