Translate

Chủ Nhật, 4 tháng 12, 2016

Binary search tree [Continue]

5.13 Find n-th Node in Pre-order Traversal 
// code 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void nthpreorder(struct node *root, int n, struct node **nthnode, int *var)
{
 
    if (root)
    {
        if ( ++(*var) == n)
        {
            printf("\n----------Found %dth node------------\n", n);
            *nthnode = root;
        }
        else
        {
            if(root->left != NULL)
            {
                nthpreorder(root->left, n , nthnode,var);           
            }
            if(root ->right != NULL)
            {
                nthpreorder(root->right, n , nthnode,var);
            }
        }
    }
    else
    {
        printf("There is no tree in this program.\n");
    }
}


5.14 Find n-th Node in In-order Traversal
// code 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void nthinorder(struct node *root, int n, struct node **nthnode,int *var)
{
    if (root)
    {
        if(root->left != NULL)
        {
            nthinorder(root->left, n , nthnode,var);           
        }
        if (++(*var) == n)
        {
            printf("\n Found %dth node\n", n);
            *nthnode = root;
        }
        if(root ->right != NULL)
        {
            nthinorder(root->right, n , nthnode,var);
        }
    }
    else
    {
        printf("There is no tree in this program.\n");
    }
}


5.15 Find n-th Node in Post-order Traversal
// code 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void nthpostorder(struct node *root, int n, struct node **nthnode,int *var)
{
    if (root)
    {
        if(root->left != NULL)
        {
            nthpostorder(root->left, n , nthnode, var);           
        }
        if(root ->right != NULL)
        {
            nthpostorder(root->right, n , nthnode, var);
        }
        if (++(*var) == n)
        {
            printf("\n Found %dth node\n", n);
            *nthnode = root;
        }
    }
    else
    {
        printf("There is no tree in this program.\n");
    }
}

5.16 The main function
// code 
?

  1. int main()  
  2. {  
  3.     struct node *head = NULL;  
  4.     int choice = 0, num, flag = 0, key;  
  5.     int var = 0;  
  6.     struct node *result = (struct node *)malloc(sizeof(result));  
  7.     struct node *parent = (struct node *)malloc(sizeof(parent));  
  8.     do  
  9.     {  
  10.         printf("\nEnter your choice:\n1. Insert\n2. Perform DFS - Pre-order Traversal\n3. Perform DFS - In-order Traversal\n\  
  11. 4. Perform DFS - Pos-order Traversal\n5. Search using recursion\n6. Search using non-recursion\n\  
  12. 7. Find nth node follow preorder traversal\n8. Find nth node follow inorder traversal\n\  
  13. 9. Find nth node follow postorder traversal\n10. Find largest node on tree\n11. Count number of leaf nodes\n\  
  14. 12. Find number of nodes on tree\n13. Find Depth of tree\n14. Delete a node on tree\n15. Exit\nChoice: ");  
  15.         scanf("%d", &choice);  
  16.         switch(choice)  
  17.         {  
  18.         case 1:   
  19.             printf("Enter element to insert: ");  
  20.             scanf("%d", &num);  
  21.             generate(&head, num);  
  22.             break;  
  23.         case 2:   
  24.             DFS_Pre_Order(head);  
  25.             break;  
  26.         case 3:  
  27.             DFS_In_Order(head);  
  28.             break;  
  29.         case 4:   
  30.             DFS_Post_Order(head);  
  31.             break;  
  32.    
  33.         // Search node in tree (where value of any node is matched or not with entered number) using recursion  
  34.         case 5:      
  35.             printf("Enter element to search: ");  
  36.             scanf("%d", &key);  
  37.             flag = search_recursion(head, key);  
  38.             if (flag)  
  39.             {  
  40.                 printf("---------------> Key = [%d] found in tree <----------------\n",key);  
  41.             }  
  42.             else  
  43.             {  
  44.                 printf("---------------> Key = [%d] not found <-------------------\n",key);  
  45.             }  
  46.             break;  
  47.    
  48.         // Search node in tree (where value of any node is matched or not with entered number) using non-recursion  
  49.         case 6:  
  50.             printf("Enter element to search: ");  
  51.             scanf("%d", &key);  
  52.             result = search_nonrecurion(head, key,&parent);  
  53.             if (result != NULL)  
  54.             {  
  55.                 printf("---------------> Key found in tree <----------------\n");  
  56.             }  
  57.             else  
  58.             {  
  59.                 printf("---------------> Key not found <-------------------\n");  
  60.             }  
  61.             break;  
  62.    
  63.         // Find n-th node by using preorder traversal  
  64.         case 7:  
  65.             printf("Enter nth node which you want to find: ");  
  66.             scanf("%d", &key);  
  67.             result = NULL;  
  68.             var = 0;  
  69.             nthpreorder(head,key,&result,&var);  
  70.             if(result != NULL)  
  71.             {  
  72.                 printf("\nResult ---->[%d]\n", result->a);  
  73.             }  
  74.             else{  
  75.                 printf("\n---------> There is no node which satisfies.<-----------------\n");  
  76.             }  
  77.             break;  
  78.    
  79.         // Find n-th node by using inorder traversal  
  80.         case 8:  
  81.             printf("Enter nth node which you want to find: ");  
  82.             scanf("%d", &key);  
  83.             result = NULL;  
  84.             var = 0;  
  85.             nthinorder(head,key,&result,&var);  
  86.             if(result != NULL)  
  87.             {  
  88.                 printf("\nResult ---->[%d]\n", result->a);  
  89.             }  
  90.             else{  
  91.                 printf("\n---------> There is no node which satisfies.<-----------------\n");  
  92.             }  
  93.             break;  
  94.    
  95.         // Find n-th node by using postorder traversal  
  96.         case 9:  
  97.             printf("Enter nth node which you want to find: ");  
  98.             scanf("%d", &key);  
  99.             result = NULL;  
  100.             var = 0;  
  101.             nthpostorder(head,key,&result,&var);  
  102.             if(result != NULL)  
  103.             {  
  104.                 printf("\nResult ---->[%d]\n", result->a);  
  105.             }  
  106.             else{  
  107.                 printf("\n---------> There is no node which satisfies.<-----------------\n");  
  108.             }  
  109.             break;  
  110.         // Find largest node on tree  
  111.         case 10:  
  112.             largest(head);  
  113.             break;  
  114.         // Find number of leaf nodes  
  115.         case 11:  
  116.             count = 0;  
  117.             count_leaf(head);  
  118.             printf("--------> Number of leaf nodes = %d <-----------\n", count);  
  119.             break;  
  120.         //Find number of nodes on tree  
  121.         case 12:  
  122.             count = count_num_nodes(head);  
  123.             printf("-------------> Number of nodes on tree = %d <-----------\n", count);  
  124.             count  = 0;  
  125.             break;  
  126.         // Find depth of tree  
  127.         case 13:  
  128.             count = TreeDepth(head);  
  129.             printf("\n-----------> Depth of tree = %d <-------------\n",count);  
  130.             count = 0;  
  131.             break;  
  132.         // Delete a node on tree.  
  133.         case 14:  
  134.             printf("Enter value of node which you want to delete = ");  
  135.             scanf("%d", &key);  
  136.             result = search_nonrecurion(head,key,&parent);  
  137.             DeleteNode(&result,&parent);  
  138.             break;  
  139.         case 15:  
  140.             delete(&head);  
  141.             printf("Memory Cleared\nPROGRAM TERMINATED\n");  
  142.             break;  
  143.         default:   
  144.             printf("Not a valid input, try again\n");  
  145.         }  
  146.     } while (choice != 15);  
  147.     return 0;  
  148. }  

Download full code here:

References
1: http://cslibrary.stanford.edu/110/BinaryTrees.html
2: https://www.cs.cmu.edu/~adamchik/15-121/lectures/Trees/trees.html
3: http://quiz.geeksforgeeks.org/binary-tree-set-3-types-of-binary-tree/
4: http://www.sanfoundry.com/c-programming-examples-on-trees/

Không có nhận xét nào:

Đăng nhận xét