algorithms

algorithms Commit Details


Date:2013-02-20 23:28:00 (11 years 10 months ago)
Author:Natalie Adams
Branch:default
Commit:e5c10fcd2400
Parents: aae1d6bbd33b
Message:Commiting a working example of BFS and DFS

Changes:
Mcpp/algorithms/src/Tree.h (1 diff)
Mcpp/algorithms/src/main.cpp (1 diff)

File differences

cpp/algorithms/src/Tree.h
3434
3535
3636
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
3794
3895
3996
//DeleteTree(root);
}
//Node<T> * DFS(T value)
//{
//return dfs(value, this->root);
//}
Node<T> * BFS(T value)
{
std::vector<Node<T> *> stack;
stack.push_back(this->root);
Node<T> * curr;
while (stack.size() != 0)
{
curr = stack.at(0);
stack.erase(stack.begin());
cout << "Examining: " << curr->object << endl;
if (curr->object == value)
return curr;
if (curr->left != null)
stack.push_back(curr->left);
if (curr->right != null)
stack.push_back(curr->right);
}
return null;
}
Node<T> * BFS_R(T value)
{
return bfs_r(value, this->root);
}
Node<T> * bfs_r(T value, Node<T> * nodes)
{
Node<T> * ret = null;
if (nodes)
cout << "Examined: " << nodes->object << endl;
if (nodes->object == value)
return nodes;
if (nodes->left)
{
ret = bfs_r(value, nodes->left);
if (ret && ret->object == value)
return ret;
}
if (nodes->right)
{
ret = bfs_r(value, nodes->right);
if (ret && ret->object == value)
return ret;
}
return ret;
}
std::vector<Node<T> *> getBoundary()
{
std::vector<Node<T> *> edges;
cpp/algorithms/src/main.cpp
3434
3535
3636
37
38
39
40
41
37
38
39
40
41
42
4243
4344
4445
r->addLeft(35);
r->addRight(50)->addRight(41);
Tree<int> tree(root);
vector<Node<int> *> nodes = tree.getBoundary();
for(size_t i = 0; i < nodes.size(); i++)
{
cout << nodes[i]->object << " ";
}
//vector<Node<int> *> nodes = tree.getBoundary();
//for(size_t i = 0; i < nodes.size(); i++)
//{
//cout << nodes[i]->object << " ";
//}
cout << tree.BFS_R(25)->object << endl;
cout << endl;
tree.DeleteTree();
//root->addRight(

Archive Download the corresponding diff file

Branches

Tags

Page rendered in 0.41085s using 14 queries.