Related questions
Concept explainers
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();
};
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
- struct insert_at_back_of_sll { // Function takes a constant Book as a parameter, inserts that book at the // back of a singly linked list, and returns nothing. void operator()(const Book& book) { /// TO-DO (3) /// // Write the lines of code to insert "book" at the back of "my_sll". Since // the SLL has no size() function and no tail pointer, you must walk the // list looking for the last node. // // HINT: Do not attempt to insert after "my_sll.end()". // ///// END-T0-DO (3) ||||// } std::forward_list& my_sll; };arrow_forward#ifndef BT_NODE_H#define BT_NODE_H struct btNode{ int data; btNode* left; btNode* right;}; // pre: bst_root is root pointer of a binary search tree (may be 0 for// empty tree) and portArray has the base address of an array large// enough to hold all the data items in the binary search tree// post: The binary search tree has been traversed in-order and the data// values are written (as they are encountered) to portArray in// increasing positional order starting from the first elementvoid portToArrayInOrder(btNode* bst_root, int* portArray);void portToArrayInOrderAux(btNode* bst_root, int* portArray, int& portIndex); // pre: (none)// post: dynamic memory of all the nodes of the tree rooted at root has been// freed up (returned back to heap/freestore) and the tree is now empty// (root pointer contains the null address)void tree_clear(btNode*& root); // pre: (none)// post: # of nodes contained in tree rooted at root is returnedint...arrow_forwardStack Implementation in C++make code for an application that uses the StackX class to create a stack.includes a brief main() code to test this class.arrow_forward
- In this code: #include <iostream> #include <string> using namespace std;// Node struct to store data for each song in the playliststruct Node { string data; Node * next;};// Function to create a new node and return its addressNode * getNewNode(string song) { Node * newNode = new Node(); newNode -> data = song; newNode -> next = NULL; return newNode;}// Function to insert a new node at the head of the linked listvoid insertAtHead(Node ** head, string song) { Node * newNode = getNewNode(song); if ( * head == NULL) { * head = newNode; return;} newNode -> next = * head; * head = newNode;}// Function to insert a new node at the tail of the linked listvoid insertAtTail(Node ** head, string song) { Node * newNode = getNewNode(song); if ( * head == NULL) { * head = newNode; return; } Node * temp = * head; while (temp -> next != NULL) { temp = temp -> next; } temp -> next = newNode;}// Function to remove a node from the linked listvoid removeNode(Node ** head,...arrow_forwardPython binary search tree: a function that takes in a root, p, and checks whether the tree rooted in p is a binary search tree or not. What is the time complexity of your function? def is_bst(self, p: Node):arrow_forwardC programming I need help writing a code that uses a struct pointer into a binary tree and using the same pointer into an arrayarrow_forward
- #ifndef NODES_LLOLL_H#define NODES_LLOLL_H #include <iostream> // for ostream namespace CS3358_SP2023_A5P2{ // child node struct CNode { int data; CNode* link; }; // parent node struct PNode { CNode* data; PNode* link; }; // toolkit functions for LLoLL based on above node definitions void Destroy_cList(CNode*& cListHead); void Destroy_pList(PNode*& pListHead); void ShowAll_DF(PNode* pListHead, std::ostream& outs); void ShowAll_BF(PNode* pListHead, std::ostream& outs);} #endif #include "nodes_LLoLL.h"#include "cnPtrQueue.h"#include <iostream>using namespace std; namespace CS3358_SP2023_A5P2{ // do breadth-first (level) traversal and print data void ShowAll_BF(PNode* pListHead, ostream& outs) { cnPtrQueue queue; CNode* currentNode; queue.push(lloLLPtr->getHead()); while (!queue.empty()) { currentNode = queue.front(); queue.pop(); if...arrow_forwardStack: Stacks are a type of container with LIFO (Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only. Your Stack should not be of the fixed sized. It should be able to grow itself. bool empty() : Returns whether the Stack is empty or not. Time Complexity should be: O(1) bool full() : Returns whether the Stack is full or not. Time Complexity should be: O(1)int size() : Returns the current size of the Stack. Time Complexity should be: O(1)Type top () : Returns the last element of the Stack. Time Complexity should be: O(1) void push(Type) : Adds the element of type Type at the top of the stack. Time Complexity should be: O(1) Type pop() : Deletes the top most element of the stack and returns it. Time Complexity should be: O(1) Write non-parameterized constructor for the above class. Write Copy constructor for the above class. Write Destructor for the above class. Now write a global function show stack which...arrow_forwardAVL Tree Implementation program Important note: You must separate your program into three files: header file (*.h), implementation file (*.cpp) and main file (*.cpp). Write a program to build an AVL tree by accepting the integers input from users. For each input, balance the tree and display it on the screen; you then calculate the indorder traversals as well. There should be a menu to drive the program. It should be similar as follows: AVL Tree Implementation A: Insert an integer to tree and show the balanced tree at each insertion. B: Display the balanced tree and show inorder traversal. C: Exit To be sure, your program is correctly working, use the following data to test AVL tree: 15, 18, 10, 7, 57, 6, 13, 12, 9, 65, 19, 16, 23 You should perform more test with different data sets.arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education