SlideShare a Scribd company logo
1 of 9
Download to read offline
Modify this code to do an Insert function for an AVL tree, instead of a B-Tree .
Please compile in Microsoft Visual studio, as it is the IDE that is required for my course.
Here is the code for the header file and the Source file.
Soure File :
#include
#include "bTree.h"
using namespace std;
int main()
{
bTree treeRoot;
int num;
cout << "Enter numbers ending with -999" << endl;
cin >> num;
while (num != -999)
{
treeRoot.insert(num);
cin >> num;
}
cout << "Inorder traversal data: ";
treeRoot.inOrder();
cout << endl;
return 0;
Header File:
#ifndef H_bTree
#define H_bTree
#include
#include
using namespace std;
//*************************************************************
// Author: D.S. Malik
//
// class bTree
// This class specifies the basic operations to implement a
// B-tree.
//*************************************************************
template
struct bTreeNode
{
int recCount;
recType list[bTreeOrder - 1];
bTreeNode *children[bTreeOrder];
};
template
class bTree
{
public:
bool search(const recType& searchItem);
//Function to determine if searchItem is in the B-tree.
//Postcondition: Returns true if searchItem is found in the
// B-tree; otherwise, returns false.
void insert(const recType& insertItem);
//Function to insert insertItem in the B-tree.
//Postcondition: If insertItem is not in the the B-tree, it
// is inserted in the B-tree.
void inOrder();
//Function to do an inorder traversal of the B-tree.
bTree();
//constructor
//Add additional members as needed
protected:
bTreeNode *root;
private:
void searchNode(bTreeNode *current,
const recType& item,
bool& found, int& location);
void insertBTree(bTreeNode *current,
const recType& insertItem,
recType& median,
bTreeNode* &rightChild,
bool& isTaller);
void insertNode(bTreeNode *current,
const recType& insertItem,
bTreeNode* &rightChild,
int insertPosition);
void splitNode (bTreeNode *current,
const recType& insertItem,
bTreeNode* rightChild,
int insertPosition,
bTreeNode* &rightNode,
recType &median);
void recInorder(bTreeNode *current);
};
template
bTree::bTree()
{
root = NULL;
} //end constructor
template
bool bTree::search(const recType& searchItem)
{
bool found = false;
int location;
bTreeNode *current;
current = root;
while (current != NULL && !found)
{
searchNode(current, item, found, location);
if (!found)
current = current->children[location];
}
return found;
} //end search
template
void bTree::searchNode
(bTreeNode *current,
const recType& item,
bool& found, int& location)
{
location = 0;
while (location < current->recCount
&& item > current->list[location])
location++;
if (location < current->recCount
&& item == current->list[location])
found = true;
else
found = false;
} //end searchNode
template
void bTree::insert(const recType& insertItem)
{
bool isTaller = false;
recType median;
bTreeNode *rightChild;
insertBTree(root, insertItem, median,
rightChild, isTaller);
if (isTaller) //the tree is initially empty or the root
//was split by the function insertBTree
{
bTreeNode *tempRoot;
tempRoot = new bTreeNode;
tempRoot->recCount = 1;
tempRoot->list[0] = median;
tempRoot->children[0] = root;
tempRoot->children[1] = rightChild;
root = tempRoot;
}
} //insert
template
void bTree::insertBTree
(bTreeNode *current,
const recType& insertItem,
recType& median,
bTreeNode* &rightChild,
bool& isTaller)
{
int position;
isTaller = false;
if (current == NULL)
{
median = insertItem;
rightChild = NULL;
isTaller = true;
}
else
{
bool found;
searchNode(current, insertItem, found, position);
if (found)
cout << "Cannot insert duplicate record." << endl;
else
{
recType newMedian;
bTreeNode *newChild;
insertBTree(current->children[position], insertItem,
newMedian, newChild, isTaller);
if (isTaller)
{
if (current->recCount < bTreeOrder - 1)
{
isTaller = false;
insertNode(current, newMedian,
newChild, position);
}
else
splitNode(current, newMedian, newChild,
position, rightChild, median);
}
}
}
} //insertBTree
template
void bTree::insertNode
(bTreeNode *current,
const recType& insertItem,
bTreeNode* &rightChild,
int insertPosition)
{
int index;
for (index = current->recCount; index > insertPosition;
index--)
{
current->list[index] = current->list[index - 1];
current->children[index + 1] = current->children[index];
}
current->list[index] = insertItem;
current->children[index + 1] = rightChild;
current->recCount++;
} //end insertNode
template
void bTree::splitNode
(bTreeNode *current,
const recType& insertItem,
bTreeNode* rightChild,
int insertPosition,
bTreeNode* &rightNode,
recType &median)
{
rightNode = new bTreeNode;
int mid = (bTreeOrder - 1) / 2;
if (insertPosition <= mid) //new item goes in the first
//half of the node
{
int index = 0;
int i = mid;
while (i < bTreeOrder - 1)
{
rightNode->list[index] = current->list[i];
rightNode->children[index + 1] =
current->children[i + 1];
index++;
i++;
}
current->recCount = mid;
insertNode(current, insertItem, rightChild,
insertPosition);
(current->recCount)--;
median = current->list[current->recCount];
rightNode->recCount = index;
rightNode->children[0] =
current->children[current->recCount + 1];
}
else //new item goes in the second half of the node
{
int i = mid + 1;
int index = 0;
while (i < bTreeOrder - 1)
{
rightNode->list[index] = current->list[i];
rightNode->children[index + 1] =
current->children[i + 1];
index++;
i++;
}
current->recCount = mid;
rightNode->recCount = index;
median = current->list[mid];
insertNode(rightNode, insertItem, rightChild,
insertPosition - mid - 1);
rightNode->children[0] =
current->children[current->recCount + 1];
}
} //splitNode
template
void bTree::inOrder()
{
recInorder(root);
} // end inOrder
template
void bTree::recInorder
(bTreeNode *current)
{
if (current != NULL)
{
recInorder(current->children[0]);
for (int i = 0; i < current->recCount; i++)
{
cout << current->list[i] << " ";
recInorder(current->children[i + 1]);
}
}
} //end recInorder
#endif
Solution
In a binary tree the balance issue of a node N is described to be the height distinction
BalanceFactor(N) := –peak(LeftSubtree(N)) + height(RightSubtree(N)) [6]
of its infant subtrees. A binary tree is defined to be an AVL tree if the invariant
BalanceFactor(N) –1,zero,+1
holds for each node N in the tree.
A node N with BalanceFactor(N) < 0 is called "left-heavy", one with BalanceFactor(N) > 0 is
known as "proper-heavy", and one with BalanceFactor(N) = 0 is once in a while clearly called
"balanced".
statement
inside the sequel, because there's a one-to-one correspondence among nodes and the subtrees
rooted via them, we every so often depart it to the context whether the call of an object stands for
the node or the subtree.
properties[edit]
balance factors can be saved up to date through knowing the preceding balance elements and the
exchange in top – it is not important to recognize absolutely the height. for containing the AVL
balance statistics, two bits in keeping with node are enough.[7]
the height h of an AVL tree with n nodes lies within the c programming language:[8]
log2(n+1) h < c log2(n+2)+b
with the golden ratio := (1+5) 2 1.618, c := 1 log2 1.44, and b := c2 log2 5 – 2 –zero.328. that
is due to the fact an AVL tree of top h includes at the least Fh+2 – 1 nodes where Fh is the
Fibonacci collection with the seed values F1 = 1, F2 = 1.

More Related Content

Similar to Modify this code to do an Insert function for an AVL tree, instead o.pdf

Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfillyasraja7
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxtienlivick
 
In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfcharanjit1717
 
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfAssignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfFootageetoffe16
 
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docxAssg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docxfestockton
 
Write a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfWrite a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfSANDEEPARIHANT
 
Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdffederaleyecare
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxarjurakibulhasanrrr7
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdfpetercoiffeur18
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfhadpadrrajeshh
 
DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxVeerannaKotagi1
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxdebishakespeare
 
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
C++ BinaryTree Help  Creating main function for Trees...Here are .pdfC++ BinaryTree Help  Creating main function for Trees...Here are .pdf
C++ BinaryTree Help Creating main function for Trees...Here are .pdfforecastfashions
 
Using the following definition for a Binary Tree Node - complete the f.docx
Using the following definition for a Binary Tree Node - complete the f.docxUsing the following definition for a Binary Tree Node - complete the f.docx
Using the following definition for a Binary Tree Node - complete the f.docxslyndon
 
Extbase and Beyond
Extbase and BeyondExtbase and Beyond
Extbase and BeyondJochen Rau
 
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
Please help write BinaryTree-java Thank you!   Create a class BinaryTr.pdfPlease help write BinaryTree-java Thank you!   Create a class BinaryTr.pdf
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdfinfo750646
 

Similar to Modify this code to do an Insert function for an AVL tree, instead o.pdf (20)

Binary Tree
Binary  TreeBinary  Tree
Binary Tree
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdf
 
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfAssignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
 
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docxAssg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
Assg 12 Binary Search TreesCOSC 2336 Spring 2019April.docx
 
Write a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdfWrite a C program that reads the words the user types at the command.pdf
Write a C program that reads the words the user types at the command.pdf
 
Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdf
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdf
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docx
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docx
 
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
C++ BinaryTree Help  Creating main function for Trees...Here are .pdfC++ BinaryTree Help  Creating main function for Trees...Here are .pdf
C++ BinaryTree Help Creating main function for Trees...Here are .pdf
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
Using the following definition for a Binary Tree Node - complete the f.docx
Using the following definition for a Binary Tree Node - complete the f.docxUsing the following definition for a Binary Tree Node - complete the f.docx
Using the following definition for a Binary Tree Node - complete the f.docx
 
Extbase and Beyond
Extbase and BeyondExtbase and Beyond
Extbase and Beyond
 
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
Please help write BinaryTree-java Thank you!   Create a class BinaryTr.pdfPlease help write BinaryTree-java Thank you!   Create a class BinaryTr.pdf
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
 

More from fathimaoptical

Define a knowledge-based agent, and describe its major components. W.pdf
Define a knowledge-based agent, and describe its major components. W.pdfDefine a knowledge-based agent, and describe its major components. W.pdf
Define a knowledge-based agent, and describe its major components. W.pdffathimaoptical
 
Create a function mean comp that compares the mean value of the odd n.pdf
Create a function mean comp that compares the mean value of the odd n.pdfCreate a function mean comp that compares the mean value of the odd n.pdf
Create a function mean comp that compares the mean value of the odd n.pdffathimaoptical
 
Compare naive, effector, and memory T & B cells (survival time, Ig &.pdf
Compare naive, effector, and memory T & B cells (survival time, Ig &.pdfCompare naive, effector, and memory T & B cells (survival time, Ig &.pdf
Compare naive, effector, and memory T & B cells (survival time, Ig &.pdffathimaoptical
 
Consider the titration of50.0 mL of 0.217 M hydrazoic acid (HN3, Ka=.pdf
Consider the titration of50.0 mL of 0.217 M hydrazoic acid (HN3, Ka=.pdfConsider the titration of50.0 mL of 0.217 M hydrazoic acid (HN3, Ka=.pdf
Consider the titration of50.0 mL of 0.217 M hydrazoic acid (HN3, Ka=.pdffathimaoptical
 
Choose one of the evolutions of CIT and discuss how it may have made.pdf
Choose one of the evolutions of CIT and discuss how it may have made.pdfChoose one of the evolutions of CIT and discuss how it may have made.pdf
Choose one of the evolutions of CIT and discuss how it may have made.pdffathimaoptical
 
Write a GUI application to simulate writing out a check. The value o.pdf
Write a GUI application to simulate writing out a check. The value o.pdfWrite a GUI application to simulate writing out a check. The value o.pdf
Write a GUI application to simulate writing out a check. The value o.pdffathimaoptical
 
Without the effects of cyanobacteria or algae on lichen metabolism, .pdf
Without the effects of cyanobacteria or algae on lichen metabolism, .pdfWithout the effects of cyanobacteria or algae on lichen metabolism, .pdf
Without the effects of cyanobacteria or algae on lichen metabolism, .pdffathimaoptical
 
What is this image trying to tell you about the American West.pdf
What is this image trying to tell you about the American West.pdfWhat is this image trying to tell you about the American West.pdf
What is this image trying to tell you about the American West.pdffathimaoptical
 
What is the difference between a defined-benefit and a defined-contr.pdf
What is the difference between a defined-benefit and a defined-contr.pdfWhat is the difference between a defined-benefit and a defined-contr.pdf
What is the difference between a defined-benefit and a defined-contr.pdffathimaoptical
 
This is question about excel cuers.How would you use Financial Fun.pdf
This is question about excel cuers.How would you use Financial Fun.pdfThis is question about excel cuers.How would you use Financial Fun.pdf
This is question about excel cuers.How would you use Financial Fun.pdffathimaoptical
 
The surface area to volume ration is least improtant in thea. sink.pdf
The surface area to volume ration is least improtant in thea. sink.pdfThe surface area to volume ration is least improtant in thea. sink.pdf
The surface area to volume ration is least improtant in thea. sink.pdffathimaoptical
 
The Martian calendar has sixteen months instead of twelve. What is t.pdf
The Martian calendar has sixteen months instead of twelve. What is t.pdfThe Martian calendar has sixteen months instead of twelve. What is t.pdf
The Martian calendar has sixteen months instead of twelve. What is t.pdffathimaoptical
 
Baby names and birth weights”IntroductionBabies are weighed soon.pdf
Baby names and birth weights”IntroductionBabies are weighed soon.pdfBaby names and birth weights”IntroductionBabies are weighed soon.pdf
Baby names and birth weights”IntroductionBabies are weighed soon.pdffathimaoptical
 
Question How can I called this method in the main, and how can I .pdf
Question How can I called this method in the main, and how can I .pdfQuestion How can I called this method in the main, and how can I .pdf
Question How can I called this method in the main, and how can I .pdffathimaoptical
 
Question 1. List and describe three rules of natural justice, provid.pdf
Question 1. List and describe three rules of natural justice, provid.pdfQuestion 1. List and describe three rules of natural justice, provid.pdf
Question 1. List and describe three rules of natural justice, provid.pdffathimaoptical
 
Postal ClerkAssistant. Why do the five steps of the recruitment pro.pdf
Postal ClerkAssistant. Why do the five steps of the recruitment pro.pdfPostal ClerkAssistant. Why do the five steps of the recruitment pro.pdf
Postal ClerkAssistant. Why do the five steps of the recruitment pro.pdffathimaoptical
 
OverviewWe will be adding some validation to our Contact classes t.pdf
OverviewWe will be adding some validation to our Contact classes t.pdfOverviewWe will be adding some validation to our Contact classes t.pdf
OverviewWe will be adding some validation to our Contact classes t.pdffathimaoptical
 
Negotiation - Porto (Porto case).note this is a case study so the.pdf
Negotiation - Porto (Porto case).note this is a case study so the.pdfNegotiation - Porto (Porto case).note this is a case study so the.pdf
Negotiation - Porto (Porto case).note this is a case study so the.pdffathimaoptical
 
need help with my computer science lab assignemnt. this assignment i.pdf
need help with my computer science lab assignemnt. this assignment i.pdfneed help with my computer science lab assignemnt. this assignment i.pdf
need help with my computer science lab assignemnt. this assignment i.pdffathimaoptical
 
Name a protein that contacts the insulin receptor inside the cell.pdf
Name a protein that contacts the insulin receptor inside the cell.pdfName a protein that contacts the insulin receptor inside the cell.pdf
Name a protein that contacts the insulin receptor inside the cell.pdffathimaoptical
 

More from fathimaoptical (20)

Define a knowledge-based agent, and describe its major components. W.pdf
Define a knowledge-based agent, and describe its major components. W.pdfDefine a knowledge-based agent, and describe its major components. W.pdf
Define a knowledge-based agent, and describe its major components. W.pdf
 
Create a function mean comp that compares the mean value of the odd n.pdf
Create a function mean comp that compares the mean value of the odd n.pdfCreate a function mean comp that compares the mean value of the odd n.pdf
Create a function mean comp that compares the mean value of the odd n.pdf
 
Compare naive, effector, and memory T & B cells (survival time, Ig &.pdf
Compare naive, effector, and memory T & B cells (survival time, Ig &.pdfCompare naive, effector, and memory T & B cells (survival time, Ig &.pdf
Compare naive, effector, and memory T & B cells (survival time, Ig &.pdf
 
Consider the titration of50.0 mL of 0.217 M hydrazoic acid (HN3, Ka=.pdf
Consider the titration of50.0 mL of 0.217 M hydrazoic acid (HN3, Ka=.pdfConsider the titration of50.0 mL of 0.217 M hydrazoic acid (HN3, Ka=.pdf
Consider the titration of50.0 mL of 0.217 M hydrazoic acid (HN3, Ka=.pdf
 
Choose one of the evolutions of CIT and discuss how it may have made.pdf
Choose one of the evolutions of CIT and discuss how it may have made.pdfChoose one of the evolutions of CIT and discuss how it may have made.pdf
Choose one of the evolutions of CIT and discuss how it may have made.pdf
 
Write a GUI application to simulate writing out a check. The value o.pdf
Write a GUI application to simulate writing out a check. The value o.pdfWrite a GUI application to simulate writing out a check. The value o.pdf
Write a GUI application to simulate writing out a check. The value o.pdf
 
Without the effects of cyanobacteria or algae on lichen metabolism, .pdf
Without the effects of cyanobacteria or algae on lichen metabolism, .pdfWithout the effects of cyanobacteria or algae on lichen metabolism, .pdf
Without the effects of cyanobacteria or algae on lichen metabolism, .pdf
 
What is this image trying to tell you about the American West.pdf
What is this image trying to tell you about the American West.pdfWhat is this image trying to tell you about the American West.pdf
What is this image trying to tell you about the American West.pdf
 
What is the difference between a defined-benefit and a defined-contr.pdf
What is the difference between a defined-benefit and a defined-contr.pdfWhat is the difference between a defined-benefit and a defined-contr.pdf
What is the difference between a defined-benefit and a defined-contr.pdf
 
This is question about excel cuers.How would you use Financial Fun.pdf
This is question about excel cuers.How would you use Financial Fun.pdfThis is question about excel cuers.How would you use Financial Fun.pdf
This is question about excel cuers.How would you use Financial Fun.pdf
 
The surface area to volume ration is least improtant in thea. sink.pdf
The surface area to volume ration is least improtant in thea. sink.pdfThe surface area to volume ration is least improtant in thea. sink.pdf
The surface area to volume ration is least improtant in thea. sink.pdf
 
The Martian calendar has sixteen months instead of twelve. What is t.pdf
The Martian calendar has sixteen months instead of twelve. What is t.pdfThe Martian calendar has sixteen months instead of twelve. What is t.pdf
The Martian calendar has sixteen months instead of twelve. What is t.pdf
 
Baby names and birth weights”IntroductionBabies are weighed soon.pdf
Baby names and birth weights”IntroductionBabies are weighed soon.pdfBaby names and birth weights”IntroductionBabies are weighed soon.pdf
Baby names and birth weights”IntroductionBabies are weighed soon.pdf
 
Question How can I called this method in the main, and how can I .pdf
Question How can I called this method in the main, and how can I .pdfQuestion How can I called this method in the main, and how can I .pdf
Question How can I called this method in the main, and how can I .pdf
 
Question 1. List and describe three rules of natural justice, provid.pdf
Question 1. List and describe three rules of natural justice, provid.pdfQuestion 1. List and describe three rules of natural justice, provid.pdf
Question 1. List and describe three rules of natural justice, provid.pdf
 
Postal ClerkAssistant. Why do the five steps of the recruitment pro.pdf
Postal ClerkAssistant. Why do the five steps of the recruitment pro.pdfPostal ClerkAssistant. Why do the five steps of the recruitment pro.pdf
Postal ClerkAssistant. Why do the five steps of the recruitment pro.pdf
 
OverviewWe will be adding some validation to our Contact classes t.pdf
OverviewWe will be adding some validation to our Contact classes t.pdfOverviewWe will be adding some validation to our Contact classes t.pdf
OverviewWe will be adding some validation to our Contact classes t.pdf
 
Negotiation - Porto (Porto case).note this is a case study so the.pdf
Negotiation - Porto (Porto case).note this is a case study so the.pdfNegotiation - Porto (Porto case).note this is a case study so the.pdf
Negotiation - Porto (Porto case).note this is a case study so the.pdf
 
need help with my computer science lab assignemnt. this assignment i.pdf
need help with my computer science lab assignemnt. this assignment i.pdfneed help with my computer science lab assignemnt. this assignment i.pdf
need help with my computer science lab assignemnt. this assignment i.pdf
 
Name a protein that contacts the insulin receptor inside the cell.pdf
Name a protein that contacts the insulin receptor inside the cell.pdfName a protein that contacts the insulin receptor inside the cell.pdf
Name a protein that contacts the insulin receptor inside the cell.pdf
 

Recently uploaded

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 

Recently uploaded (20)

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 

Modify this code to do an Insert function for an AVL tree, instead o.pdf

  • 1. Modify this code to do an Insert function for an AVL tree, instead of a B-Tree . Please compile in Microsoft Visual studio, as it is the IDE that is required for my course. Here is the code for the header file and the Source file. Soure File : #include #include "bTree.h" using namespace std; int main() { bTree treeRoot; int num; cout << "Enter numbers ending with -999" << endl; cin >> num; while (num != -999) { treeRoot.insert(num); cin >> num; } cout << "Inorder traversal data: "; treeRoot.inOrder(); cout << endl; return 0; Header File: #ifndef H_bTree #define H_bTree #include #include using namespace std; //************************************************************* // Author: D.S. Malik // // class bTree // This class specifies the basic operations to implement a // B-tree.
  • 2. //************************************************************* template struct bTreeNode { int recCount; recType list[bTreeOrder - 1]; bTreeNode *children[bTreeOrder]; }; template class bTree { public: bool search(const recType& searchItem); //Function to determine if searchItem is in the B-tree. //Postcondition: Returns true if searchItem is found in the // B-tree; otherwise, returns false. void insert(const recType& insertItem); //Function to insert insertItem in the B-tree. //Postcondition: If insertItem is not in the the B-tree, it // is inserted in the B-tree. void inOrder(); //Function to do an inorder traversal of the B-tree. bTree(); //constructor //Add additional members as needed protected: bTreeNode *root; private: void searchNode(bTreeNode *current, const recType& item, bool& found, int& location); void insertBTree(bTreeNode *current, const recType& insertItem, recType& median, bTreeNode* &rightChild, bool& isTaller);
  • 3. void insertNode(bTreeNode *current, const recType& insertItem, bTreeNode* &rightChild, int insertPosition); void splitNode (bTreeNode *current, const recType& insertItem, bTreeNode* rightChild, int insertPosition, bTreeNode* &rightNode, recType &median); void recInorder(bTreeNode *current); }; template bTree::bTree() { root = NULL; } //end constructor template bool bTree::search(const recType& searchItem) { bool found = false; int location; bTreeNode *current; current = root; while (current != NULL && !found) { searchNode(current, item, found, location); if (!found) current = current->children[location]; } return found; } //end search template void bTree::searchNode (bTreeNode *current, const recType& item,
  • 4. bool& found, int& location) { location = 0; while (location < current->recCount && item > current->list[location]) location++; if (location < current->recCount && item == current->list[location]) found = true; else found = false; } //end searchNode template void bTree::insert(const recType& insertItem) { bool isTaller = false; recType median; bTreeNode *rightChild; insertBTree(root, insertItem, median, rightChild, isTaller); if (isTaller) //the tree is initially empty or the root //was split by the function insertBTree { bTreeNode *tempRoot; tempRoot = new bTreeNode; tempRoot->recCount = 1; tempRoot->list[0] = median; tempRoot->children[0] = root; tempRoot->children[1] = rightChild; root = tempRoot; } } //insert template void bTree::insertBTree (bTreeNode *current, const recType& insertItem,
  • 5. recType& median, bTreeNode* &rightChild, bool& isTaller) { int position; isTaller = false; if (current == NULL) { median = insertItem; rightChild = NULL; isTaller = true; } else { bool found; searchNode(current, insertItem, found, position); if (found) cout << "Cannot insert duplicate record." << endl; else { recType newMedian; bTreeNode *newChild; insertBTree(current->children[position], insertItem, newMedian, newChild, isTaller); if (isTaller) { if (current->recCount < bTreeOrder - 1) { isTaller = false; insertNode(current, newMedian, newChild, position); } else splitNode(current, newMedian, newChild, position, rightChild, median); }
  • 6. } } } //insertBTree template void bTree::insertNode (bTreeNode *current, const recType& insertItem, bTreeNode* &rightChild, int insertPosition) { int index; for (index = current->recCount; index > insertPosition; index--) { current->list[index] = current->list[index - 1]; current->children[index + 1] = current->children[index]; } current->list[index] = insertItem; current->children[index + 1] = rightChild; current->recCount++; } //end insertNode template void bTree::splitNode (bTreeNode *current, const recType& insertItem, bTreeNode* rightChild, int insertPosition, bTreeNode* &rightNode, recType &median) { rightNode = new bTreeNode; int mid = (bTreeOrder - 1) / 2; if (insertPosition <= mid) //new item goes in the first //half of the node { int index = 0;
  • 7. int i = mid; while (i < bTreeOrder - 1) { rightNode->list[index] = current->list[i]; rightNode->children[index + 1] = current->children[i + 1]; index++; i++; } current->recCount = mid; insertNode(current, insertItem, rightChild, insertPosition); (current->recCount)--; median = current->list[current->recCount]; rightNode->recCount = index; rightNode->children[0] = current->children[current->recCount + 1]; } else //new item goes in the second half of the node { int i = mid + 1; int index = 0; while (i < bTreeOrder - 1) { rightNode->list[index] = current->list[i]; rightNode->children[index + 1] = current->children[i + 1]; index++; i++; } current->recCount = mid; rightNode->recCount = index; median = current->list[mid]; insertNode(rightNode, insertItem, rightChild, insertPosition - mid - 1); rightNode->children[0] =
  • 8. current->children[current->recCount + 1]; } } //splitNode template void bTree::inOrder() { recInorder(root); } // end inOrder template void bTree::recInorder (bTreeNode *current) { if (current != NULL) { recInorder(current->children[0]); for (int i = 0; i < current->recCount; i++) { cout << current->list[i] << " "; recInorder(current->children[i + 1]); } } } //end recInorder #endif Solution In a binary tree the balance issue of a node N is described to be the height distinction BalanceFactor(N) := –peak(LeftSubtree(N)) + height(RightSubtree(N)) [6] of its infant subtrees. A binary tree is defined to be an AVL tree if the invariant BalanceFactor(N) –1,zero,+1 holds for each node N in the tree. A node N with BalanceFactor(N) < 0 is called "left-heavy", one with BalanceFactor(N) > 0 is known as "proper-heavy", and one with BalanceFactor(N) = 0 is once in a while clearly called "balanced". statement inside the sequel, because there's a one-to-one correspondence among nodes and the subtrees
  • 9. rooted via them, we every so often depart it to the context whether the call of an object stands for the node or the subtree. properties[edit] balance factors can be saved up to date through knowing the preceding balance elements and the exchange in top – it is not important to recognize absolutely the height. for containing the AVL balance statistics, two bits in keeping with node are enough.[7] the height h of an AVL tree with n nodes lies within the c programming language:[8] log2(n+1) h < c log2(n+2)+b with the golden ratio := (1+5) 2 1.618, c := 1 log2 1.44, and b := c2 log2 5 – 2 –zero.328. that is due to the fact an AVL tree of top h includes at the least Fh+2 – 1 nodes where Fh is the Fibonacci collection with the seed values F1 = 1, F2 = 1.