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

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...
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.
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.
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree...
C++ tree program (do NOT use STRUCT, use classes)    Program 1 Implement a Binary tree using an array    Program 2 Implement a tree using linked list - pointer Binary Tree    Program 3 - Convert program 1 to a template
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]
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array...
C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template (include screenshots please of output)
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT