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++ Assume you need to test a function named inOrder. The function inOrder receives three int...
C++ Assume you need to test a function named inOrder. The function inOrder receives three int arguments and returns true if and only if the arguments are in non-decreasing order: that is, the second argument is not less than the first and the third is not less than the second. Write the definition of driver function testInOrder whose job it is to determine whether inOrder is correct. So testInOrder returns true if inOrder is correct and returns false otherwise. ....
In C++ language. void printTreeIO(Tnode *n)(3): recursive function that prints out the data in the tree...
In C++ language. void printTreeIO(Tnode *n)(3): recursive function that prints out the data in the tree in order
C++ Class involving union. The goal is to overload the function: void Bag<T>::operator+=(const Bag<T>& a_bag) //...
C++ Class involving union. The goal is to overload the function: void Bag<T>::operator+=(const Bag<T>& a_bag) // The union of two sets A and B is the set of elements which are in A, in B, or in both A and B. For instance, Bag<int> bag1 = 1,2,3 and Bag<int> bag2 = 3,4,5 then bag1+=bag2 should return 1,2,3,4,5. //Since type is void, it should not return an array. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag...
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.
true or false C++ a.    (T or F)   A function or method cannot return a value of type...
true or false C++ a.    (T or F)   A function or method cannot return a value of type array. b.    (T or F)   C++ throws an exception if you try to refer to element 47 in an array with 20 elements. c.    (T or F)   I can copy one array to another by simply using an assignment statement which assigns the one array to the other. d.    (T or F)  An exception is the occurrence of an erroneous or unexpected situation during the execution of a program. e.    (T...
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.
Design algorithms for the following operations for a binary tree T: . preorderNext(p) : Return the...
Design algorithms for the following operations for a binary tree T: . preorderNext(p) : Return the position visited after p in a preorder traversal of T, or NULL if p is the last node visited. . inorderNext(p) : Return the position visited after p in an inorder traversal of T, or NULL if p is the last node visited. . postorderNext(p) : Return the position visited after p in a postorder traversal of T, or NULL if p is the...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT