-
Notifications
You must be signed in to change notification settings - Fork 0
/
findSum.cpp
102 lines (75 loc) · 2.02 KB
/
findSum.cpp
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
//Input/output stream
#include<iostream>
#include<vector>
#include<memory>
struct BinaryTreeNode
{
int m_value;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
BinaryTreeNode(int value){this->m_value = value; this->m_pLeft = NULL; this->m_pRight = NULL; }
};
class BuildBinaryTree{
private: void addNode(BinaryTreeNode* root, int value);
public: BinaryTreeNode* buildBinaryTree (int num[], int length);
void printBinaryTree(BinaryTreeNode* root);
};
BinaryTreeNode*
BuildBinaryTree::buildBinaryTree (int num[], int length)
{
BinaryTreeNode* root = new BinaryTreeNode(num[0]);
BinaryTreeNode* head = root;
for (int i =1; i<length; i++)
{
addNode(root, num[i]);
std::cout<< "poiner ....." << root->m_pRight << " " << root->m_pLeft<<"\n";
std::cout<<"Ying Liu .... 2"<< num[i] <<"\n";
}
// std::cout<< "poiner ....." << head->m_pRight << " " << head->m_pLeft<<"\n";
return head;
}
void
BuildBinaryTree::addNode(BinaryTreeNode* root, int value)
{
if (root == NULL) {
std::cout<<"Ying Liu .... 3"<<"\n";
root = new BinaryTreeNode (value);
}
else if (root->m_value < value)
{
std::cout<<"Ying Liu .... 4"<<"\n";
addNode(root->m_pRight, value);
}
else
{
std::cout<<"Ying Liu .... 5"<<"\n";
addNode(root->m_pLeft, value);
}
}
void
BuildBinaryTree::printBinaryTree(BinaryTreeNode* root)
{
if(root==NULL)
return;
else
{
printBinaryTree(root->m_pLeft);
std::cout<<root->m_value<<" ";
printBinaryTree(root->m_pRight);
}
}
int main(int argc, char** argv)
{
int num[] = {47,24,41,14,25,31,39,21,28,36,32};
int length = sizeof(num)/sizeof(*num);
BuildBinaryTree* tree;
BinaryTreeNode* root = tree->buildBinaryTree (num, length);
tree->printBinaryTree(root);
BinaryTreeNode* root1 = new BinaryTreeNode(1);
BinaryTreeNode* root2 = new BinaryTreeNode(2);
BinaryTreeNode* root3 = new BinaryTreeNode(3);
root1->m_pLeft = root2;
root1->m_pRight = root3;
tree->printBinaryTree(root1);
return -1;
}