Question

Write a C++ program that checks whether a binary search tree is an AVL. The input...

Write a C++ program that checks whether a binary search tree is an AVL. The input is an arbitrary binary search tree, and the output is binary, so either true or false.

Homework Answers

Answer #1

//For checking given tree is AVL or not we need to find left height and right height of each node and difference between left and right height should not be more than 1.

//AVL is balanaced binary serach tree.

//height() is used to find height of given node
int height(nod* node) 
{ 
   if(node == NULL)
      return 0;
   return 1 + max(height(node->left), height(node->right));
}

//AVL () :Check given BST is AVL or not.

bool AVL(nod *root) {
   int lh;
   int rh;
   if(root == NULL)
      return 1;
   lh = height(root->left); // left height
   rh = height(root->right); // right height
   if(abs(lh-rh) <= 1 && AVL(root->left) && AVL(root->right)) 
       return true;
   return false;
}
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
a) Design a recursive linear-time algorithm that tests whether a binary tree is a binary search...
a) Design a recursive linear-time algorithm that tests whether a binary tree is a binary search tree. Describe your algorithm in English or with a simple pseudocode program. b) (3 bonus pts.) Extend the algorithm in a) to test whether a binary tree is an AVL tree.
Binary Search Tree Code in Java Write a program that prompts user to enter however many...
Binary Search Tree Code in Java Write a program that prompts user to enter however many numbers they want to add to the binary search tree. Then have the user input numbers until the tree contains that many elements. If the user enters a number that already exists in the tree, then a message should be displayed "Number is already in the tree". Once all elements are added to the tree print its contents in sorted order. Assume all user...
Write a program to allow user to create a binary search tree. Your program should display...
Write a program to allow user to create a binary search tree. Your program should display in BFT and DFT(in order) format.
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
C++ program checks if an input string is a palindrome or not. Ignore case and remove...
C++ program checks if an input string is a palindrome or not. Ignore case and remove all non-alphanumeric characters in the input string. C++ program uses only one string (no extra-string or array). And also, use two indices to point from the beginning and ending positions. Sample Input 0 Race car Sample Output 0 TRUE Sample Input 1 7...8 Don't nod 78. Sample Output 1 FALSE
Put these integers into a binary search tree and then state the output of a postorder...
Put these integers into a binary search tree and then state the output of a postorder traversal. Put EXACTLY ONE SPACE between each integer, so your output looks like this: 1 2 3 4 5 These are the integers to put into the tree: 41 17 80 25 8 11 50 60 100 Output: ____
Put these integers into a binary search tree and then state the output of a preorder...
Put these integers into a binary search tree and then state the output of a preorder traversal. Put EXACTLY ONE SPACE between each integer, so your output looks like this: 1 2 3 4 5 These are the integers to put into the tree: 41 17 80 25 8 11 50 60 100 Output: ____
This assignment involves using a binary search tree (BST) to keep track of all words in...
This assignment involves using a binary search tree (BST) to keep track of all words in a text document. It produces a cross-reference, or a concordance. This is very much like assignment 4, except that you must use a different data structure. You may use some of the code you wrote for that assignment, such as input parsing, for this one. Remember that in a binary search tree, the value to the left of the root is less than the...
Write code in java Implement a method to build an AVL tree out of a sorted...
Write code in java Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You...
Write a matlab program that takee a character (a-z) as an input and checks if that...
Write a matlab program that takee a character (a-z) as an input and checks if that is a vowel or not using switch statment? If the user inputs any other character, the program should display a proper error message.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT