-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchTree.h
More file actions
72 lines (64 loc) · 1.93 KB
/
binarySearchTree.h
File metadata and controls
72 lines (64 loc) · 1.93 KB
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
#include <iostream>
using namespace std;
template<class T>
struct Node {
public:
Node() : data_(nullptr), rChild_(nullptr), lChild_(nullptr) {}
Node(struct Node *other)
{
if (other == nullptr) {
return;
}
Node *root = new Node();
root->data_ = other->data;
root->right = Node(other->right);
root->left = Node(other->left);
return root;
}
/* pre-order algorithm using a lambda to do the required processing*/
static void
preOrderProcessor(struct Node* root, std::function<void(struct Node *n)>f)
{
if (root == nullptr) {
return;
}
f(root);
preOrderProcessor(root->right, f);
preOrderProcessor(root->left, f);
}
/* Breadth first traversal using a lambda to do the required processing */
static void
breadthFirstProcessor(struct Node* root, std::function<void(struct Node *n)>f)
{
if (root == nullptr) {
return;
}
queue<struct Node *> q;
q.push(root);
while(!q.empty()) {
auto n = q.pop();
f(n);
if (n->rChild) {
q.push(n->rChild);
}
if (n->lChild) {
q.push(n->lChild);
}
}
}
/* TODO Write a depth first algorithm using a lambda */
/* TODO Write a post-order algorithm using a lambda */
/* TODO Write a in-order algorithm using a lambda */
friend ostream& operator<<(ostream &out, struct Node *node)
{
/* TODO print the tree rooted at node using breadth first algorithm and a lambda */
out << "Not implemented yet!";
return out;
}
/* TODO Write destructors as I am using raw pointers all over */
/* TODO Write a method to generate a random BST */
/* TODO Does it make sense to use smart pointers here */
T* data_;
struct Node *lChild_;
struct Node *rChild_;
};