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 node *tree
- {
- node *root;
- void insert(int x){
- if(root==NULL){
- root = new node(x ,NULL ,NULL);
- return;
- }
- node *cur = root;
- while(1){
- if(cur->data<x){
- cur = cur->right;
- }
- else if(cur->data>=x){
- cur = cur->left;
- }
- if(cur==NULL){
- cur = new node(x ,NULL ,NULL);
- return;
- }
- }
- }
- void print(){
- printf("%d %d %d" ,root->data ,root->left->data ,root->right->data);
- }
- };
- int main(){
- struct tree a;
- a.insert(41);
- a.insert(32);
- a.insert(94);
- a.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement