-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreesearch.cpp
104 lines (85 loc) · 1.8 KB
/
treesearch.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
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <list>
#include <queue>
using namespace std;
class Node {
public:
Node(){}
Node(int key, double val):key(key), val(val){}
int key;
double val;
list<Node* > children;
};
void print_node(Node* node) {
if (node!=NULL) {
printf("key = %d and val = %3.4f \n",node->key,
node->val);
}
else {
printf("Node doesn't exist!\n");
}
}
void print_tree_df(Node* node) {
//print_node(node);
if (!node->children.empty()) {
list<Node* >::iterator it;
for (it=node->children.begin();
it!=node->children.end(); ++it){
//printf("key=%d\n", (*it)->key );
print_node(*it);
print_tree_df(*it);
}
}
}
void push_children(queue<Node* >& nodes, Node* node){
list<Node* >::const_iterator it;
for (it = node->children.begin();
it!= node->children.end(); ++it){
nodes.push(*it);
}
}
void print_tree_bf(Node* root){
queue<Node* > allnodes;
allnodes.push(root);
while(!allnodes.empty()){
Node* node = allnodes.front();
allnodes.pop();
print_node(node);
push_children(allnodes,node);
}
}
Node* find_node(Node* root, int key){
queue<Node* > allnodes;
allnodes.push(root);
while(!allnodes.empty()){
Node* node = allnodes.front();
allnodes.pop();
if(node->key == key){
return node;
}
push_children(allnodes,node);
}
return NULL;
}
void create_testtree(Node& root) {
root.key = 5;
root.val = 3.7;
Node* child1 = new Node(7,8.3);
Node* child2 = new Node(8,5.2);
Node* child3 = new Node(9,3.4);
Node* child21 = new Node(15,-0.1);
child2->children.push_back(child21);
root.children.push_back(child1);
root.children.push_back(child2);
root.children.push_back(child3);
}
int main (){
Node root;
create_testtree(root);
print_node(&root);
print_tree_df(&root);
print_tree_bf(&root);
Node* node = find_node(&root, 10);
print_node(node);
return 0;
}