Suppose we have a singly linked list of integers and we are given the following requirements:
For a given node, menu option 1 should display the data field of the node and any subsequent nodes in the console output.
For a given node, menu option 2 should display the data field of the node and any subsequent nodes in the console output only if data is multiple of 2.
For a given node, menu option 3 should display the data field of the node and any subsequent nodes in the console output only if data is multiple of 3.
A possible solution is as follows:
struct node {
int data;
node *next;
};
void insertBeginning(node **list, int data)
{
node *n = new node;
n->data = data;
n->next = *list;
*list = n;
}
void option1(node *node)
{
if (node != NULL) {
cout << node->data << endl;
option1(node->next);
}
}
void option2(node *node)
{
if (node != NULL) {
if (node->data % 2 == 0)
cout << node->data << endl;
option2(node->next);
}
}
void option3(node *node)
{
if (node != NULL) {
if (node->data % 3 == 0)
cout << node->data << endl;
option3(node->next);
}
}
void main()
{
node *root = new node;
root->data = 5;
root->next = NULL;
insertBeginning(&root, 4);
insertBeginning(&root, 3);
insertBeginning(&root, 2);
insertBeginning(&root, 1);
cout << "OPTION 1" << endl;
option1(root);
cout << endl;
cout << "OPTION 2" << endl;
option2(root);
cout << endl;
cout << "OPTION 3" << endl;
option3(root);
}
But a closer inspection reveals that in each menu option there is an algorithm that loop through nodes, which is the same in all options, and then a specific action. So it might be good to separate the looping algorithm from the action. How would you do it?
In this case the looping algorithm as well as the actions are very simple, so doing a separation would be killer, but in my situation the looping algorithm and the actions are more complex, and I want to avoid copying and pasting the looping algorithm. Also, this case uses a singly linked list, but it could be a tree. Anyway I do not want to distract you with these details.
Finally consider a fourth option:
For a given node, menu option 4 should display the sum of the data field of the node and any subsequent nodes in the console output.
A possible solution is as follows:
void loopNodes(node * n, void action(node * n))
{
if (n != NULL) {
action(n);
loopNodes(n->next, action);
}
}
void option1(node * node)
{
cout << node->data << endl;
}
void option2(node * node)
{
if (node->data % 2 == 0)
cout << node->data << endl;
}
void option3(node * node)
{
if (node->data % 3 == 0)
cout << node->data << endl;
}
int sum;
void option4(node * node)
{
sum += node->data;
}
void main()
{
node *root = new node;
root->data = 5;
root->next = NULL;
insertBeginning(&root, 4);
insertBeginning(&root, 3);
insertBeginning(&root, 2);
insertBeginning(&root, 1);
cout << "OPTION 1" << endl;
loopNodes(root, option1);
cout << endl;
cout << "OPTION 2" << endl;
loopNodes(root, option2);
cout << endl;
cout << "OPTION 3" << endl;
loopNodes(root, option3);
cout << endl;
cout << "OPTION 4" << endl;
sum = 0;
loopNodes(root, option4);
cout << "SUM = " << sum << endl;
}
Option 4 introduced a challenge; the need to maintain state. I used a variable with file scope, but an alternative could be (and this is my final solution):
void loopNodes(node * n, void action(node * n, void *state), void *state)
{
if (n != NULL) {
action(n, state);
loopNodes(n->next, action, state);
}
}
void option1(node * node, void *state)
{
cout << node->data << endl;
}
void option2(node * node, void *state)
{
if (node->data % 2 == 0)
cout << node->data << endl;
}
void option3(node * node, void *state)
{
if (node->data % 3 == 0)
cout << node->data << endl;
}
void option4(node * node, void *state)
{
*(int *) state += node->data;
}
void main()
{
node *root = new node;
root->data = 5;
root->next = NULL;
insertBeginning(&root, 4);
insertBeginning(&root, 3);
insertBeginning(&root, 2);
insertBeginning(&root, 1);
cout << "OPTION 1" << endl;
loopNodes(root, option1, NULL);
cout << endl;
cout << "OPTION 2" << endl;
loopNodes(root, option2, NULL);
cout << endl;
cout << "OPTION 3" << endl;
loopNodes(root, option3, NULL);
cout << endl;
cout << "OPTION 4" << endl;
int *sum = new int;
*sum = 0;
loopNodes(root, option4, sum);
cout << "SUM = " << *sum << endl;
}
What do you think?
Any feedback is really appreciated!
Note: I have to use core language facilities (I cannot use the standard library, boost, etc.).
In terms of OOP, your option mechanism depends on some specific handling bound to each option input, which are in this case positive integers. You may consider having a Handle class which takes two inputs during construction, one is option number (i.e an integer) and second is a lambda function or a function pointer which applies the logic. Your linked list then can have a AssignHandles() method to construct the menu behavior automatically.
You could use a templated version for your loop function. This could be used with functions or function objects, which implement the operator ():
#include <iostream>
int list[] = {1, 2, 3, 4, 5};
template<class Option>
void loop(Option & option) {
for (size_t i = 0; i < sizeof(list)/sizeof(list[0]); ++i) {
option(list[i]);
}
}
void print(int e) {
std::cout << e << std::endl;
}
struct Sum {
int sum = 0;
void operator () (int e) {
sum += e;
}
};
int main(int argc, char** argv) {
loop(print);
Sum s;
loop(s);
std::cout << s.sum << std::endl;
return 0;
}
Related
// in this code I first created nodes stored them in a que and keep on removing them as I entered their left and right children. To make a node have no further children I entered -1 while entering data. Here I am not able to understand what is wrong with my code , I am getting wrong output for preorder and postorder traversals. It would be really great if you guys could help me out.
I made a class que for queue ds and inherited it in tree class in protected mode.
#include <iostream>
#include <math.h>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
};
class que
{
protected:
int start;
int end;
struct node **arr;
int n;
public:
que(int x)
{
n = x;
arr = new struct node *[n];
start = -1;
end = -1;
}
void isfull()
{
if (end == n)
cout << "Queue is full !!!" << endl;
return;
}
int isempty()
{
if (start == end)
{
start = -1;
end = -1;
cout << "Queue is empty !!!" << endl;
return 1;
}
return 0;
}
void enqu(struct node *x)
{
if (end == n)
{
cout << "called" << endl;
isfull();
return;
}
end++;
arr[end] = x;
}
struct node *dequ(void)
{
struct node *q = 0;
if (start == end)
{
isempty();
return q;
}
start++;
cout << "Element removed is ->" << arr[start] << endl;
return arr[start];
}
};
class tree : protected que
{
public:
struct node *head;
struct node *ptr;
tree(int n) : que(n)
{
head = 0;
ptr = 0;
enter();
}
void create(void)
{
ptr = new struct node;
ptr->left = 0;
ptr->right = 0;
}
void enter(void)
{
struct node *p;
if (head == 0)
{
create();
cout << "Enter root element of tree -> ";
cin >> ptr->data;
head = ptr;
cout << "Enquing ptr - " << ptr << endl;
enqu(ptr);
}
while (!isempty())
{
p = dequ();
cout << "Enter left child ->";
int x;
cin >> x;
if (x != -1)
{
create();
p->left = ptr;
ptr->data = x;
cout << "Enquing ptr - " << ptr << endl;
enqu(ptr);
}
cout << "Enter right child ->";
cin >> x;
if (x != -1)
{
create();
p->right = ptr;
ptr->data = x;
cout << "Enquing ptr - " << ptr << endl;
enqu(ptr);
}
}
}
void inorder(struct node *yes)
{
if (yes != 0)
{
inorder(yes->left);
cout << "--> " << yes->data << endl;
inorder(yes->right);
}
}
void preorder(struct node *yes)
{
if (yes != 0)
{
cout << "--> " << yes->data << endl;
inorder(yes->left);
inorder(yes->right);
}
}
void postorder(struct node *yes)
{
if (yes != 0)
{
inorder(yes->left);
inorder(yes->right);
cout << "--> " << yes->data << endl;
}
}
int count(struct node *yes)
{
static int x = 0, y = 0;
if (yes == 0)
return 0;
x = count(yes->left);
y = count(yes->right);
return x + y + 1;
}
int height(struct node *yes)
{
static int a = 0, b = 0;
if (yes == 0)
return 0;
a = count(yes->left);
b = count(yes->right);
if (a > b)
return a + 1;
else
return b + 1;
}
};
int main()
{
int x;
cout << "Enter height of tree - ";
cin >> x;
int max = 0;
max = pow(2, x + 1) - 1;
tree tr(max);
cout << "Preorder traversal -- " << endl;
tr.preorder(tr.head);
cout << "Inorder traversal -- " << endl;
tr.inorder(tr.head);
cout << "Postorder traversal -- " << endl;
tr.postorder(tr.head);
cout << "\n No. of elements -- " << tr.count(tr.head);
cout << "\n Height of tree --" << tr.height(tr.head);
}
The preorder and postorder functions doesn't call themselves recursively. Instead they call the inorder function, which will lead to all but the root will be printed using inorder.
I am trying to construct an euler path for a rooted tree where root can have multiple children (i.e. its not a binary tree). To keep track of each already traversed child I am maintaining a queue in a map. When I encounter a root I simply pop one of its children and set it as root. The problem is whenever, I pop a child and its parent is traversed again the popped node appears again. I am guessing I need to store references of queue but can't get my head around it. Any help would be appreciated.
vector<int> getEulerPath(Node* root) {
Node* node = root;
vector<int> path;
unordered_map<int, queue<Node*>> uvc;
while(node != NULL) {
path.push_back(node->data);
cout << "processing node " << node->data << endl;
if(uvc.find(node->data) != uvc.end()) {
cout << "found queue: " << node->data << endl;
uv = uvc.find(node->data)->second;
} else {
for(int i = 0; i < node->children.size(); i++) {
uv.push(node->children[i]);
}
cout << "adding queue: " << node->data << endl;
uvc.insert(pair<int, queue<Node*>>{node->data, uv});
}
if(!uv.empty()) {
cout << "queue size before popping: " << uv.size() << endl;
cout << "have some children left for : " << node->data << endl;
node = uv.front();
uv.pop();
cout << "queue size after popping: " << uv.size() << endl;
} else {
cout << "no children left reverting back to : " << node->parent->data << endl;
node = node->parent;
}
cout << "new node is: " << node->data << endl;
}
return path;
}
Update
Following is the minimal runnable example
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
class Node {
public:
vector<Node*> children;
Node* parent;
int data;
Node();
Node(int, Node*);
};
Node::Node() {
parent = NULL;
data = -1;
}
Node::Node(int d, Node* p) {
parent = p;
data = d;
}
Node* generateTree(vector<vector<int>> edges) {
unordered_map<int, Node*> nodes;
Node* root = new Node();
for(int i = 0; i < edges.size(); i++) {
vector<int> edge = edges[i];
Node* parent;
Node* child;
if(nodes.find(edge[0]) == nodes.end()) {
parent = new Node(edge[0], NULL);
nodes.insert(pair<int, Node*>{edge[0], parent});
} else {
parent = nodes.find(edge[0])->second;
}
if(nodes.find(edge[1]) == nodes.end()) {
child = new Node(edge[1], parent);
nodes.insert(pair<int, Node*>{edge[0], child});
} else {
child = nodes.find(edge[1])->second;
}
if(i == 0) root = parent;
parent->children.push_back(child);
}
return root;
}
vector<int> getEulerPath(Node* root) {
Node* node = root;
vector<int> path;
unordered_map<int, queue<Node*>> uvc;
while(node != NULL) {
path.push_back(node->data);
cout << "processing node " << node->data << endl;
queue<Node*> uv;
if(uvc.find(node->data) != uvc.end()) {
cout << "found queue: " << node->data << endl;
uv = uvc.find(node->data)->second;
} else {
for(int i = 0; i < node->children.size(); i++) {
uv.push(node->children[i]);
}
cout << "adding queue: " << node->data << endl;
uvc.insert(pair<int, queue<Node*>>{node->data, uv});
}
if(!uv.empty()) {
cout << "queue size before popping: " << uv.size() << endl;
cout << "have some children left for : " << node->data << endl;
node = uv.front();
uv.pop();
cout << "queue size after popping: " << uv.size() << endl;
} else {
cout << "no children left reverting back to : " << node->parent->data << endl;
node = node->parent;
}
cout << "new node is: " << node->data << endl;
}
return path;
}
int main() {
vector<vector<int>> edges;
edges.push_back(vector<int> {1, 2});
edges.push_back(vector<int> {1, 3});
edges.push_back(vector<int> {1, 4});
edges.push_back(vector<int> {3, 5});
edges.push_back(vector<int> {3, 6});
edges.push_back(vector<int> {3, 7});
Node* root = generateTree(edges);
vector<int> euler_path = getEulerPath(root);
for(int i = 0; i < euler_path.size(); i++) {
cout << euler_path[i] << " ";
}
cout << endl;
return 0;
}
So i am writing a programm where user fills two linked lists with integers, and now i need to create a function that creates third linked list with values from both first and second lists without duplicates.
Here is the code i have right now:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node
{
int info;
node *next;
node (int i, node *n=NULL)
{
info = i;
next = n;
}
~node()
{
cout <<"NODE CONTAINING \"" << info << "\" WAS DELETED!" << endl;
}
};
struct list
{
node* startList1, *lastList1, *startList2, *lastList2;
int menuOption;
int nodeCount1=0, nodeCount2=0;
list() {
startList1 = NULL;
startList2 = NULL;
}
void addList1(node *p)
{
int n;
cout << "PLEASE INPUT VALUE WHICH YOU WANT IN THE NODE:";
cin >> n;
p = new node(n);
nodeCount1++;
if(startList1==NULL)
{
startList1 = lastList1 = p;
}
else
{
lastList1->next = p;
lastList1 = p;
}
}
void printList1(node *pr)
{
node *pr;
for (pr=startList1; pr!=NULL; pr=pr->next)
{
cout << pr->info << endl;
}
}
void addList2(node *q)
{
int n;
cout << "PLEASE INPUT VALUE WHICH YOU WANT IN THE NODE:";
cin >> n;
q = new node(n);
nodeCount2++;
if(startList2==NULL)
{
startList2 = lastList2 = q;
}
else
{
lastList2->next = q;
lastList2 = q;
}
}
void printList2(node *pr)
{
for (pr=startList2; pr!=NULL; pr=pr->next)
{
cout << pr->info << endl;
}
}
// this just prints first and second lists to show what is inside..
void printBoth(node *pr, node *qr)
{
cout << "Elements of the first list:" << endl;
for (pr=startList1; pr!=NULL; pr=pr->next)
{
cout << pr->info << endl;
}
cout << "Elements of the second list:" << endl;
for (pr=startList2; pr!=NULL; pr=pr->next)
{
cout << pr->info << endl;
}
}
void printMenu()
{
cout << "MENU" << endl;
cout << "(1) ADD ELEMENT LIST1." << endl;
cout << "(2) PRINT LIST1" << endl;
cout << "(3) ADD ELEMENT LIST2" << endl;
cout << "(4) PRINT LIST2" << endl;
cout << "(5) PRINT BOTH LISTS" << endl;
cout << "(6) USE MERGE FUNCTION" << endl;
cout << "(7) TO EXIT" << endl;
cin >> menuOption;
system ("cls");
};
void dragons()
{
node *temp1 = startList1;
node *temp2 = startList2;
while(temp1)
{
temp1 = startList1->next;
delete startList1;
startList1=temp1;
}
while(temp2)
{
temp2 = startList2->next;
delete startList2;
startList2=temp2;
}
};
};
int main()
{
struct node *p = NULL, *q = NULL;
list s;
s.printMenu();
node* list1;
node* list2;
node* sorting;
while(s.menuOption!=7)
{
switch (s.menuOption)
{
case 1: s.addList1(list1);
break;
case 2: s.printList1(list1);
break;
case 3: s.addList2(list2);
break;
case 4: s.printList2(list2);
break;
case 5:s.printBoth(list1, list2);
break;
case 6:s.merge();
break;
default: cout << "SOMETHING WENT WRONG!!!!" << endl;
break;
}
system ("pause");
system ("cls");
s.printMenu();
}
s.dragons();
return 0;
}
So now user can input elements to both lists and see them, but how can i create a function, that will merge those lists without duplicate values? for example:
List1 = 1, 2, 3, 6, 8;
List2 = 2, 4, 5, 7, 8;
Merged list = 1, 2, 3, 4, 5, 7, 8; (can be whatever order actually, it doesn't matter)
Any advice would be appreciated! Thanks!
Here is a version of your code, but using an STL list container to provide the linked list, and this provides its own functions that will achieve what you want. This code is for demonstration, and is not optimised for efficiency, it just shows one possible method of merging unique elements from two lists. (For this to work, the list requires its elements to be sorted, and you need to provide predicates for "less than" and "equal to" for the nodes in the list.)
(Its true that it takes time to learn some of the STL containers and functions, but many people would recommend doing that instead of trying to create your own raw-pointer-based linked list from scratch.)
Hope this helps, or is of interest.
#include <iostream>
#include <stdlib.h>
#include <list>
struct node
{
int info;
node (int i)
{
info = i;
}
~node()
{
//std::cout <<"NODE CONTAINING \"" << info << "\" WAS DELETED!" << std::endl;
}
};
bool nodeLess(node n1, node n2)
{
return n1.info < n2.info;
}
bool nodeEqual(node n1, node n2)
{
return n1.info == n2.info;
}
struct mylist
{
int menuOption;
std::list<node> list1;
std::list<node> list2;
void addList(std::list<node>& l)
{
int x;
std::cout << "PLEASE INPUT VALUE WHICH YOU WANT IN THE NODE: ";
std::cin >> x;
node n(x);
l.push_back(n);
}
void printList(const std::list<node>& l)
{
for (std::list<node>::const_iterator it = l.cbegin(); it != l.cend(); ++it)
{
std::cout << it->info << std::endl;
}
}
void addList1() { addList(list1); }
void addList2() { addList(list2); }
void printList1() { printList(list1); }
void printList2() { printList(list2); }
// this just prints first and second lists to show what is inside..
void printBoth()
{
std::cout << "Elements of the first list:" << std::endl;
printList1();
std::cout << "Elements of the second list:" << std::endl;
printList2();
}
void simpleMerge()
{
std::list<node> merged;
merged.insert(merged.end(), list1.begin(), list1.end());
merged.insert(merged.end(), list2.begin(), list2.end());
std::cout << "CONTENTS OF LIST1 THEN LIST2: " << std::endl;
printList(merged);
}
void uniqueSortMerge()
{
std::list<node> sorted1(list1.begin(), list1.end());
std::list<node> sorted2(list2.begin(), list2.end());
sorted1.sort(nodeLess);
sorted2.sort(nodeLess);
sorted1.unique(nodeEqual);
sorted2.unique(nodeEqual);
std::list<node> merged;
std::merge(sorted1.begin(), sorted1.end(),
sorted2.begin(), sorted2.end(),
std::back_inserter(merged),
nodeLess);
std::cout << "UNIQUE CONTENTS OF LIST1 AND LIST2 SORTED AND MERGED: " << std::endl;
printList(merged);
}
void printMenu()
{
std::cout << "MENU" << std::endl;
std::cout << "(1) ADD ELEMENT LIST1." << std::endl;
std::cout << "(2) PRINT LIST1" << std::endl;
std::cout << "(3) ADD ELEMENT LIST2" << std::endl;
std::cout << "(4) PRINT LIST2" << std::endl;
std::cout << "(5) PRINT BOTH LISTS" << std::endl;
std::cout << "(6) USE SIMPLE MERGE FUNCTION" << std::endl;
std::cout << "(7) USE UNIQUE, SORT AND MERGE FUNCTION" << std::endl;
std::cout << "(8) TO EXIT" << std::endl;
std::cin >> menuOption;
system ("cls");
};
void dragons()
{
list1.clear();
list2.clear();
};
};
int main()
{
mylist s;
do
{
s.printMenu();
switch (s.menuOption)
{
case 1:
s.addList1();
break;
case 2:
s.printList1();
break;
case 3:
s.addList2();
break;
case 4:
s.printList2();
break;
case 5:
s.printBoth();
break;
case 6:
s.simpleMerge();
break;
case 7:
s.uniqueSortMerge();
break;
case 8:
break;
default:
std::cout << "SOMETHING WENT WRONG!!!!" << std::endl;
break;
}
system ("pause");
system ("cls");
} while(s.menuOption != 8);
s.dragons();
return 0;
}
I am really trying to figure out the bug in my code I wrote for AVL tree but there seems to be a bug in it.
It calls the rotation functions but when it finishes rotation there are several problem:
root doesn't change
only root value is displayed when in-order traversal is done, other values seem to disappear
I have been trying to fix this bug since 2-3 days but cant seem to solve it.
Would love some help from you guys. I will be attaching the code with this.
/*
insertion
rotations all DONE
level order DONE
deletion
*/
#include <iostream>
#include <queue>
using namespace std;
struct node
{
int data;
int height;
node *left;
node *right;
node()
{
height = 0;
left = right = NULL;
}
};
class AVL
{
public:
node *root;
AVL()
{
root = NULL;
}
// search function
bool search(int num)
{
node *nodePtr = root;
if (root->data == num)
{
cout << "FOUND " << endl;
return true;
}
else
{
while (nodePtr != NULL)
{
if (nodePtr->data < num)
{
nodePtr = nodePtr->right;
}
else if (nodePtr->data > num)
{
nodePtr = nodePtr->left;
}
else
{
cout << "FOUND " << endl;
return true;
}
}
cout << "NOT FOUND " << endl;
return false;
}
}
// inorder
void inorder()
{
inorder(root);
}
// postorder
void postorder()
{
postorder(root);
}
// preorder
void preorder()
{
preorder(root);
}
// inorder utility function
void inorder(node *&nodePtr)
{
if (nodePtr)
{
inorder(nodePtr->left);
cout << nodePtr->data << ", ";
inorder(nodePtr->right);
}
}
// preorder utility function
void preorder(node *&nodePtr)
{
if (nodePtr)
{
cout << nodePtr->data << ", ";
preorder(nodePtr->left);
preorder(nodePtr->right);
}
}
// postorder utility function
void postorder(node *&nodePtr)
{
if (nodePtr)
{
postorder(nodePtr->left);
postorder(nodePtr->right);
cout << nodePtr->data << ", ";
}
}
// returns the number of nodes in the tree
int size(node *&nodePtr)
{
if (!nodePtr)
{
return 0;
}
else
{
return (size(nodePtr->left) + size(nodePtr->right)) + 1;
}
}
// function to check if tree is empty or not
bool isempty()
{
if (root == NULL)
{
return true;
}
else
{
return false;
}
}
// max function to calculate height and also the balance factor
int maximum(int x, int y)
{
if (x>y)
{
return x;
}
else
{
return y;
}
}
// returns the level of the tree
int returnHeight(node *&nodePtr)
{
if (nodePtr == NULL)
{
return 0;
}
else
{
return nodePtr->height;
}
}
// assigning the height to the node
void assignHeightToNode(node *&nodePtr)
{
int left = returnHeight(nodePtr->left);
int right = returnHeight(nodePtr->right);
nodePtr->height = maximum(left, right) + 1;
}
// single left rotate
node *singleLeftRotate(node *&nodePtr)
{
// if (nodePtr==NULL)
// {
// return;
// }
node * b = nodePtr->right;
nodePtr->right = b->left;
b->left = nodePtr;
return b;
}
// single right rotate
node *singleRightRotate(node *&nodePtr)
{
// if (nodePtr==NULL)
// {
// return ;
// }
node * b = nodePtr->left;
nodePtr->left = b->right;
b->right = nodePtr;
assignHeightToNode(nodePtr);
assignHeightToNode(b);
return b;
}
// double left rotate
node *doubleLeft(node *&nodePtr)
{
nodePtr->right = singleRightRotate(nodePtr->right);
return singleLeftRotate(nodePtr);
}
// double right rotate
node *doubleRight(node *&nodePtr)
{
nodePtr->left = singleLeftRotate(nodePtr->left);
return singleRightRotate(nodePtr);
}
// insert function
void insert1(int x)
{
cout << "NOW INSERTING " << x << endl;
insert2(x, root);
}
// insert utility function
void insert2(int &x, node *&nodePtr)
{
if (nodePtr == NULL)
{
node *newNode = new node;
newNode->data = x;
newNode->height = 0;
nodePtr = newNode;
checkRotations(nodePtr, x);
return;
}
else
{
if (x < nodePtr->data)
{
cout << endl << "GOING LEFT " << endl;
insert2(x, nodePtr->left);
}
else if (x > nodePtr->data)
{
cout << endl << "GOING RIGHT " << endl;
insert2(x, nodePtr->right);
}
else if (nodePtr->data == x)
{
cout << "DUPLICATE VALUES NOT ALLOWED " << endl;
return;
}
}
checkRotations(nodePtr, x);
}
// checking if rotations needed
void checkRotations(node *&nodePtr, int &x)
{
assignHeightToNode(nodePtr);
cout << endl << endl << "HEIGHT OF " << nodePtr->data << " IS " << nodePtr->height << endl;
int bf = returnHeight(nodePtr->left) - returnHeight(nodePtr->right);
cout << endl << endl << "BALANCE FACTOR AT NODE " << nodePtr->data << " IS " << bf << endl;
if (bf < -1 || bf > 1)
{
cout << endl << endl << "ROTATION IS HAPPEENING " << endl << endl;
if (x > nodePtr->data)
{
singleLeftRotate(nodePtr);
cout << endl << "ROTATION DONE SINGLE LEFT " << endl;
return;
}
else if (x < nodePtr->data)
{
singleRightRotate(nodePtr);
cout << endl << "ROTATION DONE SINGLE RIGHT " << endl;
return;
}
else if (x < root->data && x > root->left->data)
{
doubleRight(nodePtr);
cout << endl << "ROTATION DONE DOUBLE LEFT " << endl;
return;
}
else if (x > root->data && x < root->right->data)
{
doubleLeft(nodePtr);
cout << endl << "ROTATION DONE DOUBLE LEFT " << endl;
return;
}
}
}
// level order display code
void levelOrderDisplay()
{
levelOrderDisplay2(root);
}
// level order display utility function
void levelOrderDisplay2(node *&nodePtr)
{
if (nodePtr == NULL)
{
cout << "THE TREE IS EMPTY" << endl;
return;
}
queue <node *> displayer;
displayer.push(nodePtr);
while (!displayer.empty())
{
node *currentNode = displayer.front();
cout << currentNode->data << ", ";
if (currentNode->left != NULL)
{
displayer.push(currentNode->left);
}
else if (currentNode->right != NULL)
{
displayer.push(currentNode->right);
}
displayer.pop();
}
}
void rootD()
{
cout << "root is " << root->data << endl;
}
};
int main()
{
AVL tree;
tree.insert1(3);
tree.insert1(2);
tree.insert1(1);
tree.insert1(4);
tree.insert1(5);
tree.insert1(6);
tree.insert1(7);
tree.inorder();
// tree.rootD();
// tree.levelOrderDisplay();
// tree.rootD();
return 0;
}
I suggest you that do not pass node pointer by reference where your code does not change pointer value such as print. Try this... I completed my avl tree code just 2 3 day before and i have similar problem, when i debug my program i came to know that somehow in print function my pointer value is changed because i passed it by reference..... So.. Try this and see if work or not.... Hope this might help you.
Really late answer, but this is what you had to do. You should set the node passed in the function equal to the temporary node made in the function. In coding language, nodePtr=b should have been the last line of the single rotate functions.
Hope this helps :)
Working on AVL implementation. When I try to perform a rotation it deletes the children of the node being rotated. So far I've only started on right. I didn't implement a parent pointer in my nodes as I used my BST as a base.
Any guesses as to why this is fracking up? If any additional info is needed I can post it. I didn't want to upload my entire class for wall-of-code reasons.
Insert
template <typename T>
void AVL<T>::insert(T data)
{
insert(mRootNode, data);
}
template <typename T>
void AVL<T>::insert(Node<T>*& node, const T &data)
{
// If the tree is empty, make a new node and make it
// the root of the tree.
if (node == NULL)
{
node = new Node<T>(data);
return;
}
// If num is already in tree: return.
if (node->mData == data)
return;
// The tree is not empty: insert the new node into the
// left or right subtree.
if (data < node->mData)
{
insert(node->mLeft, data);
}
else
{
insert(node->mRight, data);
}
// Check for balance errors
int balance = getBalance(node);
bool
rotL = (balance > 1) && (getBalance(node->mRight) == 1),
rotR = (balance < -1) && (getBalance(node->mLeft) == -1),
rotRL = (balance > 1) && (getBalance(node->mRight) == -1),
rotLR = (balance < -1) && (getBalance(node->mLeft) == 1);
if (rotR)
{
rotateRight(node);
}
}
Right Rotation
template <class T>
void AVL<T>::rotateRight(Node<T>*& node)
{
showTree(); // Show tree before rotation
Node<T>* other = node->mLeft;
Node<T>* tmp = other->mRight;
other->mRight = node;
node->mLeft = tmp;
showTree(); // Show tree after rotation
}
Balance
template <class T>
int AVL<T>::getBalance(Node<T>* node)
{
return (node->mRight ? treeHeight(node->mRight) : 0) - (node->mLeft ? treeHeight(node->mLeft) : 0);
}
Stub
void StubAVL()
{
AVL<int> tree;
cout << "Inserting: 5\n";
tree.insert(5);
cout << "Inserting: 8\n";
tree.insert(8);
cout << "Inserting: 3\n";
tree.insert(3);
cout << "Inserting: 12\n";
tree.insert(12);
cout << "Inserting: 9\n";
tree.insert(9);
cout << "Inserting: 2\n";
tree.insert(2);
cout << "Inserting: 13\n";
tree.insert(13);
cout << "Inserting: 0\n";
tree.insert(0);
cout << "Inserting: -1\n";
tree.insert(-1);
cout << "Inorder: ";
tree.showInOrder();
cout << endl;
cout << "Preorder: ";
tree.showPreOrder();
cout << endl;
cout << "Postorder: ";
tree.showPostOrder();
cout << endl;
cout << "Node Count = " << tree.nodesCount() << endl;
cout << "Leaves Count = " << tree.leavesCount() << endl;
cout << "Height = " << tree.getHeight() << endl;
cout << "The tree looks like:\n";
tree.showTree();
cout
<< "\n\n"
<< "Heights\n";
tree.showHeight();
cout
<< "\n\n"
<< "Balances\n";
tree.showBalance();
cout << "\n\n";
system("pause");
}
Output
Right after it inserts 0 ↑ the children of 3 are deleted.