-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.h
More file actions
103 lines (90 loc) · 2.17 KB
/
BinaryTree.h
File metadata and controls
103 lines (90 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
#ifndef BINARYTREE_H
#define BINARYTREE_H
namespace AlgorithmImpl
{
class BinaryTree
{
public:
char value;
BinaryTree *leftChild;
BinaryTree *rightChild;
BinaryTree(BinaryTree *left, BinaryTree *right, char value)
{
this->leftChild = left;
this->rightChild = right;
this->value = value;
}
~BinaryTree()
{
//recursiveDelete(this);
}
void recursiveDelete(BinaryTree * &node)
{
if(node == NULL) // i don't think this will happen
return;
if(NULL != node->leftChild)
recursiveDelete(node->leftChild);
if(NULL != node->rightChild)
recursiveDelete(node->rightChild);
delete node;
}
BinaryTree(char value, BinaryTree * left, BinaryTree * right)
{
this->value = value;
this->leftChild = left;
this->rightChild = right;
}
static void printInRotatingOrder(BinaryTree * currentNode)
{
stack<BinaryTree *> leftStack;
stack<BinaryTree *> rightStack;
stack<BinaryTree *> * stack1 = &leftStack;
stack<BinaryTree *> * stack2 = &rightStack;
leftStack.push(currentNode);
while(stack1->size() || stack2->size())
{
BinaryTree * node = stack1->top();
stack1->pop();
cout << node->value << " ";
BinaryTree * childNode1;
BinaryTree * childNode2;
if(stack1 == &leftStack)
{
childNode1 = node->leftChild;
childNode2 = node->rightChild;
}
else
{
childNode2 = node->leftChild;
childNode1 = node->rightChild;
}
if(childNode1!=NULL)
stack2->push(childNode1);
if(childNode2!=NULL)
stack2->push(childNode2);
if(!stack1->size())
swap(stack1, stack2);
}
}
static void TestBinaryTreePrint()
{
BinaryTree *d = new BinaryTree(NULL,NULL, 'd');
BinaryTree *e = new BinaryTree(NULL,NULL, 'e');
BinaryTree *f = new BinaryTree(NULL,NULL, 'f');
BinaryTree *g = new BinaryTree(NULL,NULL, 'g');
BinaryTree *b = new BinaryTree(d,e, 'b');
BinaryTree *c = new BinaryTree(f,g, 'c');
BinaryTree *a = new BinaryTree(b,c, 'a');
BinaryTree::printInRotatingOrder(a);
}
};
}
#endif