Question

In: Computer Science

Add a public method called CountChildren to the OurPriorityQueue class that receives a priority value and...

Add a public method called CountChildren to the OurPriorityQueue class that receives a priority value and returns the number of children nodes below the node with the priority value. It must call a private recursive method that does the actual counting of children nodes.

The recursive private method must count and return the number of nodes within a subtree where the subtree’s “root” node has a Value that equals the value passed into the method. Do not include the parent node in your count. Do not add any variable(s) to the class.

Example:

Empty Tree ==> Count children(1) =-1

                  1
       ┌───┴───┐
     2       3
   ┌─┴─┐        ┌─┘
   4         5        6

---------------
Count children(1) returns 5
Count children(2) returns 2
Count children(3) returns 1
Count children(4) returns 0

OURPRIORITYQUEUE CLASS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
///


/// A Priority Queue with a lower value is a higher priority
///


///
///
public class OurPriorityQueue where TPriority : IComparable
{
///
/// Private class for each cell in the array
///
private class Cell
{
///
/// The data - this can be any data type not just an int or string
///
public TValue Value { get; set; }
///
/// The priority value for the data, it can be any data type as long as the type it comparable
///
public TPriority Priority { get; set; }

public Cell(TPriority aPriority = default(TPriority), TValue aValue = default(TValue))
{
Priority = aPriority;
Value = aValue;
}
public override string ToString()
{
return Priority + ":" + Value;
}
}
// -------------------------------------------------------

///


/// The array where each cell is initially null
///
private Cell[] table;   
///
/// Keeps track of the number of items
///
private int count = 0;

public OurPriorityQueue(int size = 100)
{
if (size < 10)
size = 10;

table = new Cell[size];
}
///


/// Returns true is there are no items stored
///
///
public bool IsEmpty()
{
return count == 0;
}

///


/// Changes the count to 0 but doesn't change any of the cells, the old data will be overridden later
///
public void Clear()
{
count = 0;
}

///


/// Returns the data at the root but does not remove it
///
///
public TValue Peek()
{
if (IsEmpty() == true)
throw new ApplicationException("Can't Peek an empty OurPriorityQueue");

return table[1].Value;
}

///


/// Adds a new item using its priority to help place it in the array
///
///
///
public void Add(TPriority aPriority, TValue aValue)
{
if (isFull() == true)
return;

// Percolate up
int hole = ++count; // the hold goes on the bottom level
// keep moving the hold up until it is the root or the hole's parent has a lower priority
for (; hole > 1 && aPriority.CompareTo(table[hole / 2].Priority) < 0; hole /= 2)
table[hole] = table[hole / 2];
// put the priority, value pair in the array where the hole is
table[hole] = new Cell(aPriority, aValue);
}


///


/// Remove and return the highest priority data
///
///
public TValue Remove()
{
if (IsEmpty() == true)
throw new ApplicationException("Can't Remove from an empty OurPriorityQueue");

// save the data for later
TValue value = table[1].Value;
// put the last item in the tree in the root
table[1] = table[count--];

// keep moving the lowest child up until we've found the right spot
// for the item moved from the last level to the root
percolateDown(1);

return value;
}

///


/// Reposition the hole down until it is in the right spot for its priority
///
///
private void percolateDown(int hole)
{
int child;
// save the hole's cell in a tmp spot
Cell pTmp = table[hole];

// keep going down the tree until the last level
for (; hole * 2 <= count; hole = child)
{
child = hole * 2; // get right child
// check right and left child and put lowest one in the child variable
if (child != count && table[child + 1].Priority.CompareTo(table[child].Priority) < 0)
child++;
// put lowest child in hole
if (table[child].Priority.CompareTo(pTmp.Priority) < 0)
table[hole] = table[child];
else
break;
}
// found right spot of hole's original value, put it back into tree
table[hole] = pTmp;
}
///


/// Assumes all but last item in array is in correct order
/// Shifts last item in array into correct location based on priority
///
public void buildHeap()
{
for (int i = count / 2; i > 0; i--)
percolateDown(i);
}

// return contents of array in order (e.g. left child, parent, right child)
public StringBuilder InOrder()
{
return InOrder(1);
}
private StringBuilder InOrder(int position) // why?? What does this do??
{
StringBuilder str = new StringBuilder();
if (position <= count)
{
str.Append(InOrder(position * 2) + "\t");
str.Append(table[position].Value.ToString() + "\n ");
str.Append(InOrder(position * 2 + 1) + "\t");
//str.Append("\t" + InOrder(position * 2) + " ");
//str.Append("\n" + mTable[position].Value.ToString() + " ");
//str.Append("\t" + InOrder(position * 2 + 1) + " ");
}
return str;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
for (int i = 1; i < count; i++)
{
sb.Append(table[i].Value.ToString() + "; ");
}
return sb.ToString();
}
}
}

Solutions

Expert Solution

After looking the Add and remove function we can understand that the table array is built in a order of highest priority to the lowest priority. So the root node is of higest priority. However we can visualise it as simple binary tree implemented in a array.

The following code serves the purpose of the question

//This is the recursive function returns the number of child in a given index

private int child_total(int i){  //if the parent had child
if(i<=count || table[i].Value!= NULL){

return child_total(2*i) + child_total(2*i + 1) + 1; //recurse on left and right child and add 1 to keep the count


}
else //if the index is not out of bound, which means the parent had no child
return 0;
}

public void CountChildren(TValue aValue, ref int counter_child)
{
int i;
int total_child=0;
for(i=1; i<count; i++){ //find the index of the key
if(table[i].Value== aValue){
break;
}
}
if(i==count){ //if key was not found it will return 0
Console.WriteLine(total_child);
}
else{ //if the key was found call the recursive function
total_child=child_total(int i);
}  
}


Related Solutions

Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
To the TwoDArray class, add a method called transpose() that generates the transpose of a 2D...
To the TwoDArray class, add a method called transpose() that generates the transpose of a 2D array with the same number of rows and columns (i.e., a square matrix). Add appropriate code in main() of the TwoDArrayApp class to execute the transpose() method and use the display() method to print the transposed matrix. /** * TwoDArray.java */ public class TwoDArray {    private int a[][];    private int nRows; public TwoDArray(int maxRows, int maxCols)        //constructor {    a...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public final class Account implements Comparable {     private String firstName;     private String lastName;     private int accountNumber;     private double balance;     private boolean isNewAccount;     public Account(             String firstName,             String lastName,             int accountNumber,             double balance,             boolean isNewAccount     ) {         this.firstName = firstName;         this.lastName = lastName;         this.accountNumber = accountNumber;         this.balance = balance;         this.isNewAccount = isNewAccount;     }     /**      * TO DO: override equals      */     @Override     public boolean equals(Object other) {...
2.1) To the HighArray class in the highArray.java program, add a method called getMax() that returns...
2.1) To the HighArray class in the highArray.java program, add a method called getMax() that returns the value of the highest key in the array, or a -1 if the array is empty. Add some code in main() to exercise this method. You can assume all the keys are positive numbers. 2.2) Modify the method in Programming project 2.1 so that the item with the highest key is not only returned by the method but also removed from the array....
To the TwoDArray class, add a method called sumCols() that sums the elements in each column...
To the TwoDArray class, add a method called sumCols() that sums the elements in each column and displays the sum for each column. Add appropriate code in main() of the TwoDArrayApp class to execute the sumCols() method. /** * TwoDArray.java */ public class TwoDArray { private int a[][]; private int nRows; public TwoDArray(int maxRows, int maxCols) //constructor { a = new int[maxRows][maxCols]; nRows = 0; } //****************************************************************** public void insert (int[] row) { a[nRows] = row; nRows++; } //****************************************************************** public...
public class AddValueToArray { // You must define the addValueTo method, which will add // a...
public class AddValueToArray { // You must define the addValueTo method, which will add // a given value to each element of the given array. // // TODO - define your code below this comment // // DO NOT MODIFY main! public static void main(String[] args) { int[] array = new int[]{3, 8, 6, 4}; int valueToAdd = Integer.parseInt(args[0]); addValueTo(valueToAdd, array); for (int index = 0; index < array.length; index++) { System.out.println(array[index]); } } }
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add comments, but the main method should look exactly like this // when you submit your project. public static void main(String[] args) { runTransaction(); } // TODO: Define the runTranscation method. // runTransaction runs the cash register process from start to finish. However, // it will not contain ALL Of the code. You are going to have to delegate some of // its functionality to...
In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
In javascript, Add a method named insertStep that receives a String and an int. It adds...
In javascript, Add a method named insertStep that receives a String and an int. It adds the String to the collection of instructions, before the step whose index is the int. You may assume that the user won’t try to insert something before a step that doesn’t exist.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT