-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTreeDisplayIterativeDepthFirstSearch.cpp
More file actions
133 lines (101 loc) · 3.16 KB
/
BinarySearchTreeDisplayIterativeDepthFirstSearch.cpp
File metadata and controls
133 lines (101 loc) · 3.16 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Display the nodes of a binary search tree in depth first search order.
//
// Implemented using iteration instead of recursion.
// O(n) display
// O(log(n)) to O(n) insert
//
// Complier: Visual Studio 2013 (v120)
#include <iostream>
#include <memory>
#include <list>
#include <cassert>
#include <utility>
using namespace std;
template <typename T>
class BinarySearchTree {
public:
// ** Majority of ADT methods omitted ** //
bool empty() const { return !root.get(); }
bool insert(const T& key) {
if (empty()) {
root = unique_ptr<Node>(new Node{ key, nullptr, nullptr });
return true;
}
Node* current = root.get();
Node* parent = nullptr;
// Traverse tree downwards until we find an empty node.
while (current) {
parent = current;
// Duplicate key - all elements should be unique
if (key == current->data)
return false;
// If key is less go left or if key is greater go right.
current = (key < current->data) ? current->left.get() : current->right.get();
}
assert(current == nullptr);
// Insert key according to < or > parent data
if (key < parent->data)
parent->left.reset(new Node{ key });
else
parent->right.reset(new Node{ key });
return true;
}
// Traverse downwards displaying in a depth first search (non-recursive)
void display_DFS() const {
cout << endl << "############ Binary Search Tree Contents - Depth First Search (In Order Traversal) ############ " << endl;
// first: false = search, true = visit
list<pair<bool, Node*>> stack;
stack.push_front(pair<bool, Node*>(false, root.get()));
while (!stack.empty()) {
display_stack_contents(stack);
pair<bool, Node*> current = stack.front();
stack.pop_front();
// If visiting display the current node and continue on to the next node on the stack.
if (current.first == true) {
cout << "Visiting: " << current.second->data << endl;
continue;
}
// If we are here it means we are searching the current node.
if (current.second->right.get())
stack.push_front(pair<bool, Node*>(false, current.second->right.get()));
// Mark current node for visit and push back onto stack.
if (current.second)
stack.push_front(pair<bool, Node*>(true, current.second));
if (current.second->left.get())
stack.push_front(pair<bool, Node*>(false, current.second->left.get()));
}
cout << endl;
}
private:
struct Node {
T data;
unique_ptr<Node> left;
unique_ptr<Node> right;
};
void display_stack_contents(const list<pair<bool, Node*>>& stack) const {
cout << "[ ";
for (const auto& e : stack)
cout << "(" << e.first << "|" << e.second->data << "),";
cout << "] " << endl;
}
unique_ptr<Node> root = nullptr;
};
int main()
{
auto tree = new BinarySearchTree<int>();
tree->insert(10);
tree->insert(11);
tree->insert(9);
tree->insert(4);
tree->insert(8);
tree->insert(3);
tree->insert(14);
tree->insert(12);
tree->insert(16);
tree->insert(2);
tree->insert(13);
tree->display_DFS();
cout << "[Press enter to exit]" << endl;
cin.ignore();
return 0;
}