• Breaking News

    Funny Coder

    Funny coder is an open source web for interested programmer. It is a programming environment.It's a way where you can code with fun.

    Wednesday, April 20, 2016

    Tree Traverse || Inorder, Preorder and Postorder in C



    Tree traverse full source code.
    There are six methods I've just given in this tree traverse problems ->

    1. Create a tree                                            (line no : 10-29)
    2. Display tree as inorder format                    (line no : 32-38)
    3. Display tree as preorder format                  (line no : 39-45)
    4. Display tree as postorder format                 (line no : 48-54)
    5. Find the maximum value of a tree               (line no : 67-74)
    6. Find the minimum value of a tree                (line no : 58-65)





    1. #include<stdio.h>
    2. #include<stdlib.h>
    3. typedef struct BSTnode{
    4. int data;
    5. struct BSTnode *left;
    6. struct BSTnode *right;
    7. };
    8. struct BSTnode *insert(struct BSTnode *root, int value){
    9. if(root == NULL){
    10. root = (struct BSTnode*)malloc(sizeof(struct BSTnode));
    11. root->left = root->right = NULL;
    12. root->data = value;
    13. return root;
    14. }else{
    15. if(value < root->data){
    16. root->left = insert(root->left, value);
    17. }else{
    18. if(value > root->data){
    19. root->right = insert(root->right, value);
    20. }else{
    21. }
    22. }
    23. return root;
    24. }
    25. };
    26. //making display inorder
    27. void inorder(struct BSTnode *root){
    28. if(root != NULL){
    29. inorder(root->left);
    30. printf("%d\t", root->data);
    31. inorder(root->right);
    32. }
    33. }
    34. //making display preorder
    35. void preorder(struct BSTnode *root){
    36. if(root != NULL){
    37. printf("%d\t", root->data);
    38. preorder(root->left);
    39. preorder(root->right);
    40. }
    41. }
    42. //making display postorder
    43. void postorder(struct BSTnode *root){
    44. if(root != NULL){
    45. postorder(root->left);
    46. postorder(root->right);
    47. printf("%d\t", root->data);
    48. }
    49. }
    50. int minValue(struct BSTnode* node) {
    51. struct BSTnode* current = node;
    52. while (current->left != NULL) {
    53. current = current->left;
    54. }
    55. return(current->data);
    56. }
    57. int maxValue(struct BSTnode* node) {
    58. struct BSTnode* current = node;
    59. while (current->right != NULL) {
    60. current = current->right;
    61. }
    62. return(current->data);
    63. }
    64. int main(){
    65. struct BSTnode *root = NULL;
    66. // root = (struct BSTnode*)malloc(sizeof(struct BSTnode));
    67. int n,i,x;
    68. printf("Enter the number of nodes : ");
    69. scanf("%d", &n);
    70. for(i = 1; i <=n; i++){
    71. printf("Enter the root : ");
    72. scanf("%d", &x);
    73. root = insert(root, x);
    74. }
    75. printf("After inorder tree is : \n");
    76. inorder(root);
    77. printf("\n----------------------------------\n");
    78. printf("After preorder tree is : \n");
    79. preorder(root);
    80. printf("\n----------------------------------\n");
    81. printf("After postorder tree is : \n");
    82. postorder(root);
    83. printf("\n----------------------------------\n");
    84. printf("%d", maxValue(root));
    85. printf("\n----------------------------------\n");
    86. printf("%d", minValue(root));
    87. printf("\n----------------------------------\n");
    88. }
    Store in Github TO see click the link:

    I've described full tree traverse so much easily here. You can try it now..And if you face any problem to solve this inform me or comment here.

    No comments:

    Post a Comment

    Fashion

    Beauty

    Travel