Deleting a BinaryTreeNode from a BinaryTree - c++

I have a BinarySearchTree that consists of nodes which are both a template class of dataType student, where student is a class with private variables of name and grade.
At the moment I can print the tree, find names and or grades in the tree but I am having trouble deleting nodes from the tree.
I am trying to delete all students who had a grade < 50 (so failed).
Once a node is deleted one of the following needs to occur:
Left child is empty: Replace the node by its right child.
Left child is not empty: Replace the node by the highest element in the left
branch.
My understanding of this is, if this was the tree:
1
/ \
2 3
/ \ /\
4 5 6 7
If 2 failed i.e. had a grade < 50
You would end up with
1
/ \
4 3
\ / \
5 6 7
4 being the highest element in the left branch.
If this was the tree:
1
/ \
2 3
\ / \
5 6 7
and 2 failed
you would end up with
1
/ \
5 3
/ \
6 7
If this was the tree:
1
/ \
2 3
/ \ / \
4 5 6 7
and 1 failed
you would end up with
5
/ \
2 3
/ / \
4 6 7
I have been having a lot of trouble trying to create functions to do this, at the moment I have:
void checkBranch() //called from main - used to pass the root node to checkBranch()
{
checkBranch(studentTree.getRoot());
}
bool checkBranch(BTNode<Student>* currentNode)
{
if (currentNode != NULL)
{
if (checkBranch(currentNode -> getLeft()))
{
deleteNode(currentNode, true);
}
if (checkBranch(currentNode -> getRight()))
{
deleteNode(currentNode, false);
}
return (currentNode -> getData().getGrade() < 50.0);
}
else
{
return false;
}
}
Now I am trying to add the deleteNode function where I am just stuck on what to do / handle what needs to occur:
void deleteNode(BTNode<Student> *parentNode, bool left)
{
BTNode<Student>* nodeToDelete;
if (left)
{
nodeToDelete = parentNode->getLeft();
}
}

I managed to achieve it with this:
template <typename dataType>
void BTree<dataType>::deleteNode(BTNode<dataType> *parentNode, bool left)
{
BTNode<dataType> *nodeToDelete;
BTNode<dataType> *currentNode;
if (left)
{
nodeToDelete = parentNode->getLeft();
}
else
{
nodeToDelete = parentNode->getRight();
}
if (nodeToDelete->getLeft() == NULL)
{
currentNode = nodeToDelete;
if (left)
{
parentNode->setLeft(nodeToDelete->getRight());
}
else
{
parentNode->setRight(nodeToDelete->getRight());
}
delete nodeToDelete;
}
else
{
BTNode<dataType>* greatestNode = nodeToDelete->getLeft();
if (greatestNode->getRight() == NULL)
{
nodeToDelete->getLeft()->setRight(nodeToDelete->getRight());
if (left)
{
parentNode->setLeft(nodeToDelete->getLeft());
}
else
{
parentNode->setRight(nodeToDelete->getLeft());
}
}
else
{
while (greatestNode->getRight()->getRight())
{
greatestNode = greatestNode->getRight();
}
nodeToDelete->setData(greatestNode->getRight()->getData());
BTNode<dataType> *nodeToDelete2 = greatestNode->getRight();
greatestNode->setRight(greatestNode->getRight()->getLeft());
delete nodeToDelete2;
}
}
}

First, your checkBranch is kind of wrong. What happens if the root node is invalid? It will not be deleted. I suggest a more elegant way here using a callback approach that could like this:
int deleteNodesOnCondition(BTNode<Student>* node, bool(* condition)(BTNode<Student>*))
{
int deleteCount = -1;
if(condition != NULL)
{
deleteCount = 0;
if(node != NULL)
{
deleteCount += deleteNodesOnCondition(node->getLeft(), condition);
deleteCount += deleteNodesOnCondition(node->getRight(), condition);
bool satisfied = condition(node);
if(satified)
{
deleteNode(node);
deleteCount += 1;
}
}
}
return deleteCount;
}
bool checkValidGrade(BTNode<Student> *node)
{
return node != NULL && node->getData().getGrade() >= 50.0;
}
void checkBranch()
{
deleteNodesOnCondition(studentTree.getRoot(), checkValidGrade);
}
For the delete implementation check here where found == true. Note: this code is not tested.

Related

Code for finding Lowest Common Ancestor is not working for some test cases

I am working on this exercise from Hackerrank(https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor/copy-from/158548633)
In this exercise, we are given a pointer to the root of the binary search tree and two values v1 and v2. We need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. we have to complete the function lca. It should return a pointer to the lowest common ancestor node of the two values given.
lca has the following parameters:
root: a pointer to the root node of a binary search tree
v1: a node.data value
v2: a node.data value
I know the recursive solution is easier but I tried this approach and couldn't find any reason for failing test cases. The below-mentioned solution passes only 6/10 test cases. I can't see all the 4 failed test cases but I can mention 1 test case.
here's my code
/*The tree node has data, left child and right child
class Node {
int data;
Node* left;
Node* right;
};
*/
vector<Node*> find(int v, Node* root)
{
vector<Node*> ar;
if (v == root->data) {
ar.push_back(root);
return ar;
}
if (v > root->data) {
ar.push_back(root);
find(v, root->right);
}
if (v < root->data) {
ar.push_back(root);
find(v, root->left);
}
ar.push_back(root);
return ar;
}
Node* lca(Node* root, int v1, int v2)
{
//this vector contains the nodes in path
//to reach from root to node
vector<Node *> a, b;
//find function return the vector
a = find(v1, root);
b = find(v2, root);
//index of the LCA in the vector
int res = 0;
//traversing the two vectors from
//beg to end if two nodes are same
//update the value of res. Final updated
//value will give us LCA
int n = b.size();
if (a.size() < b.size())
n = a.size();
int i = 0;
while (i < n) {
if (a[i] == b[i])
res = i;
i++;
}
return a[res];
}
failed test case:
8
8 4 9 1 2 3 6 5
1 2
Expected output:
1
My output:
8
needed help to debug the code and to find the loophole in this approach.
Below pseudo code can be used for generating the code for LCA problem.
///////////////////
Recursively call on LCA function, one for left subtree and one for right subtree. If the the node is null then return simply null, else
If the value of node is equal to any of the value v1, v2 then simply return this node,
else call recursively for the subtrees.
Now when you receive the result for your 2 recursive calls, check ->
if both are null, then return null
if one is null and the other one is not null, then return the non-null node,
if both are not null, then return the root note itself.
your algorithm cannot work
in find the vector produced by the recursive calls are lost so it like you do not do these calls, at the end you just get the vector produced by the initial call
in lca even supposing find does the right thing you suppose the answer it at the same rank in the 2 vectors, why ?
A way to do is :
Node * Node::lca(int v1, int v2) {
if ((data == v1) || (data == v2))
return this;
Node * r = (right == NULL) ? NULL : right->lca(v1, v2);
Node * l = (left == NULL) ? NULL : left->lca(v1, v2);
return ((r != NULL) && (l != NULL))
? this
: ((r == NULL) ? l : r);
}
A full example :
#include <iostream>
#include <vector>
#include <iomanip>
class Node {
private:
int data;
Node* left;
Node* right;
public:
Node(int d) : data(d), left(NULL), right(NULL) {}
void insert(int d);
void draw(int level = 1) const;
Node * lca(int v1, int v2);
};
void Node::insert(int d) {
Node * n = new Node(d);
Node * t = this;
for (;;) {
if (d > t->data) {
if (t->right == NULL) {
t->right = n;
return;
}
t = t->right;
}
else if (t->left == NULL) {
t->left = n;
return;
}
else
t = t->left;
}
}
void Node::draw(int level) const {
if (right != NULL)
right->draw(level + 1);
else
std::cout << std::setw(level + 1) << '/' << std::endl;
std::cout << std::setw(level) << data << std::endl;
if (left != NULL)
left->draw(level + 1);
else
std::cout << std::setw(level + 1) << '\\' << std::endl;
}
Node * Node::lca(int v1, int v2) {
if ((data == v1) || (data == v2))
return this;
Node * r = (right == NULL) ? NULL : right->lca(v1, v2);
Node * l = (left == NULL) ? NULL : left->lca(v1, v2);
return ((r != NULL) && (l != NULL))
? this
: ((r == NULL) ? l : r);
}
int main()
{
Node r(8);
std::vector<int> v = { 4, 9, 1, 2, 3, 6, 5 };
for (auto d : v)
r.insert(d);
r.draw();
std::cout << "----------------" << std::endl;
Node * l;
std::cout << "lca for 1 & 2" << std::endl;
l = r.lca(1, 2);
if (l != NULL)
l->draw();
std::cout << "----------------" << std::endl;
std::cout << "lca for 5 & 2" << std::endl;
l = r.lca(5, 2);
if (l != NULL)
l->draw();
}
Compilation and execution:
pi#raspberrypi:/tmp $ g++ -Wall lca.cpp
pi#raspberrypi:/tmp $ ./a.out
/
9
\
8
/
6
/
5
\
4
/
3
\
2
\
1
\
----------------
lca for 1 & 2
/
3
\
2
\
1
\
----------------
lca for 5 & 2
/
6
/
5
\
4
/
3
\
2
\
1
\
pi#raspberrypi:/tmp $

BST insertion,deletion,search

I was solving a problem and it stated:
Write a program that processes the following queries on a Binary Search Tree:
i x: Insert x in the BST
d x: Delete x from the BST
Input format
Line 1 contains an integer Q, the number of queries
The next Q lines are of the form i x or d x
Output format
For each query, print the position of x in the BST
If the position of a node is p, the positions of its left and right children are 2*p and 2*p+1 respectively
Position of the root node is 1
Question's link
11 //Queries
i 15 //i=insert; d=delete
i 9
i 25
i 8
i 13
i 18
i 19
i 7
i 11
d 9
i 14
Everything is working fine until I delete node 9. then the position of node 14 is coming out to be 5. see the diagram:Initially,
15
/ \
9 25
/ \ /
8 13 18
/ / \
7 11 19
After deleting 9;
15
/ \
11 25
/ \ /
8 13 18
/ \
7 19
After inserting 14
15
/ \
11 25
/ \ /
8 14 18
/ / \
7 13 19
Correct format should be
15
/ \
11 25
/ \ /
8 13 18
/ \ \
7 14 19
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll position=1;
struct BSTNode
{
int data;
BSTNode *left,*right;
};
BSTNode *getNewNode(int data)
{
BSTNode *newNode = new BSTNode();
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode; //returns address of new node
}
BSTNode* insert(BSTNode *root,int data)
{
if(root==NULL){
root = getNewNode(data);
}
else if(data<root->data)
{
root->left = insert(root->left,data);
}
else if(data>root->data)
{
root->right = insert(root->right,data);
}
return root;
}
BSTNode *findMin(BSTNode *root)
{
if(root->left ==NULL)
{
return root;
}
else
findMin(root->left);
}
bool search(BSTNode *root,int data)
{
if(root == NULL)
{
return 0;
}
if(data<root->data)
{
position=2*position;
search(root->left,data);
}
else if(data>root->data)
{
position=2*position+1;
search(root->right,data);
}
else
{
cout<<"Found";
return 1;
}
}
BSTNode* delet(BSTNode* root,int data)
{
if(root == NULL)
{
return 0;
}
else if(data<root->data)
{
root->left = delet(root->left,data);
}
else if(data>root->data)
{
root->right = delet(root->right,data);
}
else //Found
{
//CASE 1:No child
if(root->left == root->right ==NULL)
{
delete root;
root = NULL;
}
//CASE2: One child
else if(root->left == NULL)
{
BSTNode *temp= root;
root = root->right;
delete temp;
}
else if(root->right == NULL)
{
BSTNode *temp=root;
root= root->left;
delete temp;
}
//CASE 3: TWO CHILD
else
{
BSTNode *temp = findMin(root->right);
root->data = temp->data;
root->right = delet(root->right,root->data);
}
return root;
}
}
int main()
{
BSTNode* root = NULL; //rootptr- pointer to node
//tree is empty
int n,input,data,del;
char c;
cin>>n;
while(n--)
{
cin>>c;
cin>>input;
if(c=='i')
{
root = insert(root,input);
search(root,input);
}
if(c=='d')
{
search(root,input);
delet(root,input);
}
cout<<position<<endl;
position=1;
}
return 0;
}
How is this possible insertion is being done as a leaf node Then?
in the line
Correct format should be
your diagram is wrong. 14 is the right child of 13.
Assume that you want to insert a node is a binary search tree, you need to save the last parent node in the your searching path if the given node is not found. and insert the new node to the last parent node got in the function insert(). The program is not recursive.
here is an example in C.
#include<stdio.h>
#include<stdlib.h>
struct t_Data
{
int m_Info;
};
struct t_Node
{
struct t_Data m_Data;
struct t_Node* m_LeftChild;
struct t_Node* m_RightChild;
};
typedef struct t_Node* t_BinarySortTree;
/* return targetNode or the last node in the path */
int SearchBST(t_BinarySortTree T, int aGivenInfo, struct t_Node* lastParentNode, struct t_Node* *result)
{
if(!T)
{
*result = lastParentNode;
return 0;
}
else if (aGivenInfo == (*T).m_Data.m_Info)
{
*result = T;
return 1;
}
else if(aGivenInfo < (*T).m_Data.m_Info)
{
lastParentNode = T;
return SearchBST((*T).m_LeftChild,aGivenInfo,lastParentNode,result);
}
else
{
lastParentNode = T;
return SearchBST((*T).m_RightChild,aGivenInfo,lastParentNode,result);
}
}
void InsertBST(t_BinarySortTree *T, struct t_Data newData)
{
int status;
struct t_Node* result;
status = SearchBST(*T,newData.m_Info,NULL,&result);
/* condition: fail to search for 'newData' in the binary sort tree */
if (!status)
{
struct t_Node* newNode;
newNode = (struct t_Node*)malloc(sizeof(struct t_Node));
(*newNode).m_Data = newData;
(*newNode).m_LeftChild = NULL;
(*newNode).m_RightChild = NULL;
/* condition: result == NULL */
if (!result)
{
*T = newNode;
}
else if (newData.m_Info < (*result).m_Data.m_Info)
{
(*result).m_LeftChild = newNode;
}
/* condition: newData.m_Info > (*result).m_Data.m_Info */
else
{
(*result).m_RightChild = newNode;
}
}
}
int main(void)
{
t_BinarySortTree aBST;
aBST = NULL;
struct t_Data d1,d2,d3,d4,d5,d6,d7,d8,d9,d10;
d1.m_Info = 62;
d2.m_Info = 88;
d3.m_Info = 58;
d4.m_Info = 47;
d5.m_Info = 35;
d6.m_Info = 73;
d7.m_Info = 51;
d8.m_Info = 99;
d9.m_Info = 37;
d10.m_Info = 93;
InsertBST(&aBST,d1);
InsertBST(&aBST,d2);
InsertBST(&aBST,d3);
InsertBST(&aBST,d4);
InsertBST(&aBST,d5);
InsertBST(&aBST,d6);
InsertBST(&aBST,d7);
InsertBST(&aBST,d8);
InsertBST(&aBST,d9);
InsertBST(&aBST,d10);
}

Generating a balanced binary tree

I am not sure if this is doing what it is supposed to do.
My goal is to generate a balanced binary tree, from a set of values.
Please let me know if this is correct.
NOTE: NOT a balanced binary search tree, just balanced binary tree.
int heightPrivate(nodePtr node)
{
if (node == NULL)
return -1;
return 1 + std::max(heightPrivate(node->left), heightPrivate(node->right));
}
void addNodePrivate(nodePtr node, int val)
{
if (root == NULL)
{
root = new BTNode;
root->data = val;
root->left = root->right = NULL;
}
else
{
if (node->left == NULL)
{
node->left = new BTNode;
node->left->data = val;
node->left->left = node->left->right = NULL;
}
else if (node->right == NULL)
{
node->right = new BTNode;
node->right->data = val;
node->right->left = node->right->right = NULL;
}
else
{
int lheight = heightPrivate(node->left);
int rheight = heightPrivate(node->right);
if (lheight < rheight)
addNodePrivate(node->left, val);
else if (rheight < lheight)
addNodePrivate(node->right, val);
else
addNodePrivate(node->left, val);
}
}
}
void printPostorderPrivate(nodePtr p, int indent=0)
{
if(p != NULL) {
if(p->left) printPostorderPrivate(p->left, indent+4);
if(p->right) printPostorderPrivate(p->right, indent+4);
if (indent) {
std::cout << std::setw(indent) << ' ';
}
std::cout<< p->data << " \n ";
}
}
In main...
int main()
{
BTree tree;
tree.addNode(1);
tree.addNode(2);
tree.addNode(3);
tree.addNode(4);
tree.addNode(5);
tree.addNode(6);
tree.addNode(7);
tree.printPostorder();
The result I get is this:
7
4
6
2
5
3
1
The children of 2 are 4 and 5. The question is why is it 7 going on the next level.
The reason that 7 appears where it does is because in the addNodePrivate method checks the heights of the two child branches, and if they are equal it goes left.
So as you insert 7, when the program is at the root (node 1) it sees that the height of the left branch and height of the right branch are both equal to 1 (node 2 had children 5 and 4 but no grandchildren, and node 3 has child 6 and also no grandchildren), and so it goes left - down the branch with node 2.
To achieve what you want, you need to choose the branch which has the shortest path, so comparing the height of two branches is not enough.
Hope that helps, best of luck.

Pre-Order Function of BST

I am having trouble getting my preView300 function working correctly. I know the algerithm is working correctly because I have seen other examples the exact same way. I believe its with my findMaxNode/removeNodefunctions. I have tried passing different pointer into my findMaxNode function but nothing that is correct.
For example:
I insert these numbers in this order, 8,4,12,2,6,10,14,20 and call me remove function to remove 8. I get this output 6,4,2,12,10,14,20. I know the findMaxNode is trying to do is get rid of the 8 and replace it with 20 and delete the node containing twenty and connect everything back up right.
If I can get some help on this on why this process isn't working please let me know. Also let me know if I have to add anything to this post inorder to help you.
Struct definition:
typedef float Element300;
struct TreeNode300;
typedef TreeNode300 * TreePtr300;
struct TreeNode300
{
Element300 element;
TreePtr300 left;
TreePtr300 right;
};
Remove Function:
void BST300::remove300(const Element300 element, TreePtr300 & root)
{
if(root == NULL)
{
cerr << "Error: Remove Failed!" << endl;
}
else if(element == root->element )
{
removeNode300(root);
}
else if(element < root->element)
{
remove300(element, root->left);
}
else
{
remove300(element, root->right);
}
return;
}
Remove Node function:
void BST300::removeNode300(TreePtr300 & root)
{
TreePtr300 tempPointer = NULL;
if(root->left == NULL && root->right == NULL)
{
delete root;
root = NULL;
}
else if(root->right == NULL)
{
tempPointer = root;
root = root->left;
tempPointer->left = NULL;
delete tempPointer;
}
else if(root->left == NULL)
{
tempPointer = root;
root = root->right;
tempPointer->right = NULL;
delete tempPointer;
}
else
{
findMaxNode300(root->right, tempPointer);
root->element = tempPointer->element;
delete tempPointer;
}
tempPointer = NULL;
return;
}
find Maximum value function:
void BST300::findMaxNode300(TreePtr300 & root, TreePtr300 & tempPointer)
{
if(root->right == NULL)
{
tempPointer = root;
root = root->left;
tempPointer->left = NULL;
}
else
{
findMaxNode300(root->right, tempPointer);
}
return;
}
inView:
void BST300::inView300(const TreePtr300 root)const
{
if(root)
{
inView300(root->left);
cout << root->element << "-> ";
inView300(root->right);
}
return;
}
preView:
void BST300::preView300(const TreePtr300 root) const
{
if(root)
{
cout << root->element << "-> ";
preView300(root->left);
preView300(root->right);
}
return;
}
Results:
PREVIEW
------------------------------------------------------
Begin -> 20-> 4-> 2-> 6-> 12-> 10-> 14-> End
INVIEW
------------------------------------------------------
Begin -> 2-> 4-> 6-> 20-> 10-> 12-> 14-> End
Expected Results:
PREVIEW
------------------------------------------------------
Begin -> 20-> 4-> 2-> 6-> 12-> 10-> 14-> End
INVIEW
------------------------------------------------------
Begin -> 2.0-> 4.0-> 6-> 10-> 12-> 14-> 20 -> End
Binary Search Tree Original:
8
4 12
2 6 10 14
- 20
Expected:
20
4 12
2 6 10 14
The logic in your removeNode300 function is incorrect. When you remove a node with two children, you replace the element to be deleted with the greatest element in that branch, and delete the old-greatest node. Take this conceptual example of a tree:
5
3 7
2 4 6 8
Removing 5 from the tree then makes it look like:
8
3 7
2 4 6 -
Seven is not greater than eight, so things fall apart here.
You can either replace the node you want to delete with the greatest on the left side, or the lowest on the right. You can easily fix this right away by changing the call to findMaxNode300 in removeNode300. Pass it root->left instead:
void BST300::removeNode300(TreePtr300 & root)
{
if(root->left == NULL && root->right == NULL) {}
else
{
findMaxNode300(root->left, tempPointer); //this line
}
}
Obviously keep the rest of the code in, I just removed it to make this less verbose.

Binary tree and especial node printing

I have a tree like this in binary representation;
1
/
2
\
3
/ \
6 4
\ \
7 5
/
8
/
9
\
10
\
11
But in reality this is not binary tree but like
1
/ | | \
2 3 4 5
/\ |
6 7 8
/| \
9 10 11
Can you please help me getting printed out something like(childs are printed out in reversed order)
1 : 5 4 3 2
5 : 8
3 : 7 6
8 : 11 10 9
My TNode class looks like.
class TNode {
public:
unsigned long int data;
TNode * left;///first child
TNode * right;/// sibling
TNode * parent;/// parent
TNode(unsigned long int d = 0, TNode * p = NULL, TNode * n = NULL)///konstruktors
{
left = NULL;
right = NULL;
parent = p;
data = d;
}
};
Does this need stack implementation?
What about this approach:
Traverse the tree in preorder (visiting each node). For each node (when travelled to it) check if it has a left child. If so (indicating a node with child elements in original tree) print the nodes data with a ':' and take its subtree and follow all right sons (which are representing all siblings) recursively, afterwards print each right sons data (you have it in reversed order.
In Code:
void print_siblings() {
if (this->right != NULL) {
this->right->print_siblings();
}
cout << data << ", ";
}
void traverse(void) {
if (this->left != NULL) {
cout << data << ":";
this->left->print_siblings();
cout << endl;
this->left->traverse();
}
if (this->right != NULL) {
this->right->traverse();
}
}
Edit: Here is a inorder-traversal:
void traverse_inorder(void) {
if (this->left != NULL) {
this->left->traverse_inorder();
cout << data << ":";
this->left->print_siblings();
cout << endl;
}
if (this->right != NULL) {
this->right->traverse_inorder();
}
}
Output for preorder would be:
1:5, 4, 3, 2,
3:7, 6,
5:8,
8:11, 10, 9,
Output for inorder would be:
3:7, 6,
8:11, 10, 9,
5:8,
1:5, 4, 3, 2,
Edit2: Just for completeness, the post-order traversal as well :-)
void traverse_postorder(void) {
if (this->left != NULL) {
this->left->traverse_postorder();
}
if (this->right != NULL) {
this->right->traverse_postorder();
}
if (this->left != NULL) {
cout << data << ":";
this->left->print_siblings();
cout << endl;
}
}
And its output:
8:11, 10, 9,
5:8,
3:7, 6,
1:5, 4, 3, 2,
Try something like: (Not tested yet)
printTree(TNode root) {
queue <TNode> proc;
proc.push(root);
while (!proc.empty()) {
TNode front = proc.front();
proc.pop();
// add front's children to a stack
stack <Tnode> temp;
Tnode current = front->left;
while (current != NULL) {
temp.push(current);
current = current->right;
}
// add the children to the back of queue in reverse order using the stack.
// at the same time, print.
printf ("%lld:", front->data);
while (!temp.empty()) {
proc.push(temp.top());
printf(" %lld", temp.top()->data);
temp.pop();
}
printf("\n");
}
}
I am still trying to make it more elegant. Thanks for the interesting problem!
Edit: Changed the format specifiers to lld
I've made something like this, but kind'a unrecursive due to while loops.
Is there any way making it more rec
void mirrorChilds(TNode * root)//
{
if(!root) return;
cout << root->data << " ";
//tmp = tmp->right;
if(!root->left) return;
TNode * tmp = root->left;
while(tmp != NULL)
{
root->left = tmp;
tmp = tmp->right;
}
tmp = root->left;
while(root != tmp)
{
cout << tmp->data << " ";
tmp = tmp->parent;
}
cout << endl;
tmp = root->left;
while(root != tmp)
{
if(tmp->left) mirrorChilds(tmp);
//if(tmp->left) cout << tmp->left->data << endl;
tmp = tmp->parent;
}
}
It works prefectly fine, but ye, i kinda trying to get O(n*log(n)) time ...