Showing posts with label trees. Show all posts
Showing posts with label trees. Show all posts

Sunday, 13 October 2013

Expression Trees

Step 1 : Convert the given expression to postfix form
Step 2 : Read one symbol at a time from the postfix expression
Step 3 : Check whether the symbol is an operator or operand.
(i)If it is an operand,create a one node tree and push a pointer on the stack.
(ii)If it is an operator,pop two pointers from the stack namely T1 and T2 and form a new tree with Root as operator and T2 as left child and T1 as right child.Push the pointer to this new tree on to the stack.
Step 4 : Repeat the Steps till the end of the expression.

Depth First Search

Step1 : Choose any node in the graph,designate it as search node and mark it as visited.
Step2 : Using Adjacency matrix of the graph,find a node adjacent nodes to the search node that has been not yet visited and designate that node as the search node and mark it as visited.
Step3 : Repeat Step2 using the new search node.If no nodes satisfying Step2 is found,return to the previous search node and continue from there.
Step4 : When a return to the previous search node in Step3 is impossible,the search from the originally chosen node is complete.
Step5 : If the graph still contains unvisited nodes,choose any node that has not yet been visited and repeat Step 1 to 4.

Breadth First Search

Step1 : Choose any node in the graph,designate it as search node and mark it as visited.
Step2 : Using Adjacency matrix of the graph,find all unvisited adjacent nodes to the search node and enqueue them on to a Queue
Step3 : Then the node is dequeued from the queue.Mark the node as visited and designate it as the source node.
Step4 : Repeat step2 and step3 using the new search node.
Step5 : Repeat these process until the queue which keeps track of the adjacent nodes is empty.

Saturday, 12 October 2013

Topological Sort (only for directed Acyclic Graph)

Step1 : Find the indegree of all the vertices of the graph.
Step2 : Place the vertices whose indegree is „0‟ on an empty queue.
Step3 : Dequeue the vertex V and decrement the indegrees of all its adjacent vertices.
Step4 : Enqueue the vertex whose indegree has fallen to „0‟.
Step5 : Repeat from step3 untill the queue becomes empty.
Step6 : The topological sorting order is the order in which the vertices are dequeued.