{"id":76187,"date":"2024-07-20T20:18:12","date_gmt":"2024-07-21T00:18:12","guid":{"rendered":"https:\/\/bangla.sitestree.com\/?p=76187"},"modified":"2024-07-28T17:02:53","modified_gmt":"2024-07-28T21:02:53","slug":"c-code-build-a-binary-tree","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=76187","title":{"rendered":"C Code Build a Binary Tree."},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n#include &lt;string.h>\n#include &lt;stdlib.h>\n\n#pragma warning(disable : 4996)\n\n#define MaxWordSize 100\n\n\/\/ Declare a structure that holds data in a node\ntypedef struct {\n\tchar word&#91;MaxWordSize + 1];\n} NodeData;\n\ntypedef struct treeNode {\n\tNodeData data;\n\tstruct treeNode* left, * right;\n} TreeNode, *TreeNodePtr;\n\ntypedef struct {\n\tTreeNodePtr root;\n} BinaryTree;\n\nTreeNodePtr buildTree(FILE *in) {\n\n\tchar str&#91;MaxWordSize + 1];\n\t\n\t\/\/read one word from the file\n\tfscanf_s (in, \"%s\", str);\n\n\t\/\/if we see @ then return null (end of current recursion)\n\tif (strcmp (str, \"@\") == 0) return NULL;\n\n\t\/\/allocate space for a TreeNode\n\tTreeNodePtr p = (TreeNodePtr) malloc (sizeof(TreeNode));\n\n\t\/\/ copy the data to the tree node\n\tstrcpy_s (p->data.word, str);\n\n\t\/\/ build the left sub tree using recursion\n\tp->left = buildTree(in);\n\n\t\/\/ build the right sub tree using recursion\n\tp->right = buildTree(in);\n\n\treturn p;\n\n} \/\/end buildTree\n\n\n\/\/ traverse the tree in in-order basis\n\/\/ print the treee content in in-order basis\nvoid inOrder(TreeNodePtr node) {\t\n\t\/\/printf(\"%s \", node->data.word);\n\tif (node != NULL) {\n\t\tinOrder(node->left);\n\t\tprintf(\"%s \", node->data.word);\n\t\tinOrder(node->right);\n\t}\n} \/\/end inOrder\n\n\n\/\/ main method\nint main(){\n\n\t\/\/ declare a binary tree\n\tBinaryTree bt;\n\n\t\/\/open the file where tree data are kept\n\tFILE *in = fopen(\"btree.in\", \"r\");\n\t\n\t\/\/ build the tree\n\tbt.root = buildTree(in);\n\n\tinOrder(bt.root);\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","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-76187","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":76185,"url":"http:\/\/bangla.sitestree.com\/?p=76185","url_meta":{"origin":76187,"position":0},"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":76183,"url":"http:\/\/bangla.sitestree.com\/?p=76183","url_meta":{"origin":76187,"position":1},"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":76181,"url":"http:\/\/bangla.sitestree.com\/?p=76181","url_meta":{"origin":76187,"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":76179,"url":"http:\/\/bangla.sitestree.com\/?p=76179","url_meta":{"origin":76187,"position":3},"title":"Struct\/Record Examples in C Programming Language","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"#pragma warning(disable : 4996) #include <iostream> #include <string.h> \/\/struct struct student { char name[50]; int age; float height; }; struct student s1 = { \" Nina Chase\", 12, 1.55 }; struct student s2 = { \" Shyann Morris\", 12, 1.65 }; \/\/typedef struct typedef struct { char name[50]; int age;\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":76173,"url":"http:\/\/bangla.sitestree.com\/?p=76173","url_meta":{"origin":76187,"position":4},"title":"Nodes: Vertices and Edges. Nodes for them","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"define MaxWordSize 100 #define MaxWordSize 100 \/\/edge of a graph typedef struct gEdge{ \/\/ child is the location of the child vertex int child, weight; struct gEdge *nextEdge; } GEdge, *GEdgePtr; typedef struct{ char id[MaxWordSize]; int parent, cost, discover, finish, inDegree; GEdgePtr firstEdge; } GVertex; \/\/main method int main() {\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":8374,"url":"http:\/\/bangla.sitestree.com\/?p=8374","url_meta":{"origin":76187,"position":5},"title":"\u09b8\u09bf \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae\u09bf\u0999\u09c7 typedef \u098f\u09b0 \u09ac\u09cd\u09af\u09ac\u09b9\u09be\u09b0","author":"Author-Check- Article-or-Video","date":"April 4, 2015","format":false,"excerpt":"\u09b8\u09bf \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae\u09bf\u0999\u09c7 typedef \u098f\u09b0 \u09ac\u09cd\u09af\u09ac\u09b9\u09be\u09b0\u0983 \u09b0\u09bf\u09a6\u0993\u09df\u09be\u09a8 \u09ac\u09bf\u09a8 \u09b6\u09be\u09ae\u09c0\u09ae \u09b8\u09bf \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae\u09bf\u0982 \u09b2\u09cd\u09af\u09be\u0999\u09cd\u0997\u09c1\u09df\u09c7\u099c typedef \u09a8\u09be\u09ae\u09c7\u09b0 \u098f\u0995\u099f\u09bf \u0995\u09bf-\u0993\u09df\u09be\u09b0\u09cd\u09a1 \u09ac\u09cd\u09af\u09ac\u09b9\u09be\u09b0 \u0995\u09b0\u09a4\u09c7 \u09a6\u09c7\u09df, \u09af\u09be \u09a8\u09a4\u09c1\u09a8 \u09a8\u09be\u09ae \u099f\u09be\u0987\u09aa \u0995\u09b0\u09a4\u09c7 \u09a6\u09c7\u09df, \u09a8\u09bf\u099a\u09c7\u09b0 \u0989\u09a6\u09be\u09b9\u09b0\u09a3\u09c7 BYTE \u099f\u09be\u09b0\u09cd\u09ae \u09ac\u09bf\u09ac\u09c3\u09a4 \u0995\u09b0\u09be \u09b9\u09df\u09c7\u099b\u09c7\u0964 typedef unsigned char BYTE; \u09ac\u09bf\u09ac\u09c3\u09a4 \u0995\u09b0\u09be\u09b0 \u09aa\u09b0 unsigned char \u098f\u09b0 \u09ac\u09cd\u09af\u09be\u0996\u09cd\u09af\u09be \u09b9\u09bf\u09b8\u09be\u09ac\u09c7 BYTE identifier \u09b0\u09c2\u09aa\u09c7 \u0995\u09be\u099c \u0995\u09b0\u09c7\u0964 \u09af\u09c7\u09ae\u09a8, BYTE\u2026","rel":"","context":"In &quot;\u09aa\u09cd\u09b0\u09be\u09a5\u09ae\u09bf\u0995 \u09b8\u09bf \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae\u09bf\u0982 \u0964 C Programming Basics&quot;","block_context":{"text":"\u09aa\u09cd\u09b0\u09be\u09a5\u09ae\u09bf\u0995 \u09b8\u09bf \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae\u09bf\u0982 \u0964 C Programming Basics","link":"http:\/\/bangla.sitestree.com\/?cat=239"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76187","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=76187"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76187\/revisions"}],"predecessor-version":[{"id":76188,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76187\/revisions\/76188"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=76187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=76187"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=76187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}