Fill in Order Details

  • Submit paper details for free using our simple order form

Make Payment Securely

  • Add funds to your account. There are no upfront payments. The writer will only be paid once you have approved your paper

Writing Process

  • The best qualified expert writer is assigned to work on your order
  • Your paper is written to standard and delivered as per your instructions

Download your paper

  • Download the completed paper from your online account or your email
  • You can request a plagiarism and quality report along with your paper

help with c 1

In our study of binary search trees so far, we’ve looked at how to build and traverse one, but what happens when the tree becomes imbalanced? This is a situation we try to avoid. The more imbalanced the tree, the more our search approaches O(n) comparisons. This week we will look at types of self-balancing trees, such as AVL and 2-3 trees, which perform various operations on BSTs to maintain balance.

Assignment

AVL trees are balanced trees that track a balance factor (BF) in each node. This factor is calculated as (B – A), where B is the height of the right subtree and A is the height of the left subtree. The AVL algorithm performs rotations whenever this factor gets “out of whack” by more than 1 (that is, B – A is greater than 1 or less than -1), during an insert or delete operation.

Write a program that, given a series of integers to add to a BST, detects the point where an imbalance occurs in the tree.

Input

Use the file input.txt posted in this module, which contains 4 test cases (40 numbers total, one per line).

Requirements

  • A starter file with a simple framework for reading the input is here: balance_main.cpp or you can start with any BST code you choose (we’ve studied a few so far and there is at least one posted in Canvas).
  • The program should read a batch of 10 integers, do the inserts, and output the point where the insert causes an imbalance (where the BF for that node becomes greater than 1 or less than -1). The output should include both the value of the node where the imbalance occurs and which subtree (left or right).
  • We’re only looking at inserts in this assignment — no need to handle deletes. No classes or templates are necessary.
  • Your program must accept batches of 10 integers from a file and build a tree, until EOF. No programs will be accepted that hard-code outputs, do not read the file, or do not build a BST.
  • It is good practice to free all of the dynamic memory allocated for each batch (as the starter file does) but this is optional.
  • Duplicate values should be ignored.

Example Output

You should get the following output for the included test file.

Read 10 nodes.
Imbalance detected at node 71, left subtree.
Read 10 nodes.
Imbalance detected at node 9, right subtree.
Read 10 nodes.
Imbalance detected at node 96, left subtree.
Read 10 nodes.
Imbalance detected at node 97, right subtree.
Done.

input.txt:

71

7

32

38

99

19

82

27

2

85

58

9

59

40

36

2

62

73

3

87

45

96

31

52

40

78

30

66

94

91

44

95

31

52

40

97

30

99

98

7

balance_main.cpp:

#include <iostream>

#include <fstream>

#include <cstdlib>

using namespace std;

#define FILENAME “input.txt”

#define N_NODES 10

// A tree node

struct Node {

int val; // The (integer) value

int bf; // Balance factor of this node

Node *left; // Left child pointer

Node *right; // Right child pointer

};

// Insert a node into the tree, balancing if needed.

// *tp is a pointer to the root of the tree (reference variable);

// ‘nv’ is the new (integer) value to add.

// ‘allNodes’ is an array of all of the nodes we’ve allocated, for easy clean-up

// ‘allNodesN’ is the number of nodes set in allNodes

int insertNode(Node *&tp, int nv, Node *allNodes[], int allNodesN) {

Node *newp;

if (tp == 0) {

// Setup our new node

newp = new Node;

newp->val = nv;

newp->bf = 0;

newp->left = 0;

newp->right = 0;

tp = newp;

allNodes[allNodesN] = newp;

return (1);

}

// Ignore existing values

if (nv == tp->val)

return (0);

if (nv < tp->val) {

// Insert into the LEFT subtree

} else {

// Insert into the RIGHT subtree

}

}

int main() {

Node *rootp = 0; // Root of the tree

Node *allNodes[N_NODES]; // Bookkeeping array so we can free everything

string ent; // Input string from file

ifstream infile(FILENAME); // Input file

int nums[N_NODES]; // Input integers

int i, j, status;

while (true) {

// Read a set of insertions

i = 0, j = 0;

rootp = 0;

while (getline(infile, ent)) {

nums[i++] = atoi(ent.c_str());

if (i == N_NODES)

break;

}

if (i != N_NODES)

break;

cout << “Read ” << i << ” nodes.” << endl;

for (; j < N_NODES; j++) {

status = insertNode(rootp, nums[j], allNodes, j);

if (status == -1)

break;

}

if (j == N_NODES) {

cout << “No imbalances detected.” << endl;

}

// Free everything

for (int k = 0; k < j; k++) {

delete allNodes[k];

allNodes[k] = 0;

}

}

cout << “Done.” << endl;

return (0);

}

WHAT OUR CURRENT CUSTOMERS SAY

  • Google Rating
  • Sitejabber
  • Trustpilot
Zahraa S
Zahraa S
Absolutely spot on. I have had the best experience with Elite Academic Research and all my work have scored highly. Thank you for your professionalism and using expert writers with vast and outstanding knowledge in their fields. I highly recommend any day and time.
Stuart L
Stuart L
Thanks for keeping me sane for getting everything out of the way, I’ve been stuck working more than full time and balancing the rest but I’m glad you’ve been ensuring my school work is taken care of. I'll recommend Elite Academic Research to anyone who seeks quality academic help, thank you so much!
Mindi D
Mindi D
Brilliant writers and awesome support team. You can tell by the depth of research and the quality of work delivered that the writers care deeply about delivering that perfect grade.
Samuel Y
Samuel Y
I really appreciate the work all your amazing writers do to ensure that my papers are always delivered on time and always of the highest quality. I was at a crossroads last semester and I almost dropped out of school because of the many issues that were bombarding but I am glad a friend referred me to you guys. You came up big for me and continue to do so. I just wish I knew about your services earlier.
Cindy L
Cindy L
You can't fault the paper quality and speed of delivery. I have been using these guys for the past 3 years and I not even once have they ever failed me. They deliver properly researched papers way ahead of time. Each time I think I have had the best their professional writers surprise me with even better quality work. Elite Academic Research is a true Gem among essay writing companies.
Got an A and plagiarism percent was less than 10%! Thanks!

ORDER NOW

Consider Your Assignments Done

“All my friends and I are getting help from eliteacademicresearch. It’s every college student’s best kept secret!”

Jermaine Byrant
BSN

“I was apprehensive at first. But I must say it was a great experience and well worth the price. I got an A!”

Nicole Johnson
Finance & Economics

Our Top Experts

————-

See Why Our Clients Hire Us Again And Again!


OVER
10.3k
Reviews

RATING
4.89/5
Avg Rating

YEARS
12
Experience

Elite Academic Research Promises You:


Always on Time

If we are a minute late, the work is on us – it’s free!

Plagiarism-free

If the work we produce contains plagiarism we’ll pay out a £5,000 guarantee.

Quality

Providing quality work is core to our beliefs, which is why we will strive to give you exactly that, and more!

Written to Standard

All of our assignments go through a stringent quality checking process from start to finish.

Success Guarantee

When you order form the best, some of your greatest problems as a student are solved!

Reliable

Professional

Affordable

Quick

Using this writing service is legal and is not prohibited by any law, university or college policies. Services of Elite Academic Research are provided for research and study purposes only with the intent to help students improve their writing and academic experience. We do not condone or encourage cheating, academic dishonesty, or any form of plagiarism. Our original, plagiarism-free, zero-AI expert samples should only be used as references. It is your responsibility to cite any outside sources appropriately. This service will be useful for students looking for quick, reliable, and efficient online class-help on a variety of topics.