Advertisement
apl-mhd

binaryTree

Apr 21st, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 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. tree *root;
  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.  
  32. if(root == NULL){
  33. root = new tree();
  34. cout<<"enter root node\n";
  35. cin>>root->data;
  36. root->left = NULL;
  37. root->right = NULL;
  38. return createTree(root);
  39. }
  40. printf("%c has any left child?\n", root->data);
  41.  
  42. cin>>node;
  43. if(node == 'y'){
  44.  
  45. root->left = new tree();
  46. printf("Enter left data %c:", root->data);
  47. cin>>root->left->data;
  48. createTree(root->left);
  49. }
  50. else{
  51.  
  52. root->left = NULL;
  53. }
  54.  
  55.  
  56. printf("%c has any right child?\n", root->data);
  57.  
  58. cin>>node;
  59. if(node == 'y'){
  60.  
  61. root->right = new tree();
  62. printf("Enter right dat %c:", root->data);
  63. cin>>root->right->data;
  64. createTree(root->right);
  65. }
  66. else{
  67.  
  68. root->right = NULL;
  69. }
  70.  
  71. return root;
  72. }
  73.  
  74.  
  75.  
  76. int main()
  77. {
  78.  
  79. //root = NULL;
  80. //root = new tree();
  81. // root->data = 'a';
  82. createTree(root);
  83. inorder(root);
  84.  
  85.  
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement