## Friday, November 21, 2014

### Graph Search or Traversal Algorithms

Graph is a set of connected vertices {V} and edges {E}. A graph may be connected, disconnected, weighted or non-weighted. In other terms, Graph could also be a  tree with cycles. Graph Search or Traversal can be done in two ways as explained below:

1. Depth First Search
In this type of search, we begin at a vertex Vi and traverse through all vertices from Vi unto Vx depth wise, until there is no adjacent vertex which is unvisited. Then we backup all the way upto an unvisited vertex Vy and continue. We continue until there are no unvisited vertices left. It represents Backtracking in Algorithmic Problem Solving.

2. Breadth First Search
In this type of search, we begin at a vertex Vi and traverse each vertex Vj that is reachable from there. Then we continue in the same way at every vertex reachable from Vj. It creates a queue of vertices visited from a given vertex and then deletes each of them if visited or after visiting them. The process is terminated once there is no non-visited vertex left. It represents Dynamic Programming in Algorithmic Problem Solving.

## Friday, November 14, 2014

### Binary Search Tree Traversal Algorithms

I hereby post my version of the inorder, preorder and postorder code in c++. Please provide your valuable feedback or additions to the code.

I am also providing a simple explanation here along with the download for an explanation of how to perform these simple tree traversals. Please provide your comments on the documentation as well.

In  a Binary Search Tree, every node has the left subtree with elements lesser than itself and the right subtree withe elements greater than (or equal) to itself. In a BST, the tree can be traversed using the following mechanisms.

Inorder Tree Traversal - C++
For a set of elements, that are inserted as mentioned below, the inorder tree traversal algorithms is as follows. It is recursive in nature and can start at root always.

1. Traverse the Left Subtree
2. Print the Node
3. Traverse the Right Subtree

Postorder Tree Traversal - C++
Postorder is a similar algorithm in nature - except that the traversal order is different.

1. Traverse the Left Subtree
2. Traverse the Right Subtree
3. Print the Node

Preorder Tree Traversal - C++
Whenever we want to traverse the node before the two subtrees, we use pre-order traversal.

1. Print the Node
2. Traverse the Left Subtree
3. Traverse the Right Subtree

We use the following insertion order for all algorithms above: 9 7 6 1 3 5 4 2 8

You can run the C++ code to see the output yourself. It is left as an exercise (or direct code reuse) for the user.