Question

In: Computer Science

Traversal tree program

Traversal tree program

Solutions

Expert Solution

Traversing a tree means visiting each and every node of the tree. To add all the values in the tree or to find the largest one we can prefer this.

There are three types of traversal depending on the order in which we do this.

1. Inorder traversal

1. First, visit all the nodes in the left subtree

2. Visit the root node

3. Visit all the nodes in the right subtree

2. Preorder traversal

1. First, Visit the root node

2. Visit all the nodes in the left subtree

3. Visit all the nodes in the right subtree

3. Postorder traversal

1.First, Visit all the nodes in the left subtree

2. Visit all the nodes in the right subtree

3. Visit the root node

Traversal tree program:

Node.java

package com;

public class Node
{
int key;
Node left, right;
  
public Node(int n)
{
key = n;
left = right = null;
}
}

TraversalTree.java

package com;


public class TraversalTree
{

Node root;
  
TraversalTree()
{
root = null;
}
  
void DisplayInorder(Node node)
{
if (node == null)
return;
  
  
DisplayInorder(node.left);

System.out.print(node.key + " ");
  
DisplayInorder(node.right);
}

void DisplayPostorder(Node node)
{
if (node == null)
return;
  
DisplayPostorder(node.left);
  
DisplayPostorder(node.right);
  
System.out.print(node.key + " ");
}
  
  
void DisplayPreorder(Node node)
{
if (node == null)
return;
  
System.out.print(node.key + " ");
DisplayPreorder(node.left);
DisplayPreorder(node.right);
}
  
void DisplayInorder() { DisplayInorder(root); }
void DisplayPreorder() { DisplayPreorder(root); }
void DisplayPostorder() { DisplayPostorder(root); }

// main method
public static void main(String[] args)
{
   TraversalTree tree = new TraversalTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
  
System.out.println("\nInorder traversal is : ");
tree.DisplayInorder();
  
System.out.println("\nPreorder traversal is: ");
tree.DisplayPreorder();
  
  
System.out.println("\nPostorder traversal is: ");
tree.DisplayPostorder();
}
}

Output:

Inorder traversal is :
4 2 5 1 3
Preorder traversal is:
1 2 4 5 3
Postorder traversal is:
4 5 2 3 1


Related Solutions

Traversal tree program in c/c++
Traversal tree program in c/c++
Modifying the tree traversal C program shown below to do reversed tree traversal as defined below....
Modifying the tree traversal C program shown below to do reversed tree traversal as defined below. Reversed PreOrder: Root, Right, Left. Reversed InOrder:     Right, Root, Left. Reversed PostOrder: Right, Left, Root. The tree is: Root = 1, Left(1) = -2, Right(1) = -3; Left(-2) = 4, Right(-2) = 5; Left(-3) = 6, Right(-3)= 7; Left(5) = -8, Right(5)= -9; Left(7) = 10, Right(7) = 11; Left(11) = -12, Right(11) = -13; Left(-13) = 14. Your program should output the printed...
Write a C program that demonstrate tree traversal. A. first is to ask the user to...
Write a C program that demonstrate tree traversal. A. first is to ask the user to enter data B. ask how many nodes THIS IS JUST A SAMPLE. PLEASE ANSWER IT IN COMPLETE CODE. See for the sample output. Sample Output: How many node do you have in your tree : 5 Enter root: 20 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 10 if you want to add to...
Exercise 1: (Divide and Conquer: Binary tree traversal) Write pseudocode for one of the classic traversal...
Exercise 1: (Divide and Conquer: Binary tree traversal) Write pseudocode for one of the classic traversal algorithms (preorder, inorder, and postorder) for binary trees. Assuming that your algorithm is recursive, find the number of recursive calls made.
C program that demonstrate tree traversal. see for the sample output. Sample Output: How many node...
C program that demonstrate tree traversal. see for the sample output. Sample Output: How many node do you have in your tree : 5 Enter root: 20 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 10 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 8 if you want to add to the left press 0 otherwise press 1 answer: 0...
Please ready a program should output Inorder Tree Traversal without recursion and without stack!, output in...
Please ready a program should output Inorder Tree Traversal without recursion and without stack!, output in ALGOL W programming only. Thumbs down for wrong answers Make a program to perform Heap Sort, must run in Alice programming only. Only correct answers needed should be in given language
Use C programming to Implement the breadth-first tree traversal. The tasks are: a) Build the tree....
Use C programming to Implement the breadth-first tree traversal. The tasks are: a) Build the tree. b) Perform breadth first traversal on the tree that you have built at step a). Note: "Please comment the program"
Tree Reconstruction - Pre+In => Post (C++) Give you pre-order traversal and in-order traversal, please output...
Tree Reconstruction - Pre+In => Post (C++) Give you pre-order traversal and in-order traversal, please output post-order traversal. Input Two lines. Pre-order traversal and in-order traversal. Output Post-order traversal. Sample Input GDAFEMHZ ADEFGHMZ Sample Output AEFDHZMG
1. Create the binary tree that has postorder traversal of { b d a h g...
1. Create the binary tree that has postorder traversal of { b d a h g c f,} and has the inorder traversal of {b i a d f g h c} 2. Create the binary tree that has the preorder traversal of {10, 20, 40, 50, 80, 60, 70, 90} and the inorder traversal of { 40, 50, 20, 10, 80, 70, 90, 60}
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal...
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal of the tree. 50, 60, 25, 40, 30, 70, 35, 10, 55, 65, 5 Write an algorithm to delete a node in Singly Linked List                            [12 Write an algorithm of Binary Search                                                              [10] Write a program in ā€˜C’ to generate Fibonacci series using recursion            [8]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT