{"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_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"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":76183,"url":"http:\/\/bangla.sitestree.com\/?p=76183","url_meta":{"origin":76187,"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":76187,"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":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":76173,"url":"http:\/\/bangla.sitestree.com\/?p=76173","url_meta":{"origin":76187,"position":3},"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":76179,"url":"http:\/\/bangla.sitestree.com\/?p=76179","url_meta":{"origin":76187,"position":4},"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":76175,"url":"http:\/\/bangla.sitestree.com\/?p=76175","url_meta":{"origin":76187,"position":5},"title":"Build an Adjacency List Graph from Node Labels in a File","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"\/\/build the graph void buildGraph(FILE* in, Graph G) { int j, k, numEdges, weight; char name[MaxWordSize], nodeID[MaxWordSize], adjID[MaxWordSize]; \/\/ read the names of the vertices \/\/ and store them in the graph array for (j = 1; j <= G->numV; j++) { fscanf(in, \"%s\", name); G->vertex[j] = newGVertex(name); strcpy(G->vertex[j].id, name);\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\/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}]}}