// 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
- int main()
- {
- struct node *head = NULL;
- int choice = 0, num, flag = 0, key;
- int var = 0;
- struct node *result = (struct node *)malloc(sizeof(result));
- struct node *parent = (struct node *)malloc(sizeof(parent));
- do
- {
- printf("\nEnter your choice:\n1. Insert\n2. Perform DFS - Pre-order Traversal\n3. Perform DFS - In-order Traversal\n\
- 4. Perform DFS - Pos-order Traversal\n5. Search using recursion\n6. Search using non-recursion\n\
- 7. Find nth node follow preorder traversal\n8. Find nth node follow inorder traversal\n\
- 9. Find nth node follow postorder traversal\n10. Find largest node on tree\n11. Count number of leaf nodes\n\
- 12. Find number of nodes on tree\n13. Find Depth of tree\n14. Delete a node on tree\n15. Exit\nChoice: ");
- scanf("%d", &choice);
- switch(choice)
- {
- case 1:
- printf("Enter element to insert: ");
- scanf("%d", &num);
- generate(&head, num);
- break;
- case 2:
- DFS_Pre_Order(head);
- break;
- case 3:
- DFS_In_Order(head);
- break;
- case 4:
- DFS_Post_Order(head);
- break;
- // Search node in tree (where value of any node is matched or not with entered number) using recursion
- case 5:
- printf("Enter element to search: ");
- scanf("%d", &key);
- flag = search_recursion(head, key);
- if (flag)
- {
- printf("---------------> Key = [%d] found in tree <----------------\n",key);
- }
- else
- {
- printf("---------------> Key = [%d] not found <-------------------\n",key);
- }
- break;
- // Search node in tree (where value of any node is matched or not with entered number) using non-recursion
- case 6:
- printf("Enter element to search: ");
- scanf("%d", &key);
- result = search_nonrecurion(head, key,&parent);
- if (result != NULL)
- {
- printf("---------------> Key found in tree <----------------\n");
- }
- else
- {
- printf("---------------> Key not found <-------------------\n");
- }
- break;
- // Find n-th node by using preorder traversal
- case 7:
- printf("Enter nth node which you want to find: ");
- scanf("%d", &key);
- result = NULL;
- var = 0;
- nthpreorder(head,key,&result,&var);
- if(result != NULL)
- {
- printf("\nResult ---->[%d]\n", result->a);
- }
- else{
- printf("\n---------> There is no node which satisfies.<-----------------\n");
- }
- break;
- // Find n-th node by using inorder traversal
- case 8:
- printf("Enter nth node which you want to find: ");
- scanf("%d", &key);
- result = NULL;
- var = 0;
- nthinorder(head,key,&result,&var);
- if(result != NULL)
- {
- printf("\nResult ---->[%d]\n", result->a);
- }
- else{
- printf("\n---------> There is no node which satisfies.<-----------------\n");
- }
- break;
- // Find n-th node by using postorder traversal
- case 9:
- printf("Enter nth node which you want to find: ");
- scanf("%d", &key);
- result = NULL;
- var = 0;
- nthpostorder(head,key,&result,&var);
- if(result != NULL)
- {
- printf("\nResult ---->[%d]\n", result->a);
- }
- else{
- printf("\n---------> There is no node which satisfies.<-----------------\n");
- }
- break;
- // Find largest node on tree
- case 10:
- largest(head);
- break;
- // Find number of leaf nodes
- case 11:
- count = 0;
- count_leaf(head);
- printf("--------> Number of leaf nodes = %d <-----------\n", count);
- break;
- //Find number of nodes on tree
- case 12:
- count = count_num_nodes(head);
- printf("-------------> Number of nodes on tree = %d <-----------\n", count);
- count = 0;
- break;
- // Find depth of tree
- case 13:
- count = TreeDepth(head);
- printf("\n-----------> Depth of tree = %d <-------------\n",count);
- count = 0;
- break;
- // Delete a node on tree.
- case 14:
- printf("Enter value of node which you want to delete = ");
- scanf("%d", &key);
- result = search_nonrecurion(head,key,&parent);
- DeleteNode(&result,&parent);
- break;
- case 15:
- delete(&head);
- printf("Memory Cleared\nPROGRAM TERMINATED\n");
- break;
- default:
- printf("Not a valid input, try again\n");
- }
- } while (choice != 15);
- return 0;
- }
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