Question

In: Computer Science

Traversal tree program in c/c++

Traversal tree program in c/c++

Solutions

Expert Solution

Code:

#include <iostream>
using namespace std;
// struct Node
struct Node
{
int data;
struct Node*left;
struct Node*right;
//constructor
Node(int val)
{
this->data=val;
left=NULL;right=NULL;
}
};

//Requested function to traverse tree
void preorderTraverse(Node* A)
{
//base case
if(A==NULL)return;
//printing data of Node
cout<<A->data<<" ";
//if left node exist traverse left
if(A->left!=NULL)preorderTraverse(A->left);
//if right node exist traverse right
if(A->right!=NULL)preorderTraverse(A->right);
  
}

int main()
{
//sample tree to check working of function
struct Node *root=new Node(3);
root->left=new Node(7);
root->right=new Node(4);
root->left->left=new Node(9);
root->left->right=new Node(5);
root->right->left=new Node(11);
  
// 3
// / \
// 7 4
// / \ /
// 9 5 11
  
//calling traverse function
preorderTraverse(root);
}

Sample Output:

NOTE:  If you got any help from this article please UPVOTE and if you hace any query please do mention in comments...


Related Solutions

Traversal tree program
Traversal tree program
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...
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...
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
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.
Write a C++ or Java program name that conducts the BFS traversal of a graph and...
Write a C++ or Java program name that conducts the BFS traversal of a graph and displays city names in the range of hop(s) from a starting city Input format: This is a sample input from a user. 4 Monterey LA SF SD 6 Monterey LA LA SD SD Monterey SF Monterey SF LA SF SD Monterey 2 The first line (= 4 in the example) indicates that there are four vertices in the graph. The following four lines describe...
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
P1. Construct the tree with post-order traversal string = i b d a h g c...
P1. Construct the tree with post-order traversal string = i b d a h g c f, and in-order traversal string = b i a d f g h c. P2. Construct the tree with pre-order traversal string = 10, 20, 40, 50, 80, 60, 70, 90, and in-order traversal string = 40, 50, 20, 10, 80, 70, 90, 60.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT