Question

The main goal is to implement two recursive methods, each is built to manipulate linked data...

The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes.

1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class

Generic implementation not needed, the nodes will store integer values

The standard methods will not be needed in this exercise except the constructor

2.) Add a toString( ) method to the BinaryNode class. The method creates a String description of the tree.

3.) Add a method named BSTFactory () to the class. This method is static returns, for any given N, the root reference of a full binary search tree of depth N such that the nodes store the integers 1, 2, 3, …, 2 N+1 – 1

must be recursive. More precisely double recursive, the method calls itself with decreased depth with respect to the left link and right link of the caller node

takes parameter(s), depth is naturally the control parameter, and it may be helpful to add another parameter, the “top” number stored in the root of the current sub-tree

may also benefit of a private helper method which computes the power of 2 of a given exponent

Hints:

Construct and draw the BST solutions on a piece of paper for depth values

N = 0, 1, 2, 3.

Note that the solution is always unique, there is only one way to fill in the numbers

Observe the values in the root nodes, deduct and verify a rule for the root values

Observe the values in the children of the root, deduct and verify a rule how to express the children values in terms of the root value.

The rules you found will help to parametrize correct the BSTFactory( ) method in the recursive algorithm

4.) Add another class SimpleNode to you project (this class is independent of the previous BinaryNode class). SimpleNode is a (non-generic) representative of any Node class . As in the above case, none of the standard methods but the constructor will be used in this demonstration. You will need however the toString( ) method and the getTail( ) method from your previous assignments. The type of the data stored in the nodes has no relevance to this application, choose String values at will

5.) Add a method named reverse () to the class. This method is static

takes the head of the original list for parameter

given the head reference of a linked list, returns the head reference of the linked list in reversed order; that is the method has to reverse all the links of the original list, the original tail will be the head and the original head will be the new tail. Note that the nodes are not cloned and the data stored in the nodes are not moved or changed

must be recursive, applied on shorter and shorter lists

Choose carefully the base case(s)

6.) Add an Application class to the project to test and demonstrate the work of the methods. Print all output to the console

7.) Test BSTFactory for all values N = 0, 1, 2, 3, 4, 5

8.) Test reverse( ) for lists of length 0, 1, 5 and 10

9.) Use the toString() method of your classes to create output messages. Design your display such that it gives clear explanation about the output data.

Homework Answers

Answer #1

Answer:

# include <iostream>
# include <cstdlib>
using namespace std;
struct node
{
    int value;
    struct node *first;
    struct node *second;
}*root;
class BST
{
    public:
        void search(int, node **, node **);  
        void push(int);
        void del(int);
        void function_one(node *,node *);
        void function_two(node *,node *);
        void function_three(node *,node *);
        void preorder(node *);
        void inorder(node *);
        void postorder(node *);
        void display(node *, int);
        BST()
        {
            root = NULL;
        }
};
int main()
{
    int option, num;
    BST bst;
    node *value;
    while (1)
    {
        cout<<"-----------------"<<endl;
        cout<<"Operations on BST"<<endl;
        cout<<"-----------------"<<endl;
        cout<<"1.push Element "<<endl;
        cout<<"2.Delete Element "<<endl;
        cout<<"3.Inorder Traversal"<<endl;
        cout<<"4.Preorder Traversal"<<endl;
        cout<<"5.Postorder Traversal"<<endl;
        cout<<"6.Display"<<endl;
        cout<<"7.Quit"<<endl;
        cout<<"Enter your option : ";
        cin>>option;
        switch(option)
        {
        case 1:
            value = new node;
            cout<<"Enter the number to be pushed : ";
        cin>>value->value;
            bst.push(root, value);
        case 2:
            if (root == NULL)
            {
                cout<<"Tree is empty, nothing to delete"<<endl;
                continue;
            }
            cout<<"Enter the number to be deleted : ";
            cin>>num;
            bst.del(num);
            break;
        case 3:
            cout<<"Inorder Traversal of BST:"<<endl;
            bst.inorder(root);
            cout<<endl;
            break;
   case 4:
            cout<<"Preorder Traversal of BST:"<<endl;
            bst.preorder(root);
            cout<<endl;
            break;
        case 5:
            cout<<"Postorder Traversal of BST:"<<endl;
            bst.postorder(root);
            cout<<endl;
            break;
        case 6:
            cout<<"Display BST:"<<endl;
            bst.display(root,1);
            cout<<endl;
            break;
        case 7:
            exit(1);
        default:
            cout<<"Wrong option"<<endl;
        }
    }
}
void BST::search(int input, node **pointer, node **places)
{
    node *ptr, *ptrsave;
    if (root == NULL)
    {
        *places = NULL;
        *pointer = NULL;
        return;
    }
    if (input == root->value)
    {
        *places = root;
        *pointer = NULL;
        return;
    }
    if (input < root->value)
        ptr = root->first;
    else
        ptr = root->second;
    ptrsave = root;
    while (ptr != NULL)
    {
        if (input == ptr->value)
        {
            *places = ptr;
            *pointer = ptrsave;
            return;
        }
        ptrsave = ptr;
        if (input < ptr->value)
            ptr = ptr->first;
   else
        ptr = ptr->second;
    }
    *places = NULL;
    *pointer = ptrsave;
}
void BST::push(node *tree, node *newnode)
{
    if (root == NULL)
    {
        root = new node;
        root->value = newnode->value;
        root->first = NULL;
        root->second = NULL;
        cout<<"Root Node is Added"<<endl;
        return;
    }
    if (tree->value == newnode->value)
    {
        cout<<"Element already in the tree"<<endl;
        return;
    }
    if (tree->value > newnode->value)
    {
        if (tree->first != NULL)
        {
            push(tree->first, newnode);  
   }
   else
   {
            tree->first = newnode;
            (tree->first)->first = NULL;
            (tree->first)->second = NULL;
            cout<<"Node Added To first"<<endl;
            return;
        }
    }
    else
    {
        if (tree->second != NULL)
        {
            push(tree->second, newnode);
        }
        else
        {
            tree->second = newnode;
            (tree->second)->first = NULL;
            (tree->second)->second = NULL;
            cout<<"Node Added To second"<<endl;
            return;
        }  
    }
}
void BST::del(int input)
{
    node *pointerent, *place;
    if (root == NULL)
    {
        cout<<"Tree empty"<<endl;
        return;
    }
    search(input, &pointerent, &place);
    if (place == NULL)
    {
        cout<<"input not present in tree"<<endl;
        return;
    }
    if (place->first == NULL && place->second == NULL)
        function_one(pointerent, place);
    if (place->first != NULL && place->second == NULL)
        function_two(pointerent, place);
    if (place->first == NULL && place->second != NULL)
        function_two(pointerent, place);
    if (place->first != NULL && place->second != NULL)
        function_three(pointerent, place);
    free(place);
}
void BST::function_one(node *pointer, node *places )
{
    if (pointer == NULL)
    {
        root = NULL;
    }
    else
    {
        if (places == pointer->first)
            pointer->first = NULL;
        else
            pointer->second = NULL;
    }
}
void BST::function_two(node *pointer, node *places)
{
    node *child;
    if (places->first != NULL)
        child = places->first;
    else
        child = places->second;
    if (pointer == NULL)
    {
        root = child;
    }
    else
    {
        if (places == pointer->first)
            pointer->first = child;
        else
            pointer->second = child;
    }
}
void BST::function_three(node *pointer, node *places)
{
    node *ptr, *ptrsave, *suc, *pointersuc;
    ptrsave = places;
    ptr = places->second;
    while (ptr->first != NULL)
    {
        ptrsave = ptr;
        ptr = ptr->first;
    }
    suc = ptr;
    pointersuc = ptrsave;
    if (suc->first == NULL && suc->second == NULL)
        function_one(pointersuc, suc);
    else
        function_two(pointersuc, suc);
    if (pointer == NULL)
    {
        root = suc;
    }
    else
    {
        if (places == pointer->first)
            pointer->first = suc;
        else
            pointer->second = suc;
    }
    suc->first = places->first;
    suc->second = places->second;
}
void BST::preorder(node *ptr)
{
    if (root == NULL)
    {
        cout<<"Tree is empty"<<endl;
        return;
    }
    if (ptr != NULL)
    {
        cout<<ptr->value<<" ";
        preorder(ptr->first);
        preorder(ptr->second);
    }
}

void BST::inorder(node *ptr)
{
    if (root == NULL)
    {
        cout<<"Tree is empty"<<endl;
        return;
    }
    if (ptr != NULL)
    {
        inorder(ptr->first);
        cout<<ptr->value<<" ";
        inorder(ptr->second);
    }
}
void BST::postorder(node *ptr)
{
    if (root == NULL)
    {
        cout<<"Tree is empty"<<endl;
        return;
    }
    if (ptr != NULL)
    {
        postorder(ptr->first);
        postorder(ptr->second);
        cout<<ptr->value<<" ";
    }
}

void BST::display(node *ptr, int level)
{
    int i;
    if (ptr != NULL)
    {
        display(ptr->second, level+1);
        cout<<endl;
        if (ptr == root)
            cout<<"Root->: ";
        else
        {
            for (i = 0;i < level;i++)
                cout<<"       ";
   }
        cout<<ptr->value;
        display(ptr->first, level+1);
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
Given this definition of a generic Linked List node: public class LLNode {     private T...
Given this definition of a generic Linked List node: public class LLNode {     private T data;     private LLNode next;     public LLNode(T data, LLNode next) {           this.data = data;           this.next = next;     }     public void setNext(LLNode newNext){ next = newNext; }     public LLNode getNext(){ return next; }     public T getData() {return data;} } Write the findMinimumNode method body. This method returns the linked list node that contains the minimum value in the...
1. Create a linked list using the Node class that contains the first million prime numbers...
1. Create a linked list using the Node class that contains the first million prime numbers (primes4.txt). It should have a menu with these options: 1 = Search for a Number (let the user enter a number to search for) 2 = Add a new Number (let the user enter a number and add it to the head of the list) 3 = Delete a Number (let the user enter a number and delete it if found) 4 = Exit...
Lab 02, Data Abstractions and Structures, CSE 274, Fall 2019 Linked Bag In this lab, we...
Lab 02, Data Abstractions and Structures, CSE 274, Fall 2019 Linked Bag In this lab, we will implement the Linked Bag. The bag will contain a sequence of strings. First, you need to design the Node class (Node.java). It will contain an integer data and a reference to the next Node. The constructor of Node class receives an integer and assigns it to the data field. It also has a default constructor. Then we will design another class named LinkedBag...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted below) Implement the following methods using recursion: int depth() // returns the length of the deepest path from root to any leaf int node_count() // returns the number of nodes in the tree void insert(int n) // inserts value n into the tree BinarySearchTree2C clone() // returns a clone (deep copy) of the tree Add code to the main method to test these methods....
You will be traversing through an integer tree to print the data. Given main(), write the...
You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 - 85 -...
Java Program: You will be traversing through an integer tree to print the data. Given main(),...
Java Program: You will be traversing through an integer tree to print the data. Given main(), write the methods in the 'IntegerBinaryTree' class specified by the // TODO: sections. There are 6 methods in all to write. Ex: If the input is: 70 86 60 90 49 62 81 85 38 -1 the output should be: Enter whole numbers to insert into the tree, -1 to stop Inorder: 38 - 49 - 60 - 62 - 70 - 81 -...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked list with the contents of the original list reversed. Make sure not to use any methods like addToHead() or addToTail(). In addition, consider any special cases that might arise. What is the big-O complexity of your method in terms of the list size n Supplementary Exercise for Programming (Coding) [Singly Linked Lists] Download and unpack (unzip) the file SinglyLinkedList.rar. Compile and execute the class...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT