Data Structures using C Part 22 - Binary trees operations in c language | Binary Tree Traversals
Binary Tree Traversals Preorder Traversal Inorder Traversal Postorder Traversal Levelorder Traversal Preorder Traversal : Process the Root node Traverse left subtree Traverse right subtree pre order traversal program: void preOrder(struct btNode *root) { if(root) { printf("%d", root- greaterthaninfo); preOrder(root- greaterthanleft); preOrder(root- greaterthan right); } } Inorder Traversal : Traverse left subtree Process the Root node Traverse right subtree Inorder traversal program : void inOrder(struct btNode *root) { if(root) { inOrder(root- greaterthan left); printf("%d", root- greaterthan info); inOrder(root- greaterthan right); } } Post order Traversal : Traverse left subtree Traverse right subtree Process the Root node Post order Traversal program : void postOrder(struct btNode *root) { if(root) { postOrder(root- greaterthan left); postOrder(root- greaterthan right); printf("%d", root- greaterthan info); } } Level order Traversal : Process root Go to next level, process left to right Repeat step 2 until the last level 1. visit the root 2. while traversing level L, keep all the elements at level L+1 in a queue. 3. Go to the next level and visit all the nodes at that level. 4. Repeat this until all levels are completed. Level order Traversal program : void levelOrder(struct btNode *root) { struct btNode *temp; struct Queue *Q = createQueue( ); if(root==NULL) return; enQueue(Q,root); while(!isEmptyQueue(Q)) { temp = deQueue(Q); printf("%d",temp- greaterthan info); if(temp- greaterthan left) enQueue(Q,temp- greaterthan left); if(temp- greaterthan right) enQueue(Q,temp- greaterthan right); } deleteQueue(Q); } ankpro ankpro training Asp.net MVC C# C sharp Bangalore Rajajinagar Selenium Coded UI Mobile automation testing Mobile testing JQuery JavaScript .Net C C++ Components of the .Net framework Hello World Literal Keywords Variable Data types Operators Branching Loops Arrays Strings Structures Enums Functions
Download
0 formatsNo download links available.