-- > In C++ write a program without using classes, and build in functions, only use structure.!!!!!! Populate a tree via a text file (input.txt) Make sure that after every insert, the tree is balanced. At the end, display the tree in level format. Make sure to include the height and the balance factor of every node in your output. Redirect the display to an output file (output.txt) Hint: //I will not accept any other algorithm //This is not a recursive algorithm node * rebalance(node *node){ node->height = max(height(node->left), height(node->right)) + 1; int balance = getBalance(node); //node->left - node->right /*do rotations as necessary If Left heavy outside : return rightRotate(node); If right heavy outside: return leftRotate(node); If left heavy inside: left rotation first, right rotation 2nd, return top node node->left = leftRotate(node->left); return rightRotate(node); if right heavy inside: right rotation first, left rotation 2nd, return top node node->right = rightRotate(node->right); return leftRotate(node); if no rotation, return node */ } //non-tail recursive algorithm because of rebalance node* insert(node* node, int key) { //recursive Code for inserting a node //When insert happens set height to 0 for the node if (node == NULL) return(newNode(key)); if (key < node->key) node->left = insert(node->left, key); else node->right = insert(node->right, key); node=rebalance(node); //update heights and rebalance return node; }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

-- > In C++ write a program without using classes, and build in functions, only use structure.!!!!!!

Populate a tree via a text file (input.txt) Make sure that after every insert, the tree is balanced.
At the end, display the tree in level format. Make sure to include the height and the balance
factor of every node in your output. Redirect the display to an output file (output.txt)
Hint:
//I will not accept any other algorithm
//This is not a recursive algorithm
node * rebalance(node *node){
node->height = max(height(node->left), height(node->right)) + 1;
int balance = getBalance(node); //node->left - node->right

/*do rotations as necessary
If Left heavy outside : return rightRotate(node);
If right heavy outside: return leftRotate(node);
If left heavy inside: left rotation first, right rotation 2nd, return top node
node->left = leftRotate(node->left);
return rightRotate(node);
if right heavy inside: right rotation first, left rotation 2nd, return top node
node->right = rightRotate(node->right);
return leftRotate(node);
if no rotation, return node
*/
}
//non-tail recursive algorithm because of rebalance
node* insert(node* node, int key)
{
//recursive Code for inserting a node
//When insert happens set height to 0 for the node
if (node == NULL)
return(newNode(key));

if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
node=rebalance(node); //update heights and rebalance
return node;
}

 
 
 
 
 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Types of trees
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY