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.