Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

AVL Tree Creation

main.cpp

#include <fstream>
#include <iostream>
#include <cmath>
#include <time.h>
#include <stack>
#include <queue>

#include "AVLTree.h"

using namespace std;


int main() {

AVLTree* tree1Root = new AVLTree(50, nullptr);
srand(time(NULL));
uint32_t numNodes = 10;
for (uint32_t i=1; i < numNodes; i++ ) {
tree1Root = tree1Root->insert(( rand() % 10000));

//Uncomment to help debug lost nodes
// if (tree1Root->countNodes() != i+2) {
// std::cout<<"Lost node "<<std::endl;
// return 1;
// }

//uncomment to help debug unbalanced trees
// tree1Root->updateHeight();
// if ( ! tree1Root->isBalanced() ) {
// std::cout<<"Tree1Root balanced: FAILED at node insertion "<<i<<std::endl;
// return 1;
// }

}

if (tree1Root->countNodes() == numNodes) {
std::cout<<"tree1Root lost Nodes: PASSED"<<std::endl;
}
else {
std::cout<<"tree1Root lost Nodes: FAILED expected: 100 actual: "<<tree1Root->countNodes()<<std::endl;
}

tree1Root->updateHeight();
float expectedHeight = log2(numNodes) * 1.5;
if (tree1Root->getHeight() < expectedHeight) {
std::cout<<"tree1Root height: PASSED"<<std::endl;
}
else {
std::cout<<"tree1Root height: FAILED expected: <" <<expectedHeight<<" actual: "<<tree1Root->getHeight()<<std::endl;
}

if ( tree1Root->isBalanced()) {
std::cout<<"Tree1Root is balanced: PASSED"<<std::endl;
}
else {
std::cout<<"Tree1Root is balanced: FAILED"<<std::endl;
}
}

AVLTree.cpp

#include "AVLTree.h"

#include <iostream>

using namespace std;
//************** already implemented helper functions
AVLTree::AVLTree(int t_data, AVLTree* t_parent, AVLTree* t_left, AVLTree* t_right) {
data = t_data;
height = 0;
parent = t_parent;
left = t_left;
right = t_right;
}

bool AVLTree::isLeaf() {
//insert code here
return ((left == nullptr) and (right == nullptr));
}

uint32_t AVLTree::getHeight() {
return height;
}

//*******

int AVLTree::getBalance() {
//insert code here
}

AVLTree* AVLTree::rotateRight() {
//insert code here
}

AVLTree* AVLTree::rotateLeft() {
//insert code here
}

AVLTree* AVLTree::rebalance() {
//insert code here
}

AVLTree* AVLTree::insert(int new_data) {
//insert code here to insert and rebalance tree
}


//***************************
//Do not edit code below here
uint32_t AVLTree::countNodes() {
//insert code here
if (isLeaf()) {
return 1;
}
if (left != nullptr) {
if (right != nullptr) {
return 1 + left->countNodes() + right->countNodes();
}
return 1+ left->countNodes();
}
return 1 + right->countNodes();
}

void AVLTree::updateHeight() {
//insert code here
if (isLeaf()) {
height = 0;
return;
}
if (left != nullptr) {
left->updateHeight();
if (right != nullptr) {
right->updateHeight();
height = (1 + max(left->getHeight(), right->getHeight()));
return;
}
height = 1 + left->getHeight();
return;
}
right->updateHeight();
height = 1 + right->getHeight();
return;
}

bool AVLTree::isBalanced() {
if ( isLeaf() ) {
return true;
}
if (left == nullptr) {
return ( right->getHeight() < 1 );
}
if (right == nullptr) {
return ( left->getHeight() < 1 );
}
return ( left->isBalanced() and right->isBalanced() and abs(getBalance() < 2) );
}

AVLTree.h

#include <iostream>

class AVLTree {
public:
int data;
uint32_t height;
AVLTree* parent;
AVLTree* left;
AVLTree* right;

//base functions defined for you
AVLTree(int data, AVLTree* parent=nullptr, AVLTree* left=nullptr, AVLTree* right=nullptr);
bool isLeaf();
uint32_t getHeight();

//*******************
//functions you need to define

//insert a node and rebalance tree to maintain AVL balanced tree
//return new root of tree
AVLTree* insert(int data);

//computes a node's balance factor by subtracting the right subtree height from the left subtree height.
int getBalance();

//checks for imbalance and rebalances if neccessary
//return new root of tree
AVLTree* rebalance();


//implement a right rotate
//return new root of tree
AVLTree* rotateRight();

//implement a left rotate
//return new root of tree
AVLTree* rotateLeft();

//Do not edit these three functions
bool isBalanced();
uint32_t countNodes();
void updateHeight();
};

[画像:This week we are going to continue our development of trees and make a balanced AVL tree. The functions you will need are stubbed out in the AVLTree header file. Specifically you need to implement the insert function that will insert a new value and rebalance the tree if neccesary. In order to test for balance you will need to implement the getBalance() function. In order to rebalance it will be neccesary for you to implement both the rotateRight() and rotateLeft() functions. I have implemented some of the functions you created last week. There is a testbench included which will help you to test your code. I have gotten you started with a 10 element tree - you should try out larger numNodes to insure that your code is functioning as intended at higher values of n once you are done developing. Do not edit the function headers. Do not use global variables. Any algorithms that operate on more than one level of the tree should be recursive in nature. This program will take longer than most of the ones you have done so far this semester, get started early. ]
expand button
Transcribed Image Text:This week we are going to continue our development of trees and make a balanced AVL tree. The functions you will need are stubbed out in the AVLTree header file. Specifically you need to implement the insert function that will insert a new value and rebalance the tree if neccesary. In order to test for balance you will need to implement the getBalance() function. In order to rebalance it will be neccesary for you to implement both the rotateRight() and rotateLeft() functions. I have implemented some of the functions you created last week. There is a testbench included which will help you to test your code. I have gotten you started with a 10 element tree - you should try out larger numNodes to insure that your code is functioning as intended at higher values of n once you are done developing. Do not edit the function headers. Do not use global variables. Any algorithms that operate on more than one level of the tree should be recursive in nature. This program will take longer than most of the ones you have done so far this semester, get started early.
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education