bst traversal code in data structure with c 2b 2b

Solutions on MaxInterview for bst traversal code in data structure with c 2b 2b by the best coders in the world

showing results for - "bst traversal code in data structure with c 2b 2b"
Annaelle
26 Mar 2020
1#include<iostream>
2using namespace std;
3struct node {
4   int data;
5   struct node *left;
6   struct node *right;
7};
8struct node *createNode(int val) {
9   struct node *temp = (struct node *)malloc(sizeof(struct node));
10   temp->data = val;
11   temp->left = temp->right = NULL;
12   return temp;
13}
14void inorder(struct node *root) {
15   if (root != NULL) {
16      inorder(root->left);
17      cout<<root->data<<" ";
18      inorder(root->right);
19   }
20}
21struct node* insertNode(struct node* node, int val) {
22   if (node == NULL) return createNode(val);
23   if (val < node->data)
24   node->left = insertNode(node->left, val);
25   else if (val > node->data)
26   node->right = insertNode(node->right, val);
27   return node;
28}
29int main() {
30   struct node *root = NULL;
31   root = insertNode(root, 4);
32   insertNode(root, 5);
33   insertNode(root, 2);
34   insertNode(root, 9);
35   insertNode(root, 1);
36   insertNode(root, 3);
37   cout<<"In-Order traversal of the Binary Search Tree is: ";
38   inorder(root);
39   return 0;
40}