SuitNdtie

Mr Ducks BST EXAM03

Mar 28th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3.  
  4. typedef struct node{
  5.     int data;
  6.     struct node* left;
  7.     struct node* right;
  8. }nodeT;
  9.  
  10. nodeT* createnode(int data){
  11.     nodeT* newnode = (nodeT*)malloc(sizeof(nodeT));
  12.     newnode->data = data;
  13.     newnode->left = NULL;
  14.     newnode->right = NULL;
  15.     return newnode;
  16. }
  17.  
  18. nodeT* insertnode(nodeT* root,int data)
  19. {
  20. //  nodeT* newnode = createnode
  21.     if(root == NULL)return createnode(data);
  22.    
  23.     if(data < root->data){
  24.         root->left = insertnode(root->left,data);
  25.     }
  26.     else if(data >= root->data){
  27.         root->right = insertnode(root->right,data);
  28.     }
  29.     return root;
  30. }
  31.  
  32. void postorder(nodeT* root){
  33.     if(root == NULL)return;
  34.    
  35.     postorder(root->left);
  36.     postorder(root->right);
  37.     printf("%d ",root->data);
  38. }
  39.  
  40. int main()
  41. {
  42.     int n;
  43.     scanf("%d",&n);
  44.     nodeT* root = NULL;
  45.     for(int i=0;i<n;i++){
  46.         int x;
  47.         scanf("%d",&x);
  48.         root = insertnode(root,x);
  49.     }
  50.     postorder(root);
  51.     return 0;
  52.    
  53. }
Add Comment
Please, Sign In to add comment