algorithms

algorithms Commit Details


Date:2013-02-19 23:47:00 (11 years 10 months ago)
Author:Natalie Adams
Branch:default
Commit:aae1d6bbd33b
Parents: 8b7c98b7acaa
Message:Adding algorithms for navigating a tree

Changes:
Acpp/algorithms/src/Node.h (full)
Acpp/algorithms/src/Tree.h (full)
Mcpp/algorithms/algorithms.vcxproj (1 diff)
Mcpp/algorithms/algorithms.vcxproj.filters (1 diff)
Mcpp/algorithms/src/main.cpp (1 diff)

File differences

cpp/algorithms/algorithms.vcxproj
2020
2121
2222
23
2324
2425
2526
27
2628
2729
2830
<ClInclude Include="src\InsertSort.h" />
<ClInclude Include="src\MedianSort.h" />
<ClInclude Include="src\MergeSort.h" />
<ClInclude Include="src\Node.h" />
<ClInclude Include="src\QuickSort.h" />
<ClInclude Include="src\SearchAlgorithm.h" />
<ClInclude Include="src\SortAlgorithm.h" />
<ClInclude Include="src\Tree.h" />
<ClInclude Include="src\vector_helper.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
cpp/algorithms/algorithms.vcxproj.filters
5050
5151
5252
53
54
55
56
57
58
5359
5460
<ClInclude Include="src\BinarySearch.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Node.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Tree.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
cpp/algorithms/src/Node.h
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
#ifndef NODE_H
#define NODE_H
#define null 0
#include <string>
#include <iostream>
template <class T>
class Node
{
private:
Node * createNode(T obj)
{
Node<T> * newnode = new Node<T>(obj);
newnode->parent = this;
return newnode;
}
void initVars()
{
this->parent = 0;
this->left = 0;
this->right = 0;
}
public:
Node(T obj) { this->object = obj; initVars(); }
T object;
Node * parent;
Node * left;
Node * right;
Node<T> * addLeft(T obj)
{
if (this->left != null)
throw std::string("Left is not null!");
this->left = createNode(obj);
}
Node<T> * addRight(T obj)
{
if (this->right != null)
throw std::string("Right is not null!");
this->right = createNode(obj);
}
void addLeftRight(T left, T right)
{
this->addLeft(left);
this->addRight(right);
}
};
#endif
cpp/algorithms/src/Tree.h
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
#ifndef TREE_H
#define TREE_H
#include "Node.h"
#include <vector>
template <class T>
class Tree
{
private:
void _DeleteTree(Node<T> * node)
{
cout << "Killing node: " << node->object << endl;
if (node->left != null)
_DeleteTree(node->left);
if (node->right != null)
_DeleteTree(node->right);
delete node;
node = null;
}
public:
Node<T> * root;
void DeleteTree()
{
this->_DeleteTree(root);
}
Tree(Node<T> * root)
{
this->root = root;
}
~Tree()
{
//DeleteTree(root);
}
std::vector<Node<T> *> getBoundary()
{
std::vector<Node<T> *> edges;
edges.push_back(root);
std::vector<Node<T> *> tmp;
tmp = getLeftEdges(root->left);
edges.insert(edges.end(), tmp.begin(), tmp.end());
tmp = getRightEdges(root->right);
edges.insert(edges.end(), tmp.begin(), tmp.end());
return edges;
}
std::vector<Node<T> *> getLeftEdges(Node<T> * nodes)
{
std::vector<Node<T> *> ret;
if (nodes == null)
return ret;
if (nodes->parent->right == nodes) //if the node is a right child
{
//add current node
if (nodes->right == null && nodes->left == null)
ret.push_back(nodes);
}
else if (nodes->parent->left == nodes) // if the node is a left child
{
ret.push_back(nodes);
}
std::vector<Node<T> *> tmp;
tmp = getLeftEdges(nodes->left);
ret.insert(ret.end(), tmp.begin(), tmp.end());
tmp = getLeftEdges(nodes->right);
ret.insert(ret.end(), tmp.begin(), tmp.end());
return ret;
}
std::vector<Node<T> *> getRightEdges(Node<T> * nodes)
{
std::vector<Node<T> *> ret;
if (nodes == null)
return ret;
std::vector<Node<T> *> tmp;
tmp = getRightEdges(nodes->left);
ret.insert(ret.end(), tmp.begin(), tmp.end());
tmp = getRightEdges(nodes->right);
ret.insert(ret.end(), tmp.begin(), tmp.end());
if (nodes->parent->right == nodes) //if the node is a right child
{
//add current node
ret.push_back(nodes);
}
else if (nodes->parent->left == nodes) // if the node is a left child
{
if (nodes->right == null && nodes->left == null)
ret.push_back(nodes);
}
return ret;
}
};
#endif
cpp/algorithms/src/main.cpp
99
1010
1111
12
13
1214
1315
1416
1517
1618
17
18
19
1920
2021
22
2123
2224
2325
24
25
26
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
2751
#include "QuickSort.h"
#include "MergeSort.h"
#include "BinarySearch.h"
#include "Node.h"
#include "Tree.h"
using namespace std;
int main()
{
MergeSort<int> sort1;
//sort1.initContainer(create_vector<string>("test")("test2"));
/*//sort1.initContainer(create_vector<string>("test")("test2"));
//cout << sort1.ToString();
//vector<int> arr1 = create_vector<int>(
MedianSort<int> sort1;
sort1.initContainer(create_vector<int>(3)(2)(4)(3)(1));
sort1.Sort();
cout << sort1.ToString() << endl;
BinarySearch<int> search1;
search1.initContainer(sort1.getContainer());
cout << search1.Search(7) << endl;
//BinarySearch<int> search1;
//search1.initContainer(sort1.getContainer());
//cout << search1.Search(7) << endl;*/
Node<int> * root = new Node<int>(30);
Node<int> * l = root->addLeft(20);
Node<int> * r = root->addRight(40);
l->addLeft(10)->addLeftRight(5,15);
l->addRight(25)->addRight(28);
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 << " ";
}
cout << endl;
tree.DeleteTree();
//root->addRight(
//root->left
//Tree<int> t(root);
//delete root;
}

Archive Download the corresponding diff file

Branches

Tags

Page rendered in 0.41600s using 14 queries.