Question

In: Computer Science

Design a linear-time algorithm that verifies that the height information in an AVL tree is correctly...

Design a linear-time algorithm that verifies that the height information in an AVL tree is correctly maintained and that the balance property is in order. (C++)

Solutions

Expert Solution

#include <bits/stdc++.h>

using namespace std;

#define bool int

class node {

public:

    int data;

    node* left;

    node* right;

};

bool isBalanced(node* root, int* height)

{

    int lh = 0, rh = 0;

    int l = 0, r = 0;

    if (root == NULL) {

        *height = 0;

        return 1;

    }

    l = isBalanced(root->left, &lh);

    r = isBalanced(root->right, &rh);

    *height = (lh > rh ? lh : rh) + 1;

    if (abs(lh - rh) >= 2)

        return 0;

    else

        return l && r;

}

node* newNode(int data)

{

    node* Node = new node();

    Node->data = data;

    Node->left = NULL;

    Node->right = NULL;

    return (Node);

}

int main()

{

    int height = 0;

    node* root = newNode(1);

    root->left = newNode(2);

    root->right = newNode(3);

    root->left->left = newNode(4);

    root->left->right = newNode(5);

    root->right->left = newNode(6);

    root->left->left->left = newNode(7);

    if (isBalanced(root, &height))

        cout << "Tree is balanced";

    else

        cout << "Tree is not balanced";

    return 0;

}


Related Solutions

Write code for O(n) worst-case algorithm that verifies that a tree is actually an AVL tree by detecting imbalance.
Write code for O(n) worst-case algorithm that verifies that a tree is actually an AVL tree by detecting imbalance. If imbalance is true, then call the proper rotation function provided in the lecture slides to fix the imbalance condition.1. Must read AVLNode data from a text file2. Create a Book Object; and an AVL node object to be inserted into the AVL tree3. At each insert, detect imbalance and fix the AVL tree4. Report each imbalance detection and the node...
Course: DSA Data Structure and Algorithm What is an AVL Tree, what are its different types...
Course: DSA Data Structure and Algorithm What is an AVL Tree, what are its different types of rotation, Illustrate with examples.
Design a linear-time algorithm which, given an undirected graph G and a particular edge e in...
Design a linear-time algorithm which, given an undirected graph G and a particular edge e in it, determines whether G has a cycle containing e. Your algorithm should also return the length (number of edges) of the shortest cycle containing e, if one exists. Just give the algorithm, no proofs are necessary. Hint: you can use BFS to solve this.
Let X = {x1,x2,...,xn} a sequence of real numbers. Design an algorithm that in linear time...
Let X = {x1,x2,...,xn} a sequence of real numbers. Design an algorithm that in linear time finds the continue subsequence of elements xi,xi+1,...,x, which product is the maximum. Suppose that the product of an empty subsequence is 1 and observe that the values can be less to 0 and less to 1.
Pseudocode and algorithm of finding median of unordered array in linear time.
Pseudocode and algorithm of finding median of unordered array in linear time.
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n)...
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n) algorithm to find the duplicates between two sorted arrays (m, n are the sizes of two arrays). For instance, for inputs {1,3,5,7} and {1,2,3,4}, the correct outputs should be {1,3
Design a nonrecursive post order traversal algorithm. 1.Use the linked representation for the binary tree to...
Design a nonrecursive post order traversal algorithm. 1.Use the linked representation for the binary tree to be traversed. 2.Except left, right, and data fields, there are no other fields in a node. 3.After the traversal the tree should remain intact. 4.Use only one stack. 5.You can’t push anything other than nodes into the stack.
Recall our linear-time deterministic selection algorithm, SELECT. Give the pseudocode for a modified Quicksort algorithm, call...
Recall our linear-time deterministic selection algorithm, SELECT. Give the pseudocode for a modified Quicksort algorithm, call it AWESOME-QUICKSORT , that has worst-case run time O(nlogn). State the cost of your algorithm using a recurrence T(n), and solve then solve this recurrence any way you like (e.g., master method).
Describe in pseudo-code, a linear-time algorithm for reversing a queue Q. To access the queue, you...
Describe in pseudo-code, a linear-time algorithm for reversing a queue Q. To access the queue, you are only allowed to use the basic functions of the queue ADT defined as follows (Hint: Using a stack, the basic stack functions defined in the textbook and in the class). class Queue { public: int size(); bool isEmpty(); Object front(); void enqueue(Object o); Object dequeue(); };
Who are the five scientists who came up with the linear time selection algorithm? How many...
Who are the five scientists who came up with the linear time selection algorithm? How many of them are still alive? What is the title of their original paper? What basis did they have for thinking that a linear time algorithm existed for this problem?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT