{"id":76155,"date":"2024-07-19T11:00:58","date_gmt":"2024-07-19T15:00:58","guid":{"rendered":"https:\/\/bangla.sitestree.com\/?p=76155"},"modified":"2024-07-28T17:02:54","modified_gmt":"2024-07-28T21:02:54","slug":"input-tree-traversal-data-output-build-the-tree","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=76155","title":{"rendered":"Input: Tree Traversal Data Output: Build the Tree"},"content":{"rendered":"\n<div class=\"wp-block-media-text is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><\/figure><div class=\"wp-block-media-text__content\">\n<p>Problem: <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-4.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"229\" src=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-4.png?resize=750%2C229&#038;ssl=1\" alt=\"\" class=\"wp-image-76156\" srcset=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-4.png?w=868 868w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-4.png?resize=300%2C92 300w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-4.png?resize=768%2C234 768w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Ref: Book on Data Structure and Algorithm in C<\/p>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-media-text is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><\/figure><div class=\"wp-block-media-text__content\">\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-7.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"74\" src=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-7-1024x101.png?resize=750%2C74&#038;ssl=1\" alt=\"\" class=\"wp-image-76159\" srcset=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-7.png?resize=1024%2C101 1024w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-7.png?resize=300%2C29 300w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-7.png?resize=768%2C75 768w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-7.png?w=1109 1109w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<p>\u2022From In-order Output to Build the Tree<\/p>\n\n\n\n<p>\u2022Take In-order Traversal Output Data<\/p>\n\n\n\n<p>\u2022And Build the Tree<\/p>\n\n\n\n<p>\u2022Take the middle (N) or so as the root<\/p>\n\n\n\n<p>\u2022Keep going\/taking alternate left (nodeLabels) (K D) from there<\/p>\n\n\n\n<p>\u2022Make those also roots\/parents (left sub tree)<\/p>\n\n\n\n<p>\u2022The last may be at the last left in this flow<\/p>\n\n\n\n<p>\u2022Then from the last (left last of data)<\/p>\n\n\n\n<p>\u2022Take alternate nodeLabels (P, E) and make them the right child (in left side tree)<\/p>\n\n\n\n<p>\u2022From middle Keep going\/taking alternate right node labels (A L)<\/p>\n\n\n\n<p>\u2022Make center\/parent\/root (right subtree)<\/p>\n\n\n\n<p>\u2022From the end reverse back with the alternate nodes (T F)<\/p>\n\n\n\n<p>\u2022Make these as left children<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-media-text is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><\/figure><div class=\"wp-block-media-text__content\">\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-6.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"46\" src=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-6-1024x63.png?resize=750%2C46&#038;ssl=1\" alt=\"\" class=\"wp-image-76158\" srcset=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-6.png?resize=1024%2C63 1024w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-6.png?resize=300%2C18 300w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-6.png?resize=768%2C47 768w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-6.png?resize=1536%2C95 1536w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-6.png?w=1900 1900w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<p>From Pre-order Tree Traversal Output to Build the Tree<br \/>First one becomes the root such as N<br \/>Some immediate ones (D G) also becomes parents up until middle (you can choose) \u2013 left sub tree parens<br \/><br \/>Then put some immediate ones (K P) as right children (in left subtree) to come to root<br \/><br \/>Then take alternate (E F L) to have the parents on the right (Goes Right, right subtree)<br \/><br \/>come back from right, take remaining (A, T), make them left children (bottom to up direction)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-media-text is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><\/figure><div class=\"wp-block-media-text__content\">\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-5.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"76\" src=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-5.png?resize=750%2C76&#038;ssl=1\" alt=\"\" class=\"wp-image-76157\" srcset=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-5.png?w=1003 1003w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-5.png?resize=300%2C30 300w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-5.png?resize=768%2C77 768w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<p>\u2022Input: Post order Tree Traversal data, Output: Tree<\/p>\n\n\n\n<p>\u2022Last one (E) becomes the root<\/p>\n\n\n\n<p>\u2022Some immediate (backward) right ones ones (L A T) up until middle (you can choose)&nbsp; also becomes parents (downward right side parent)<\/p>\n\n\n\n<p>\u2022\u2013 Then put some immediate ones (backward) ( N F ) as left&nbsp; children to come to root<br \/><br \/>Then take alternate (K P) to have the parents on the left side of the tree<\/p>\n\n\n\n<p>\u2022Come back from the last, with the remaining ones (G D)<\/p>\n\n\n\n<p>\u2022and make right children (left sub tree)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2022From In-order Output to Build the Tree \u2022Take In-order Traversal Output Data \u2022And Build the Tree \u2022Take the middle (N) or so as the root \u2022Keep going\/taking alternate left (nodeLabels) (K D) from there \u2022Make those also roots\/parents (left sub tree) \u2022The last may be at the last left in this flow \u2022Then from the &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=76155\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1966,1883,182],"tags":[],"class_list":["post-76155","post","type-post","status-publish","format-standard","hentry","category-data-structure-and-algorithms","category---data-structure","category---blog","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":76183,"url":"http:\/\/bangla.sitestree.com\/?p=76183","url_meta":{"origin":76155,"position":0},"title":"Struct and Tree Node Examples","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"#include <iostream> #include <string.h> #pragma warning(disable : 4996) #define MaxWordSize 100 \/\/ Declare a structure that holds data in a node typedef struct { int num; } NodeDataInt; \/\/ Declare a structure that holds data in a node typedef struct { char word[MaxWordSize + 1]; int freq; } NodeDataChar; \/\/\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76185,"url":"http:\/\/bangla.sitestree.com\/?p=76185","url_meta":{"origin":76155,"position":1},"title":"A Binary Tree Declaration","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"#include <stdio.h> #include <stdlib.h> \/\/ Declare a structure that holds data in a node typedef struct { int num; } NodeData; typedef struct treeNode { NodeData data; struct treeNode* left, * right; } TreeNode, *TreeNodePtr; typedef struct { TreeNodePtr root; } BinaryTree; \/\/ main method int main() { \/\/ NULL\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76181,"url":"http:\/\/bangla.sitestree.com\/?p=76181","url_meta":{"origin":76155,"position":2},"title":"Struct and Tree Node","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"\/\/ Ref: typedef and struct \/\/ https:\/\/www.w3resource.com\/c-programming-exercises\/c-snippets\/difference-between-typedef-struct-and-struct-definitions-with-example.php#google_vignette \/\/ https:\/\/www.tutorialspoint.com\/cprogramming\/c_pointers.htm \/\/ https:\/\/www.geeksforgeeks.org\/typedef-in-c\/ #pragma warning(disable : 4996) #include <iostream> #include <string.h> \/\/ Declare a structure that holds data in a node typedef struct { int num; } NodeData; \/\/ define what a node will look like typedef struct treenode { NodeData data;\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76152,"url":"http:\/\/bangla.sitestree.com\/?p=76152","url_meta":{"origin":76155,"position":3},"title":"Input: Post order Tree Traversal data, Output: Tree","author":"Sayed","date":"July 19, 2024","format":false,"excerpt":"\u2022 Last one (E) becomes the root \u2022 Some immediate (backward) right ones ones (L A T) up until middle (you can choose)\u00a0 also becomes parents (downward right side parent) \u2022 \u2013 Then put some immediate ones (backward) ( N F ) as left\u00a0 children to come to rootThen take\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-3.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-3.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-3.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/07\/image-3.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":76187,"url":"http:\/\/bangla.sitestree.com\/?p=76187","url_meta":{"origin":76155,"position":4},"title":"C Code Build a Binary Tree.","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"#include <iostream> #include <string.h> #include <stdlib.h> #pragma warning(disable : 4996) #define MaxWordSize 100 \/\/ Declare a structure that holds data in a node typedef struct { char word[MaxWordSize + 1]; } NodeData; typedef struct treeNode { NodeData data; struct treeNode* left, * right; } TreeNode, *TreeNodePtr; typedef struct { TreeNodePtr\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76201,"url":"http:\/\/bangla.sitestree.com\/?p=76201","url_meta":{"origin":76155,"position":5},"title":"Steps to Create a Binary Search Tree","author":"Sayed","date":"July 28, 2024","format":false,"excerpt":"If binary tree is empty, create a node, and assign data, point to itOtherwise : point to root node Compare the new - data - to - insert with the current node dataWe maintain a pointer say current to point to the node under visit If data matched : nothing\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=76155"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76155\/revisions"}],"predecessor-version":[{"id":76160,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76155\/revisions\/76160"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=76155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=76155"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=76155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}