Some essential definitions:
typedef char Element;
struct Node
{
Node( Element inputData ) : data( inputData ) {
cout << "Construct " << data << endl;
}
~Node() {
cout << "Destruct " << data << endl;
if( lchild != NULL ) {
delete lchild;
lchild = NULL;
}
if( rchild != NULL ) {
delete rchild;
rchild = NULL;
}
}
Element data;
Node *lchild;
Node *rchild;
};
typedef Node* Tree;
BFS: Breadth-First Search
// BFS
void breadthFirstSearch(Tree root)
{
queue<Node *> nodeQueue;
nodeQueue.push (root);
Node *node;
while (!nodeQueue.empty()) {
node = nodeQueue.front();
nodeQueue.pop ();
cout << node->data;
if (node->lchild) {
nodeQueue.push(node->lchild);
}
if (node->rchild) {
nodeQueue.push(node->rchild);
}
}
}
DFS: Depth-First Search
// DFS (iterative)
void depthFirstSearch(Tree root)
{
stack<Node *> nodeStack;
nodeStack.push (root);
Node *node;
while (!nodeStack.empty ()) {
node = nodeStack.top();
nodeStack.pop ();
cout << node->data;
if (node->rchild) {
nodeStack.push(node->rchild);
}
if (node->lchild) {
nodeStack.push(node->lchild);
}
}
}
// DFS (recursive)
void depthFirstSearchRecursive(Tree root)
{
if (root) {
cout << root->data;
if (root->lchild) {
depthFirstSearchRecursive(root->lchild);
}
if (root->rchild) {
depthFirstSearchRecursive(root->rchild);
}
}
}
Test
Define a class TreeBuilder to build a tree and destroy it.
class TreeBuilder
{
public:
TreeBuilder( Element data[] ) : index_( 0 ) {
constructTree( root_, data );
}
~TreeBuilder() {
destructTree( root_ );
}
Tree tree() const { return root_ ;}
private:
void constructTree(Tree &root, Element data[])
{
Element e = data[index_++];
if (e == '#') {
root = NULL;
}
else {
root = new Node (e);
constructTree(root->lchild, data);
constructTree(root->rchild, data);
}
}
void destructTree(Tree root) {
if( root != NULL ) {
delete root;
root = NULL;
}
}
private:
int index_;
Tree root_;
};
Test BFS and DFS.
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
...
int main(int argc, char** argv)
{
Element data[15] = { 'A', 'B', 'D', '#', '#', 'E', '#', '#', 'C', 'F','#', '#', 'G', '#', '#' };
TreeBuilder builder( data );
auto tree = builder.tree();
cout << endl;
cout << "BFS result:";
breadthFirstSearch(tree);
cout << endl;
cout << "DFS (iterative) result:";
depthFirstSearch(tree);
cout << endl;
cout << "DFS (recursive) result:";
depthFirstSearchRecursive(tree);
cout << endl;
return 0;
}
Build and run it.
g++ main.cpp --std=c++14
./a.out
Construct A
Construct B
Construct D
Construct E
Construct C
Construct F
Construct G
BFS result:ABCDEFG
DFS (iterative) result:ABDECFG
DFS (recursive) result:ABDECFG
Destruct A
Destruct B
Destruct D
Destruct E
Destruct C
Destruct F
Destruct G
Use valgrind to check if there is any memory leak.
valgrind --leak-check=yes ./a.out
...
==30852== HEAP SUMMARY:
==30852== in use at exit: 0 bytes in 0 blocks
==30852== total heap usage: 12 allocs, 12 frees, 74,024 bytes allocated
==30852==
==30852== All heap blocks were freed -- no leaks are possible
==30852==
==30852== For counts of detected and suppressed errors, rerun with: -v
==30852== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)