(97). 28.1. BINARY SEARCH TREE (INTRODUCTION, BUILD) - anishsingh90/Data_Structure_And_Algorithm_In_Cpp_github.io GitHub Wiki

#include <bits/stdc++.h> using namespace std;

struct Node{ int data; Node *left, *right;

Node(int val){
	data = val;
	left = NULL;
	right = NULL;
}

};

//insert binary search tree Node* insertBST(Node *root , int val){ if(root == NULL){ return new Node(val); }

if(val < root->data){
	root->left = insertBST(root->left, val);
}
else{
	//val > root->data
	root->right = insertBST(root->right, val);
}
return root;

}

//inorder function = to check builded binary search tree Right or Wrong void inorder(Node* root){ if(root == NULL){ return; } inorder(root->left); cout << root->data << " "; inorder(root->right); }

int main(){ Node* root = NULL; root = insertBST(root,5); insertBST(root,1); insertBST(root,3); insertBST(root,4); insertBST(root,2); insertBST(root,7);

//print inorder
cout << "Inorder Value: ";
inorder(root);
cout << endl;	

return 0;

}

/* OUTPUT: Inorder Value: 1 2 3 4 5 7 */

⚠️ **GitHub.com Fallback** ⚠️