Question

create a getBalFactor that calculates the balance factor of a BINARY TREE in C++

create a getBalFactor that calculates the balance factor of a BINARY TREE
in C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

BALANCE FACTOR -

BalanceFactor = height(left-sutree) − height(right-sutree)

Here is the method using which you can print Balance factor of each internal node of binary tree.

int printBalanceFactor(struct Node* t)
{
if (t == null)
return 0;

if (t->left == null && t->right == null)
return 1;

int heightL = printBalanceFactor(t->left);
int heightR = printBalanceFactor(t->right);

cout<<"Balance factor of "<< t " is "<<(heightL - heightR);

return (heightL + heightR + 1);
}

/*

I have used a structure for binary tree

struct Node

{

    int data;

    struct Node* left, *right;

    Node(int data)

    {

        this->data = data;

        left = right = NULL;

    }

};

*/

REMARKS-: if you find any difficulties in understanding the solution please comment down

I will be more than happy to help you

happy learning.....

Add a comment
Know the answer?
Add Answer to:
create a getBalFactor that calculates the balance factor of a BINARY TREE in C++
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given the follow Binary Search Tree (AVL Tree). Show the balance factor for each node. Is...

    Given the follow Binary Search Tree (AVL Tree). Show the balance factor for each node. Is this binary tree balanced? If not which nodes would have to be removed to make it balanced?   

  • [DSW] Create a balanced binary tree from the tree in figure 1 using DSW algorithm. Show...

    [DSW] Create a balanced binary tree from the tree in figure 1 using DSW algorithm. Show step-by-step process including the process of creating backbone and    perfectly balanced tree   [AVL] Delete node 9 from tree in figure 1, then determine balance factor for each remaining node, and create a balanced AVL tree from it.    Delete node 3 from tree in figure 1 by using Delete-by-Copying procedure, determine balance factor for each remaining node, and create a balanced AVL tree...

  • Needed in Java Code Create a Binary Search Tree with the following elements in the order...

    Needed in Java Code Create a Binary Search Tree with the following elements in the order mentioned below: 5, 85, 89, 3, 2, 8, 65, 92 1.Print the Pre-order of this tree 2. Print the height and the balance factor of the nodes in the order they were inserted (5, 85, 89, 3, 2, 8, 65, 92) in the form of a table with three columns and 9 rows. Use column headers “Node”, “Height”, and “Balance Factor” for the three...

  • LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display...

    LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display the output using inorder, preorder and postorder tree traversal methods.

  • c++ Write a class that will create a binary tree that can hold values of string...

    c++ Write a class that will create a binary tree that can hold values of string data type, define and implement insert, delete, search, functions, and a constructor and a destructor. Demonstrate the insertion, searching, and deletion functions in a program.

  • PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a...

    PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a class called MyTree with the methods __init__(x), getLeft(), getRight(), getData(), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node's position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a...

  • C++ Write a function, singleParent, that returns the number of nodes in a binary tree that...

    C++ Write a function, singleParent, that returns the number of nodes in a binary tree that have only one child. Add this function to the class binaryTreeType and create a program to test this function. (Note: First create a binary search tree.)

  • Create a binary search tree for 6 randomly chosen vegetables.

    Create a binary search tree for 6 randomly chosen vegetables.

  • Create a binary search tree in C++, where it will have an add_node(int i) function which...

    Create a binary search tree in C++, where it will have an add_node(int i) function which when called in the main (i.e. add_node(5)) it will add it to the tree. This tree will also place the nodes added in the correct order (if the head node is 40, all the nodes that are added that are less than 40 go to the left of the tree, and all the nodes larger than 40 go to the right) Also add a...

  • A Binary Search Tree is a binary tree where nodes are ordered in the following way:...

    A Binary Search Tree is a binary tree where nodes are ordered in the following way: each node contains one key (also known as data) the keys in the left subtree are less than the key in its parent node the keys in the right subtree are greater than the key in its parent node duplicate keys are not allowed Create a Binary Search Tree inserting the following list of numbers in order from left to right. 10,          6,           4,            8,            18,          15,          21 Please type...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT