Created
January 29, 2020 08:22
-
-
Save winhtut/4ca06a1e455e3e3c3fe0fef939773bb0 to your computer and use it in GitHub Desktop.
Tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
struct node { | |
int data; | |
struct node* left; | |
struct node* right; | |
}; | |
struct node* createNode(int val) { | |
struct node* temp = (struct node*)malloc(sizeof(struct node)); | |
temp->data = val; | |
temp->left = temp->right = NULL; | |
return temp; | |
} | |
void inorder(struct node* root) { | |
if (root != NULL) { | |
inorder(root->left); | |
cout << root->data << " "; | |
inorder(root->right); | |
} | |
} | |
struct node* insertNode(struct node* node, int val) { | |
if (node == NULL) return createNode(val); | |
if (val < node->data) | |
node->left = insertNode(node->left, val); | |
else if (val > node->data) | |
node->right = insertNode(node->right, val); | |
return node; | |
} | |
int main() { | |
struct node* root = NULL; | |
root = insertNode(root, 9); | |
insertNode(root, 10); | |
insertNode(root, 4); | |
insertNode(root, 3); | |
insertNode(root, 8); | |
insertNode(root, 0); | |
cout << "In-Order traversal of the Binary Search Tree is: "; | |
inorder(root); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment