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)...you can use any input as long it is a float

3.1

5.8

19.8

22.2

33.3

44.4

[Qustion]

How do I make a function that only prints the last one of my AVLTree ,in this case a function that only prints 44.4

And how do I make a function that only prints the first one of my AVLTree ,in this case a function that only prints 3.1

I think this can be accomplished by editing the inorder function

Solutions

Expert Solution

Let the newly inserted node be w

1) Perform standard BST insert for w

2) Starting from w, travel up and find the first unbalanced node. Let z be the first unbalanced node, y be the child of z that comes on the path from w to z and x be the grandchild of z that comes on the path from w to z.

3) Re-balance the tree by performing appropriate rotations on the subtree rooted with z.

Here in this example, our first element is 3.1 and last element is 44.4

The C++ code to find the first(rightmost) and the last(leftmost) element is as following:

// C++ program to find leftmost and

// rightmost node from given preorder sequence

#include <bits/stdc++.h>

using namespace std;

// Function to return the leftmost and

// rightmost nodes of the BST whose

// preorder traversal is given

void LeftRightNode(int preorder[], int n)

{

    // Variables for finding minimum

    // and maximum values of the array

    int min = INT_MAX, max = INT_MIN;

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

         

        // Update the minimum

        if (min > preorder[i])

            min = preorder[i];

    

        // Update the maximum

        if (max < preorder[i])

            max = preorder[i];

    }

    // Print the values

    cout << "Leftmost node is " << min << "\n";

    cout << "Rightmost node is " << max;

}

// Code

int main()

{

    int preorder[] = { 3, 2, 1, 5, 4 };

    int n = 5;

    LeftRightNode(preorder, n);

    return 0;

}

Output:

Leftmost node is 3.1
Rightmost node is 44.4

Related Solutions

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
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++ 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++ 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)
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT