Question

In: Computer Science

Respond to the following in a minimum of 175 words: Explain two different ways to traverse...

Respond to the following in a minimum of 175 words:

Explain two different ways to traverse a tree and how beneficial or not each tree traversing algorithm could be to an organization.

Solutions

Expert Solution

Ways to traverse a tree:

There are two methods to traverse a tree. The tree traversal means visiting each and every node of a tree.The two methods are:

  • Depth first
  • Breadth first

Depth first traversal:

The depth first traversal methods has three variants that are associated with it.They are as shown below.

  • Inorder traversal
  • Preorder traversal
  • postorder traversal

These three variants can be briefly explained as shown below.

Inorder traversal:

The inorder traversal is the method in which the nodes of the tree are visited based on the key values in ascending order.This method of traversal is called as inorder traversal.

When we need to create the sorted list of node values of tree, we can use this inorder traversal algorithm to create the ascending order of values.

The sample algorithm that can be used to traverse a tree using inorder traversal method is shown below.

void inorder_traversal(node root)

{

if(root!=NULL)

{

inorder_traversal(root.left);

root.displayNode();

inorder_traversal(root.right);

}

}

Postorder traversal:

The post order traversal is the traversal method in which all the nodes of all subtrees are processed first, then the root node.

This is called as postorder traversal.

The sample algorithm that can be used to traverse a tree using postorder traversal method is shown below.

void postorder_traversal(node root)

{

if(root!=NULL)

{

postorder_traversal(root.left);

postorder_traversal(root.right);

root.displayNode();

}

}

Preorder traversal:

The preorder traversal is the method in which all the nodes of a tree are processed by processing the root node first, then the nodes of all the subtrees recursively.

This is called as preorder traversal.

This is exactly opposite to the postorder traversal.

The sample algorithm that can be used to traverse a tree using preorder traversal method is shown below.

void preorder_traversal(node root)

{

if(root!=NULL)

{

root.displayNode();

postorder_traversal(root.left);

postorder_traversal(root.right);  

}

}

These are the three variants of depth first traversal methods.

Breadth first traversal:

This method of traversal is also known as level order traversal.

In this method of traversal, we first access the root node,then the children of root node from left to right fashion.

This is called as breadth first traversal of a tree.


Related Solutions

Respond to the following in a minimum of 175 words: Discuss the ways in which business...
Respond to the following in a minimum of 175 words: Discuss the ways in which business to business buying behavior differs from consumer purchasing. Consider in your discussion the purchasing decision as well and ways to measure customer satisfaction and loyalty.
Respond to the following in a minimum of 175 words: Explain the real world applications of...
Respond to the following in a minimum of 175 words: Explain the real world applications of Statistics and provide a detailed example.
Respond to the following in a minimum of 175 words: Explain the tax implications of compensation...
Respond to the following in a minimum of 175 words: Explain the tax implications of compensation in the form of salary and wages from the perspectives of the employee and employer.
Due Thursday Respond to the following in a minimum of 175 words: Explain the purpose of...
Due Thursday Respond to the following in a minimum of 175 words: Explain the purpose of an HTML form. Research alternatives to HTML forms. Post the ones you believe are best, and explain why.
please respond to the following in a minimum of 175 words: Explain what happens to the...
please respond to the following in a minimum of 175 words: Explain what happens to the interest rate if the money supply increases or decreases and the money demand remains unchanged. Explain what happens to the interest rate if the money demand increases or decreases and the money supply remains unchanged.
Respond to the following in a minimum of 175 words: What is the purpose of an...
Respond to the following in a minimum of 175 words: What is the purpose of an income statement, and who is the audience for this document? What components do income statements typically contain? Why?
Respond to the following in a minimum of 175 words: What is the purpose of a...
Respond to the following in a minimum of 175 words: What is the purpose of a Comprehensive Annual Financial Report, or CAFR? What standards must a CAFR comply with? What statements comprise a CAFR?
Respond to the following in a minimum of 175 words: In the early weeks of this...
Respond to the following in a minimum of 175 words: In the early weeks of this course, you learned some valuable skills for communicating within and improving the performance of teams you might be working with. This week, we will explore two crucial aspects of being on a team. We will learn about leadership and what it takes to be a good leader. We also examine conflict management and learn some ways to navigate through the inevitability of encountering conflict....
Respond to the following in a minimum of 175 words: The flow of a program is...
Respond to the following in a minimum of 175 words: The flow of a program is controlled by different structures. The three basic control structures in computer programs are sequence structures, decision structures (also called selection structures), and repetition structures (also called iteration structures). Discuss the differences between decision structures and repetition structures used in algorithms. Provide examples of when you might use each.
Respond to the following in a minimum of 175 words: Explain why responsive design is important...
Respond to the following in a minimum of 175 words: Explain why responsive design is important in today's landscape. What do websites risk if they decide not to address responsive design?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT