Question

In: Computer Science

C++ AVL tree My AVL tree function void inorder(AVLNode* t) { if (t == NULL) return;...

C++ AVL tree

My AVL tree function

void inorder(AVLNode* t)
{
if (t == NULL)
return;
inorder(t->left);
cout << t->data.cancer_rate << " ";
inorder(t->right);
}

void inorder()
{
inorder(root);

}

Will Print my cancer city rate Inorder

example)

3.1

5.8

19.8

My question is how can I add a decreasing index to this function

example)

3) 3.1

2)5.8

1)19.8

Solutions

Expert Solution

You can modify your inorder function slightly to achieve what you want. Please look two sets of examples below

  1. The first block of code shows your current situation. I have created AVLNode with example data and an inorder method similar to yours.
  2. This contains the change
    • The important thing is to know the size of your tree so that you can know the first value to start from and then decrement this value as you go forward.
    • Once we know the size, we will pass this in our inorder function by reference. This means that throught recursion the value will be consistent and whenever we print a node data we will print this value and decrement once.

Block 1 (Your Current State)

#include <bits/stdc++.h> 
using namespace std; 
struct Data{
        double cancer_rate;
};
typedef struct node{
        struct Data data;
        struct node* left;
        struct node* right;
}AVLNode;
void inorder(AVLNode* t){
        if (t == NULL)
                return;
        inorder(t->left);
        cout << t->data.cancer_rate <<endl;
        inorder(t->right);
}
AVLNode* new_AVLNode(double cancer_rate){
        AVLNode* root = new AVLNode;
        root->left=NULL;
        root->right=NULL;
        root->data = {cancer_rate};
}
int main(int argc, char const *argv[])
{
        AVLNode* root = new_AVLNode(5.8);
        root->left = new_AVLNode(3.1);
        root->right = new_AVLNode(19.8);
        inorder(root);
}

Console Output for Block 1

3.1
5.8
19.8

Block 2 with required changes

#include <bits/stdc++.h> 
using namespace std; 
struct Data{
        double cancer_rate;
};
typedef struct node{
        struct Data data;
        struct node* left;
        struct node* right;
}AVLNode;
void inorder(AVLNode* t){
        if (t == NULL)
                return;
        inorder(t->left);
        cout << t->data.cancer_rate <<endl;
        inorder(t->right);
}
//notice that we have passed counter by reference
void inorder_with_decrement_counter(AVLNode* t, int &counter){
        if (t == NULL)
                return;
        inorder_with_decrement_counter(t->left, counter);
        cout << (counter--)<<") "<<t->data.cancer_rate <<endl;
        inorder_with_decrement_counter(t->right, counter);
}
AVLNode* new_AVLNode(double cancer_rate){
        AVLNode* root = new AVLNode;
        root->left=NULL;
        root->right=NULL;
        root->data = {cancer_rate};
}
int tree_size(AVLNode* t){
        if(t==NULL)
                return 0;
        else 
                return 1+ tree_size(t->left)+tree_size(t->right);
}
int main(int argc, char const *argv[])
{
        AVLNode* root = new_AVLNode(5.8);
        root->left = new_AVLNode(3.1);
        root->right = new_AVLNode(19.8);
        cout<<"Normal inorder."<<endl;
        inorder(root);
        cout<<"Inorder with decrement counter."<<endl;
        int size = tree_size(root);
        inorder_with_decrement_counter(root, size);
}

Console Output for Block 2

Normal inorder.
3.1
5.8
19.8
Inorder with decrement counter.
3) 3.1
2) 5.8
1) 19.8

Let me know in comments if you have any doubts. Do leave a thumbs up if this was helpful.


Related Solutions

C++ code (just fuction) Binary Search Tree (BTS) 1) Algorithm for all traversals (inorder, preorder, postorder,...
C++ code (just fuction) Binary Search Tree (BTS) 1) Algorithm for all traversals (inorder, preorder, postorder, level order), done nonrecursively 2)Ability to provide the result of traversing a tree in all traversals (inorder, preorder, postorder, level order)
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace std; AVLTree::AVLTree() { root = NULL; } AVLTree::~AVLTree() { delete root; root = NULL; } // insert finds a position for x in the tree and places it there, rebalancing // as necessary. void AVLTree::insert(const string& x) { // YOUR IMPLEMENTATION GOES HERE } // remove finds x's position in the tree and removes it, rebalancing as // necessary. void AVLTree::remove(const string& x) {...
For a Binary Search Tree in C++ void setHeight(TNode *n)(10): This method sets the heights of...
For a Binary Search Tree in C++ void setHeight(TNode *n)(10): This method sets the heights of the nodes in a tree. Once a node is inserted, only the node’s ancestors can have their height changed. Thus you should set the height of the node being inserted (to 1) and then adjust the heights of the node’s parent, grandparent, etc. up until either the height of the node doesn’t change or you hit the root.
Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
Why is my C code counter returning the wrong output? void removeLn(char *tPtr) { for (;...
Why is my C code counter returning the wrong output? void removeLn(char *tPtr) { for (; *tPtr != '\0'; tPtr++) { if (*tPtr == '\n') { *tPtr = '\0'; } } } This is a method I wrote that essentially replaces the \n in a text file with a null. int counter(FILE *filePtr) { char ln[length]; int counter = 0; while (fgets(ln, length, filePtr) != NULL) { char *r;    for (r = ln; *r != '\0';) { while (isspace(*r))...
In C++ Instructions: Use a void function to print the following message (should be in welcome...
In C++ Instructions: Use a void function to print the following message (should be in welcome function) Welcome to the Event Scheduling program create 3 int arrays with 3 positions (one array for days one array for moths and one array for years) (should be in main) Create a file that contains the following (you can just create the file or write the file in the program) 1 / 26 / 2021 12 / 13 / 2020 2 / 1...
Instructions (in C++): 1 ) Use a void function to print the following message (should be...
Instructions (in C++): 1 ) Use a void function to print the following message (should be in welcome function) Welcome to the Event Scheduling program 2 ) create 3 int arrays with 3 positions (one array for days one array for moths and one array for years) (should be in main) 3 ) Create a file that contains the following (you can just create the file or write the file in the program) 1 / 26 / 2021 12 /...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT