-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree.cpp
93 lines (74 loc) · 1.51 KB
/
binary_tree.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
86
87
88
89
90
91
92
93
#include <bits/stdc++.h>
using namespace std;
struct BstNode
{
int data;
BstNode* left;
BstNode* right;
};
BstNode* rootPtr;//to store address of root node
BstNode* GetNewNode(int data)
{
BstNode* newNode = new BstNode();
newNode->data = data;// to store data
newNode->left = newNode->right = NULL;//left & right is empty
return newNode;
}
BstNode* Insert(BstNode *root,int data)//void寫法: 原本傳pointer進去就要一個*,因此再傳pointer的address還要多一個*
{
if (root == NULL)//void寫法: rootPtr is empty,so get a new node
{
root = GetNewNode(data);
return root;
}
else if (data <= root->data)
{
root->left = Insert(root->left,data);
}
else
{
root->right = Insert(root->right,data);
}
return root;
}
BstNode* dfs(BstNode *root)
{
if (root == NULL)
{
return root;
}
else
{
cout << root->data;
dfs(root->left);
dfs(root->right);
}
}
bool search(BstNode* root,int data)
{
if (root == NULL)
return false;
else if (root->data == data)
return true;
else if (data <= root->data)
return search(root->left,data);
else
return search(root->right,data);
}
int main()
{
#ifdef DBG
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
BstNode* root_main = NULL;
root_main = Insert(root_main,15);//return pointer store in root
root_main = Insert(root_main,10);
root_main = Insert(root_main,20);
root_main = Insert(root_main,25);
root_main = Insert(root_main,8);
root_main = Insert(root_main,12);
int number;
dfs(root_main);
return 0;
}