
Concept explainers
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
...
...](https://content.bartleby.com/qna-images/question/3ded9074-643d-40d8-825c-f0dff7af7ebf/fb9d9b76-02c4-4f9d-8820-55b700258b00/mcw12cea_thumbnail.png)


Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

- Given the following C code: struct Node int data; struct Node struct Node next; prev; struct Node head; void funx ( ) if (head==NULL) return; struct Node current=head; while (current->next!=NULL) current=current-->next; while (current!=NULL) printf("&d ", current->data; current=current->prev; What is the functionality of this function? a) Print the contents of linked list b) Print the contents of linked list in reverse order c) Nonearrow_forwardIt is python language Write the code that creates a new Node class. It will store data and next attributes. You only need to create the __init__ method. data and next variables will have default values, both set to None. Assume you are using the Node class from the previous connection to create a LinkedList. You have the code below, create a method that removes the first node from the LinkedList. class LinkedList: def __init__(self): self.head = None Based on the code from the last two questions, create a new LinkedList. Add 2 values to the LinkedList (there is an add method that accepts data as an argument, called add). Then call the removeFront method created in the previous question. Based on the previous questions, create a Queue class that uses the LinkedList for its data storage. Create the __init__, isEmpty, insert, remove, and size methods. Assume that LinkedList class has the add, removeFront and size methods defined. Based on the LinkedList code already…arrow_forward#include <iostream>#include <cstdlib>using namespace std; class IntNode {public: IntNode(int dataInit = 0, IntNode* nextLoc = nullptr); void InsertAfter(IntNode* nodeLoc); IntNode* GetNext(); void PrintNodeData(); int GetDataVal();private: int dataVal; IntNode* nextNodePtr;}; // ConstructorIntNode::IntNode(int dataInit, IntNode* nextLoc) { this->dataVal = dataInit; this->nextNodePtr = nextLoc;} /* Insert node after this node. * Before: this -- next * After: this -- node -- next */void IntNode::InsertAfter(IntNode* nodeLoc) { IntNode* tmpNext = nullptr; tmpNext = this->nextNodePtr; // Remember next this->nextNodePtr = nodeLoc; // this -- node -- ? nodeLoc->nextNodePtr = tmpNext; // this -- node -- next} // Print dataValvoid IntNode::PrintNodeData() { cout << this->dataVal << endl;} // Grab location pointed by nextNodePtrIntNode* IntNode::GetNext() { return this->nextNodePtr;} int IntNode::GetDataVal() {…arrow_forward
- Data Structures/Algorithms in Javaarrow_forwardWhat happens when a programmer attempts to access a node's data fields when the node variable refers to None? How do you guard against it? *PYTHONarrow_forwardFor any element in keysList with a value greater than 50, print the corresponding value in itemsList, followed by a comma (no spaces). Ex: If the input is: 32 105 101 35 10 20 30 40 the output is: 20,30, 1 #include 2 3 int main(void) { const int SIZE_LIST = 4; int keysList[SIZE_LIST]; int itemsList[SIZE_LIST]; int i; 4 6 7 8 scanf("%d", &keysList[0]); scanf ("%d", &keysList[1]); scanf("%d", &keysList[2]); scanf("%d", &keysList[3]); 10 11 12 13 scanf ("%d", &itemsList[0]); scanf ("%d", &itemsList[1]); scanf("%d", &itemsList[2]); scanf ("%d", &itemsList[3]); 14 15 16 17 18 19 /* Your code goes here */ 20 21 printf("\n"); 22 23 return 0; 24 }arrow_forward
- The numbers on the left margin denote line numbers. line#01 class LL_node {02 int val;03 LL_node next;04 public LL_node (int n) {05 val = n;06 next = null;07 }08 public void set_next (LL_node nextNode) {09 next = nextNode;10 }11 public LL_node get_next () {12 return next;13 }14 public void set_value (int input) {15 val = input;16 }17 public int get_value () {18 return val;19 }20 }21 public class LL {22 protected LL_node head;23 protected LL_node tail;24 public LL () {25 head = null;26 tail = null;27 }28 public int append (int n) {29 if (head == null) {30 head = new LL_node(n);31 tail = head;32 } else {33 LL_node new_node;34 new_node = new LL_node(n);35 tail.set_next (new_node);36 tail = new_node;37 }38 return n;39 }40 } which the following statement…arrow_forward#ifndef NODES_LLOLL_H#define NODES_LLOLL_H #include <iostream> // for ostream namespace CS3358_SP2023_A5P2{ // child node struct CNode { int data; CNode* link; }; // parent node struct PNode { CNode* data; PNode* link; }; // toolkit functions for LLoLL based on above node definitions void Destroy_cList(CNode*& cListHead); void Destroy_pList(PNode*& pListHead); void ShowAll_DF(PNode* pListHead, std::ostream& outs); void ShowAll_BF(PNode* pListHead, std::ostream& outs);} #endif #include "nodes_LLoLL.h"#include "cnPtrQueue.h"#include <iostream>using namespace std; namespace CS3358_SP2023_A5P2{ // do breadth-first (level) traversal and print data void ShowAll_BF(PNode* pListHead, ostream& outs) { cnPtrQueue queue; CNode* currentNode; queue.push(lloLLPtr->getHead()); while (!queue.empty()) { currentNode = queue.front(); queue.pop(); if…arrow_forwardExplain the following code : public static void printLeafNodes(TreeNode node) { // base case if (node == null) { return; } if (node.left == null && node.right == null) { System.out.printf("%d ", node.value); } printLeafNodes(node.left); printLeafNodes(node.right); }arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





