Parent reference for BST Redefine TreeNode by adding a refer

(Parent reference for BST) Redefine TreeNode by adding a reference to a node\'s parent, as shown below\': Reimplement the insert and delete methods in the BST class to update the parent for each node in the tree. Add the following new method in BST:/** Returns the node for the specified element. * Returns null if the element is not in the tree. */private TreeNode getNode(E element)/** Returns true if the node for the element is a leaf */private boolean isLeaf(E element)/** Returns the path of elements from the specified element * to the root in an array list. */public ArrayList getPath(E e) Write a test program that prompts the user to enter 10 integers, adds them to the tree, deletes the first integer from the tree, and displays the paths for all leaf nodes. Here is a sample run:

Solution

#include <stdio.h>

#include <string.h>

void swap(char *x, char *y)

{

    char temp;

    temp = *x;

    *x = *y;

    *y = temp;

void permute(char *a, int l, int r)

{

   int i;

   if (l == r)

     printf(\"%s\ \", a);

   else

   {

       for (i = l; i <= r; i++)

       {

          swap((a+l), (a+i));

          permute(a, l+1, r);

          swap((a+l), (a+i)); //backtrack

       }

   }

}

int main()

{

    char str[] = \"ABC\";

    int n = strlen(str);

    permute(str, 0, n-1);

    return 0;

}

part ii radix sort:

c++

#include<iostream>

using namespace std;

int getMax(int arr[], int n)

{

    int mx = arr[0];

    for (int i = 1; i < n; i++)

        if (arr[i] > mx)

            mx = arr[i];

    return main()

void countSort(int arr[], int n, int exp

for (i = 0; i < n; i++)

        count[ (arr[i]/exp)%10 ]++;

    for (i = 1; i < 10; i++)

        count[i] += count[i - 1];

    for (i = n - 1; i >= 0; i--)

    {

        output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];

        count[ (arr[i]/exp)%10 ]--;

    }

    for (i = 0; i < n; i++)

        arr[i] = output[i];

}

adixsort(int arr[], int n)

{

    int m = getMax(arr, n);

for (int exp = 1; m/exp > 0; exp *= 10)

        countSort(arr, n, exp);

}

void print(int arr[], int n)

{

    for (int i = 0; i < n; i++)

        cout << arr[i] << \" \";

}

int main()

{

    int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};

    int n = sizeof(arr)/sizeof(arr[0]);

    radixsort(arr, n);

    print(arr, n);

    return 0;

}

java:

import java.io.*;

import java.util.*;

class Radix {

static int getMax(int arr[], int n)

    {

        int mx = arr[0];

        for (int i = 1; i < n; i++)

            if (arr[i] > mx)

                mx = arr[i];

        return mx;

    }

    static void countSort(int arr[], int n, int exp)

    {

        int output[] = new int[n]; // output array

        int i;

        int count[] = new int[10];

        Arrays.fill(count,0);

        for (i = 0; i < n; i++)

            count[ (arr[i]/exp)%10 ]++;

        for (i = 1; i < 10; i++)

            count[i] += count[i - 1];

        for (i = n - 1; i >= 0; i--)

        {

            output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];

            count[ (arr[i]/exp)%10 ]--;

        }

        for (i = 0; i < n; i++)

            arr[i] = output[i];

    }

    static void radixsort(int arr[], int n)

    {

        int m = getMax(arr, n);

        for (int exp = 1; m/exp > 0; exp *= 10)

            countSort(arr, n, exp);

    }

    static void print(int arr[], int n)

    {

        for (int i=0; i<n; i++)

            System.out.print(arr[i]+\" \");

    }

public static void main (String[] args)

    {

        int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};

        int n = arr.length;

        radixsort(arr, n);

        print(arr, n);

    }

}

#include <stdio.h>

#include <string.h>

void swap(char *x, char *y)

{

    char temp;

    temp = *x;

    *x = *y;

    *y = temp;

void permute(char *a, int l, int r)

{

   int i;

   if (l == r)

     printf(\"%s\ \", a);

   else

   {

       for (i = l; i <= r; i++)

       {

          swap((a+l), (a+i));

          permute(a, l+1, r);

          swap((a+l), (a+i)); //backtrack

       }

   }

}

int main()

{

    char str[] = \"ABC\";

    int n = strlen(str);

    permute(str, 0, n-1);

    return 0;

}

 (Parent reference for BST) Redefine TreeNode by adding a reference to a node\'s parent, as shown below\': Reimplement the insert and delete methods in the BST
 (Parent reference for BST) Redefine TreeNode by adding a reference to a node\'s parent, as shown below\': Reimplement the insert and delete methods in the BST

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site