Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct node
- {
- int data;
- node *left = NULL;
- node *right = NULL;
- node(){
- }
- node(int _data ,node *_left ,node *_right){
- data = _data;
- left = _left;
- right = _right;
- }
- };
- struct tree
- {
- node *root=NULL;
- void insert(int x){
- if(root==NULL){
- root = new node(x ,NULL ,NULL);
- return;
- }
- node *cur = root;
- while(1){
- if(x <= cur->data){
- if(cur->left==NULL){
- cur->left = new node(x ,NULL ,NULL);
- return;
- }
- else cur = cur->left ;
- }
- else if(x > cur->data){
- if(cur->right==NULL){
- cur->right = new node(x ,NULL ,NULL);
- return;
- }
- else cur = cur->right ;
- }
- }
- }
- void inorder(struct node *cur){
- if(cur!=NULL){
- inorder(cur->left);
- printf("%d" ,cur->data);
- inorder(cur->right);
- }
- }
- };
- int main(){
- struct tree a;
- a.insert(41);
- a.insert(32);
- a.insert(94);
- a.inorder();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement