Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://www.facebook.com/AminproPastebin
- // Thank you Joshua Soo for sharing and helping with the code :)
- // ^ ini orang life saver
- //And also to Dato Absyarie Syafiq for the skeleton to speed up the coding ^^
- #include<stdio.h>
- #include<stdlib.h>
- #include<ctype.h>
- #include<string.h>
- struct BTnode *root, *parent1, *parent2; // Global
- struct BTnode {
- char val;
- struct BTnode *left;
- struct BTnode *right;
- };
- struct BTnode* newBTnode(char val);
- void freeBT(struct BTnode *root);
- void inorder(struct BTnode*node);
- void growTree(void){
- root = newBTnode('G');
- root->left = newBTnode('B');
- root->right = newBTnode('K');
- parent1 = root->left;
- parent1->left = newBTnode('A');
- parent1->right = newBTnode('C');
- parent2 = root->right;
- parent2->left = newBTnode('J');
- parent2->right = newBTnode('M');
- }
- void inorder(struct BTnode *node){
- if (node){
- inorder(node->left);
- printf("%c\n",node->val);
- inorder(node->right);
- printf("\t");
- }
- }
- int main(void){
- growTree();
- inorder(root);
- freeBT(root);
- return 0;
- }
- struct BTnode* newBTnode(char val) {
- struct BTnode* newnode ;
- newnode = (struct BTnode*)malloc(sizeof(struct BTnode));
- newnode->val = val;
- newnode->left = NULL;
- newnode->right = NULL;
- return newnode;
- }
- void freeBT(struct BTnode *root) {
- if (root) {
- freeBT(root->left);
- freeBT(root->right);
- free(root);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement