Advertisement
apl-mhd

bst

Apr 18th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. using namespace std;
  5. struct tree{
  6.     char data;
  7.     struct tree *left;
  8.     struct tree  *right;
  9.  
  10. };
  11.  
  12. typedef struct tree tree;
  13.  
  14.  
  15.  
  16. void inorder(tree *root){
  17.     if(root->left != NULL)
  18.             inorder(root->left);
  19.  
  20.     printf("%c ", root->data);
  21.     if(root->right !=NULL)
  22.         inorder(root->right);
  23.  
  24.  
  25.  
  26. }
  27.  
  28. tree *createTree(tree *root){
  29.  
  30.     char node;
  31.     printf("%c has any left child?\n", root->data);
  32.  
  33.     cin>>node;
  34.     if(node == 'y'){
  35.  
  36.         root->left = new node();
  37.         printf("Enter left dat %c", root->data);
  38.         cin>>root->left->data;
  39.         createTree(root->left);
  40.     }
  41.     else{
  42.  
  43.         root->left = NULL;
  44.     }
  45.  
  46.  
  47.     printf("%c has any right child?\n", root->data);
  48.  
  49.     cin>>node;
  50.     if(node == 'y'){
  51.  
  52.          root->right = new node();
  53.         printf("Enter left dat %c", root->data);
  54.         cin>>root->right->data;
  55.         createTree(root->right);
  56.     }
  57.     else{
  58.  
  59.         root->right = NULL;
  60.     }
  61.  
  62.  
  63.  
  64. return root;
  65. }
  66.  
  67.  
  68.  
  69. int main()
  70. {
  71.     tree  *root;
  72.    root = new node();
  73.    root->data = 'a';
  74.    createTree(root);
  75.    inorder(root);
  76.  
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement