title
5.6 Binary Tree traversal | Preorder, Inorder, Postorder | Data Structures Tutorials
description
In this lecture, I have described preorder, inorder and postorder algorithms for binary tree and I have written a C program for binary tree traversal.
DSA Full Course: https: https://www.youtube.com/playlist?list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU
**********************************************
See Complete Playlists:
C Programming Course: https://www.youtube.com/playlist?list=PLdo5W4Nhv31a8UcMN9-35ghv8qyFWD9_S
C++ Programming: https://www.youtube.com/playlist?list=PLdo5W4Nhv31YU5Wx1dopka58teWP9aCee
Python Full Course: https://www.youtube.com/playlist?list=PLdo5W4Nhv31bZSiqiOL5ta39vSnBxpOPT
Printing Pattern in C: https://www.youtube.com/playlist?list=PLdo5W4Nhv31Yu1igxTE2x0aeShbKtVcCy
DAA Course: https://www.youtube.com/playlist?list=PLdo5W4Nhv31ZTn2P9vF02bkb3SC8uiUUn
Placement Series: https://www.youtube.com/playlist?list=PLdo5W4Nhv31YvlDpJhvOYbM9Ap8UypgEy
Dynamic Programming: https://www.youtube.com/playlist?list=PLdo5W4Nhv31aBrJE1WS4MR9LRfbmZrAQu
Operating Systems: //www.youtube.com/playlist?list=PLdo5W4Nhv31a5ucW_S1K3-x6ztBRD-PNa
DBMS: https://www.youtube.com/playlist?list=PLdo5W4Nhv31b33kF46f9aFjoJPOkdlsRc
**********************************************
Connect & Contact Me:
Facebook: https://www.facebook.com/Jennys-Lectures-CSIT-Netjrf-316814368950701/
Quora: https://www.quora.com/profile/Jayanti-Khatri-Lamba
Instagram: https://www.instagram.com/jayantikhatrilamba/
detail
{'title': '5.6 Binary Tree traversal | Preorder, Inorder, Postorder | Data Structures Tutorials', 'heatmap': [{'end': 1201.46, 'start': 1174.025, 'weight': 0.735}], 'summary': 'Covers the implementation of inorder, preorder, and postorder traversal of a binary tree using a recursive approach, with examples provided for better understanding. it also discusses stack memory allocation during function calls and emphasizes the recursive approach for binary tree traversals, including specific tree examples and printed outputs.', 'chapters': [{'end': 70.307, 'segs': [{'end': 48.971, 'src': 'embed', 'start': 0.349, 'weight': 0, 'content': [{'end': 3.252, 'text': 'So the topic is binary tree traversals right?', 'start': 0.349, 'duration': 2.903}, {'end': 7.515, 'text': 'In the previous video we have already discussed how to create a binary tree.', 'start': 3.892, 'duration': 3.623}, {'end': 10.077, 'text': 'that is, binary tree implementation right?', 'start': 7.515, 'duration': 2.562}, {'end': 12.68, 'text': 'For that, first of all, you check out that video.', 'start': 10.538, 'duration': 2.142}, {'end': 14.881, 'text': "I'll provide you the link of that video in this i button.", 'start': 12.68, 'duration': 2.201}, {'end': 16.283, 'text': 'then come to this video right?', 'start': 14.881, 'duration': 1.402}, {'end': 22.067, 'text': 'I assume that you have created this tree by calling the create function right?', 'start': 16.783, 'duration': 5.284}, {'end': 27.792, 'text': 'Now we will find out the inorder traversal, preorder traversal as well as postorder traversal of this tree.', 'start': 22.508, 'duration': 5.284}, {'end': 37.466, 'text': 'we are going to write down the code for all the three traversals right and we are going to use the recursive approach here in this case.', 'start': 29.213, 'duration': 8.253}, {'end': 46.909, 'text': 'fine, now, see, have already discussed how to find out all the tree traversals of a tree in that video.', 'start': 37.466, 'duration': 9.443}, {'end': 48.971, 'text': "i haven't discussed the coding part.", 'start': 46.909, 'duration': 2.062}], 'summary': 'In this video, we will cover the inorder, preorder, and postorder traversals of a binary tree using a recursive approach.', 'duration': 48.622, 'max_score': 0.349, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/e_Wv_pH4Se8/pics/e_Wv_pH4Se8349.jpg'}], 'start': 0.349, 'title': 'Binary tree traversal', 'summary': 'Covers the implementation of inorder, preorder, and postorder traversal of a binary tree using a recursive approach, with references to previous videos and examples provided for better understanding.', 'chapters': [{'end': 70.307, 'start': 0.349, 'title': 'Binary tree traversal', 'summary': 'Covers the implementation of inorder, preorder, and postorder traversal of a binary tree using a recursive approach, with references to previous videos and examples provided for better understanding.', 'duration': 69.958, 'highlights': ['The video discusses the implementation of inorder, preorder, and postorder traversal of a binary tree using a recursive approach, with references to previous videos and examples provided for better understanding.', 'Mention of previous video explaining binary tree creation and a link provided for reference.', 'Explanations and coding of the tree traversals are done, emphasizing the use of a recursive approach for implementation.']}], 'duration': 69.958, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/e_Wv_pH4Se8/pics/e_Wv_pH4Se8349.jpg', 'highlights': ['The video discusses the implementation of inorder, preorder, and postorder traversal of a binary tree using a recursive approach, with references to previous videos and examples provided for better understanding.', 'Explanations and coding of the tree traversals are done, emphasizing the use of a recursive approach for implementation.', 'Mention of previous video explaining binary tree creation and a link provided for reference.']}, {'end': 471.845, 'segs': [{'end': 95.645, 'src': 'embed', 'start': 70.307, 'weight': 2, 'content': [{'end': 81.515, 'text': 'so now here the inorder traversal of this tree would be see, inorder traversal is what first of all left, then root, then right, right,', 'start': 70.307, 'duration': 11.208}, {'end': 82.796, 'text': 'this thing i have already discussed.', 'start': 81.515, 'duration': 1.281}, {'end': 84.462, 'text': 'now in the tree.', 'start': 83.502, 'duration': 0.96}, {'end': 88.343, 'text': 'first of all, obviously we have only the root.', 'start': 84.462, 'duration': 3.881}, {'end': 92.484, 'text': 'sorry, the pointer to this root node, that is address of the root node, that is 100.', 'start': 88.343, 'duration': 4.141}, {'end': 95.645, 'text': 'that we have already discussed in the previous video right.', 'start': 92.484, 'duration': 3.161}], 'summary': "Inorder traversal of the tree starts with left, then root, then right. root node's address is 100.", 'duration': 25.338, 'max_score': 70.307, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/e_Wv_pH4Se8/pics/e_Wv_pH4Se870307.jpg'}, {'end': 182.122, 'src': 'embed', 'start': 150.578, 'weight': 1, 'content': [{'end': 152.34, 'text': 'root would be first pre means.', 'start': 150.578, 'duration': 1.762}, {'end': 159.342, 'text': 'So in this case root would be first, then left and then right.', 'start': 154.96, 'duration': 4.382}, {'end': 175.549, 'text': 'So, pre-order traversal of this tree would be 4 1 and post-order traversal would be in post-order we will write left then right and then root.', 'start': 159.722, 'duration': 15.827}, {'end': 182.122, 'text': 'so now I am going to write down the code, how to write down the code for these traversal using recursive approach.', 'start': 176.42, 'duration': 5.702}], 'summary': 'The summary discusses pre-order and post-order tree traversals using a recursive approach.', 'duration': 31.544, 'max_score': 150.578, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/e_Wv_pH4Se8/pics/e_Wv_pH4Se8150578.jpg'}, {'end': 268.61, 'src': 'embed', 'start': 240.143, 'weight': 0, 'content': [{'end': 242.505, 'text': 'fine, means that address of that root node.', 'start': 240.143, 'duration': 2.362}, {'end': 244.787, 'text': 'that is 100 i am going to pass.', 'start': 242.505, 'duration': 2.282}, {'end': 247.67, 'text': 'now you need to define this function.', 'start': 244.787, 'duration': 2.883}, {'end': 249.532, 'text': 'so what is the definition of this function?', 'start': 247.67, 'duration': 1.862}, {'end': 255.001, 'text': 'see. So here what you will write, see, it is going to pass address of the root node.', 'start': 249.532, 'duration': 5.469}, {'end': 257.942, 'text': 'that is, it is going to pass a pointer to root node.', 'start': 255.001, 'duration': 2.941}, {'end': 265.047, 'text': 'So here also you need a pointer right and the pointer the type would be always struct node.', 'start': 257.963, 'duration': 7.084}, {'end': 268.61, 'text': "asterisk, and here also i'm taking the name root.", 'start': 266.289, 'duration': 2.321}], 'summary': 'Defining a function to pass the address of the root node as a pointer to a struct node.', 'duration': 28.467, 'max_score': 240.143, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/e_Wv_pH4Se8/pics/e_Wv_pH4Se8240143.jpg'}], 'start': 70.307, 'title': 'Binary tree traversal', 'summary': 'Discusses the inorder, preorder, and postorder traversals of a binary tree, emphasizing recursive approach, with example tree having root at address 100 and nodes 5, 7, and 8 resulting in inorder traversal 7, 5, 8, preorder traversal 4, 1, and postorder traversal 5, 8, 7, and explains pre-order traversal of a binary tree in c programming.', 'chapters': [{'end': 182.122, 'start': 70.307, 'title': 'Binary tree traversal', 'summary': 'Discusses the inorder, preorder, and postorder traversals of a binary tree, with an example tree having root at address 100 and nodes 5, 7, and 8, resulting in inorder traversal 7, 5, 8, preorder traversal 4, 1, and postorder traversal 5, 8, 7, using recursive approach.', 'duration': 111.815, 'highlights': ['The inorder traversal of the binary tree with root at address 100 and nodes 5, 7, and 8 results in the sequence 7, 5, 8.', 'The preorder traversal of the binary tree with root at address 100 and nodes 5, 7, and 8 results in the sequence 4, 1.', 'The postorder traversal of the binary tree with root at address 100 and nodes 5, 7, and 8 results in the sequence 5, 8, 7.']}, {'end': 287.934, 'start': 182.122, 'title': 'Pre-order traversal of binary tree', 'summary': 'Explains the process of finding the pre-order traversal of a binary tree in c programming, emphasizing the passing of root pointer and the use of pointers in the function definition.', 'duration': 105.812, 'highlights': ['The chapter emphasizes the process of finding the pre-order traversal of a binary tree in C programming.', 'It explains the importance of passing the root pointer and using pointers in the function definition.', 'The speaker discusses the specific scenario of having the address of the root node (100) and passing it to the pre-order function.', 'The detailed explanation includes the use of pointers, the function definition, and the specific scenario of passing the address (100) as the root pointer.']}, {'end': 471.845, 'start': 287.934, 'title': 'Pre-order tree traversal', 'summary': 'Explains the pre-order traversal of a tree, using recursive calls and a termination condition, to print the root, left, and right child nodes, with a specific example of 4, 5, and 7.', 'duration': 183.911, 'highlights': ['The chapter explains the recursive process of pre-order traversal for a tree, highlighting the printing of the root, left, and right child nodes, with a specific example of 4, 5, and 7.', "It emphasizes the necessity of a termination condition in recursive approaches, with the specific condition of 'if root equals null or 0, then return', to prevent infinite recursion.", 'The detailed explanation of accessing and printing the left and right child nodes through recursive calls and subtree traversal provides a comprehensive understanding of the pre-order traversal process.']}], 'duration': 401.538, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/e_Wv_pH4Se8/pics/e_Wv_pH4Se870307.jpg', 'highlights': ['The detailed explanation includes the use of pointers, the function definition, and the specific scenario of passing the address (100) as the root pointer.', 'The chapter explains the recursive process of pre-order traversal for a tree, highlighting the printing of the root, left, and right child nodes, with a specific example of 4, 5, and 7.', 'The inorder traversal of the binary tree with root at address 100 and nodes 5, 7, and 8 results in the sequence 7, 5, 8.', 'The postorder traversal of the binary tree with root at address 100 and nodes 5, 7, and 8 results in the sequence 5, 8, 7.', 'The preorder traversal of the binary tree with root at address 100 and nodes 5, 7, and 8 results in the sequence 4, 1.']}, {'end': 1212.11, 'segs': [{'end': 543.551, 'src': 'embed', 'start': 471.845, 'weight': 4, 'content': [{'end': 480.933, 'text': 'so during the execution, the control first of all will go to the word, this main function right here this we have created a root pointer.', 'start': 471.845, 'duration': 9.088}, {'end': 482.653, 'text': 'we have called the create function.', 'start': 480.933, 'duration': 1.72}, {'end': 486.315, 'text': 'we have created this tree and in root pointer we have now 100.', 'start': 482.653, 'duration': 3.662}, {'end': 489.157, 'text': 'that is address of the first node i am considering.', 'start': 486.315, 'duration': 2.842}, {'end': 490.997, 'text': 'address of the first node is 100.', 'start': 489.157, 'duration': 1.84}, {'end': 494.012, 'text': 'right now, Printf preorder is.', 'start': 490.997, 'duration': 3.015}, {'end': 495.513, 'text': 'Now control will go to this line.', 'start': 494.192, 'duration': 1.321}, {'end': 496.734, 'text': 'Now this is what a function call.', 'start': 495.553, 'duration': 1.181}, {'end': 500.136, 'text': 'When a function is going to call from this main function.', 'start': 497.374, 'duration': 2.762}, {'end': 504.119, 'text': 'Then this stack would be created.', 'start': 500.696, 'duration': 3.423}, {'end': 507.001, 'text': 'Means memory would be allocated to this function in stack.', 'start': 504.139, 'duration': 2.862}, {'end': 508.281, 'text': 'From the stack memory.', 'start': 507.061, 'duration': 1.22}, {'end': 511.223, 'text': 'Right The memory is to be allocated for this function.', 'start': 508.742, 'duration': 2.481}, {'end': 515.885, 'text': 'Means for all the variables of local variables of this function.', 'start': 511.263, 'duration': 4.622}, {'end': 517.307, 'text': 'Right Now see.', 'start': 516.246, 'duration': 1.061}, {'end': 519.448, 'text': 'Now control will go to here.', 'start': 517.788, 'duration': 1.66}, {'end': 521.83, 'text': 'Now suppose in the stack.', 'start': 520.269, 'duration': 1.561}, {'end': 524.733, 'text': 'See here root is what 100.', 'start': 522.231, 'duration': 2.502}, {'end': 525.655, 'text': 'root value is 100.', 'start': 524.733, 'duration': 0.922}, {'end': 527.216, 'text': 'that means 100 would be passed.', 'start': 525.655, 'duration': 1.561}, {'end': 528.678, 'text': 'so here 100 would be passed.', 'start': 527.216, 'duration': 1.462}, {'end': 535.626, 'text': 'in this case, right now this stack memory would be allocated and here all these statements would be there.', 'start': 528.678, 'duration': 6.948}, {'end': 540.85, 'text': 'right, this working of this recursion also, we have discussed in the previous video.', 'start': 536.348, 'duration': 4.502}, {'end': 543.551, 'text': 'now, suppose I am numbering these statements.', 'start': 540.85, 'duration': 2.701}], 'summary': 'During execution, a tree with root value 100 is created, and stack memory is allocated for function calls.', 'duration': 71.706, 'max_score': 471.845, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/e_Wv_pH4Se8/pics/e_Wv_pH4Se8471845.jpg'}, {'end': 637.243, 'src': 'embed', 'start': 589.625, 'weight': 2, 'content': [{'end': 592.868, 'text': 'Now, next statement is fourth statement.', 'start': 589.625, 'duration': 3.243}, {'end': 597.15, 'text': 'Here I have preorder means again the function call.', 'start': 593.268, 'duration': 3.882}, {'end': 601.173, 'text': 'It means it is going to again call this function.', 'start': 598.351, 'duration': 2.822}, {'end': 610, 'text': 'it means here and in the previous video also, i have told you, different copy of all the local variables would be created for each function call.', 'start': 602.176, 'duration': 7.824}, {'end': 613.341, 'text': 'so for this function, call also all these.', 'start': 610, 'duration': 3.341}, {'end': 621.265, 'text': 'if there is any local variable, then that copy would be created for that variable copy would be created in this stack right now.', 'start': 613.341, 'duration': 7.924}, {'end': 626.948, 'text': 'here also, i have all these statement one, two, three, four, five and four and five.', 'start': 621.265, 'duration': 5.683}, {'end': 634.6, 'text': 'now here, see, in this case root value is what Here in preorder, what should be passed? Root of left.', 'start': 626.948, 'duration': 7.652}, {'end': 637.243, 'text': 'Means from here root of left would be passed.', 'start': 635.001, 'duration': 2.242}], 'summary': 'In each function call, a copy of all local variables is created, leading to the creation of copies in the stack. the root of the left is passed as the value in preorder.', 'duration': 47.618, 'max_score': 589.625, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/e_Wv_pH4Se8/pics/e_Wv_pH4Se8589625.jpg'}, {'end': 1174.025, 'src': 'embed', 'start': 1150.057, 'weight': 0, 'content': [{'end': 1156.592, 'text': 'here what you will write first of all, left means call the post order traversal.', 'start': 1150.057, 'duration': 6.535}, {'end': 1165.779, 'text': 'recursive call to this post order function on the left sub-tree means you will pass root of left, then right.', 'start': 1156.592, 'duration': 9.187}, {'end': 1171.943, 'text': 'so again call this post order on root of right.', 'start': 1165.779, 'duration': 6.164}, {'end': 1174.025, 'text': 'at last you are going to print the data.', 'start': 1171.943, 'duration': 2.082}], 'summary': 'Explains the post order traversal using recursive calls on left and right sub-trees.', 'duration': 23.968, 'max_score': 1150.057, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/e_Wv_pH4Se8/pics/e_Wv_pH4Se81150057.jpg'}, {'end': 1201.46, 'src': 'heatmap', 'start': 1174.025, 'weight': 0.735, 'content': [{'end': 1188.906, 'text': 'so here you will write printf, %d and root data and after printing all these values suppose i have printed here till one, it means this is the end.', 'start': 1174.025, 'duration': 14.881}, {'end': 1191.129, 'text': 'now it is going to return where.', 'start': 1188.906, 'duration': 2.223}, {'end': 1194.453, 'text': 'see, i have called this pre-order function in the main function from here.', 'start': 1191.129, 'duration': 3.324}, {'end': 1196.174, 'text': 'so it is going to return here now.', 'start': 1194.453, 'duration': 1.721}, {'end': 1198.196, 'text': 'Then inorder function would be called.', 'start': 1196.795, 'duration': 1.401}, {'end': 1201.46, 'text': 'Then after returning back, post-order traversal would be called.', 'start': 1198.517, 'duration': 2.943}], 'summary': 'The code involves printf and various function calls for preorder, inorder, and postorder traversal.', 'duration': 27.435, 'max_score': 1174.025, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/e_Wv_pH4Se8/pics/e_Wv_pH4Se81174025.jpg'}], 'start': 471.845, 'title': 'Stack memory allocation and binary tree traversals', 'summary': 'Discusses the stack memory allocation process during function calls, creating a tree structure, passing variables, emphasizing recursion and covers binary tree traversals including pre-order, in-order, and post-order with detailed recursive calls and printed outputs.', 'chapters': [{'end': 589.105, 'start': 471.845, 'title': 'Understanding stack memory allocation', 'summary': 'Discusses the process of stack memory allocation during function calls, with a focus on creating a tree structure and passing variables, and highlights the key steps and the role of recursion.', 'duration': 117.26, 'highlights': ['The control starts at the main function and moves to the create function where a root pointer is created and the tree is initialized, with the root pointer having the address 100.', "During a function call from the main function, stack memory is allocated for the function, including memory for local variables, and the control moves through the function's statements.", 'The process of recursion is discussed, with the numbering of statements to illustrate the flow within the function call and the execution of specific statements based on conditions.']}, {'end': 1212.11, 'start': 589.625, 'title': 'Binary tree traversals', 'summary': 'Explains the process of pre-order, in-order, and post-order traversals of a binary tree, emphasizing the creation of different copies of local variables for each function call and detailing the recursive calls and printed outputs for each traversal.', 'duration': 622.485, 'highlights': ['The chapter explains the creation of different copies of local variables for each function call, illustrating the process with detailed examples and emphasizing the significance of this concept in understanding the traversal of a binary tree.', 'The chapter provides a detailed walkthrough of the pre-order traversal, including the recursive calls and printed outputs for each function call, aiding in understanding the sequential process and expected outputs for the traversal.', 'The chapter elaborates on the in-order traversal process, emphasizing the recursive calls to the left and right subtrees and the sequential printing of the root data, providing a comprehensive understanding of the traversal sequence.', 'The chapter details the post-order traversal method, highlighting the recursive calls to the left and right subtrees and the final printing of the root data, enabling a clear comprehension of the traversal process and expected output sequence.']}], 'duration': 740.265, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/e_Wv_pH4Se8/pics/e_Wv_pH4Se8471845.jpg', 'highlights': ['The chapter details the post-order traversal method, highlighting the recursive calls to the left and right subtrees and the final printing of the root data, enabling a clear comprehension of the traversal process and expected output sequence.', 'The chapter elaborates on the in-order traversal process, emphasizing the recursive calls to the left and right subtrees and the sequential printing of the root data, providing a comprehensive understanding of the traversal sequence.', 'The chapter provides a detailed walkthrough of the pre-order traversal, including the recursive calls and printed outputs for each function call, aiding in understanding the sequential process and expected outputs for the traversal.', 'The chapter explains the creation of different copies of local variables for each function call, illustrating the process with detailed examples and emphasizing the significance of this concept in understanding the traversal of a binary tree.', 'The process of recursion is discussed, with the numbering of statements to illustrate the flow within the function call and the execution of specific statements based on conditions.', "During a function call from the main function, stack memory is allocated for the function, including memory for local variables, and the control moves through the function's statements.", 'The control starts at the main function and moves to the create function where a root pointer is created and the tree is initialized, with the root pointer having the address 100.']}], 'highlights': ['The chapter provides a detailed walkthrough of the pre-order traversal, including the recursive calls and printed outputs for each function call, aiding in understanding the sequential process and expected outputs for the traversal.', 'The chapter details the post-order traversal method, highlighting the recursive calls to the left and right subtrees and the final printing of the root data, enabling a clear comprehension of the traversal process and expected output sequence.', 'The chapter elaborates on the in-order traversal process, emphasizing the recursive calls to the left and right subtrees and the sequential printing of the root data, providing a comprehensive understanding of the traversal sequence.', 'The detailed explanation includes the use of pointers, the function definition, and the specific scenario of passing the address (100) as the root pointer.', 'The chapter explains the creation of different copies of local variables for each function call, illustrating the process with detailed examples and emphasizing the significance of this concept in understanding the traversal of a binary tree.', "During a function call from the main function, stack memory is allocated for the function, including memory for local variables, and the control moves through the function's statements.", 'The video discusses the implementation of inorder, preorder, and postorder traversal of a binary tree using a recursive approach, with references to previous videos and examples provided for better understanding.', 'Explanations and coding of the tree traversals are done, emphasizing the use of a recursive approach for implementation.', 'Mention of previous video explaining binary tree creation and a link provided for reference.']}