Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
BST
//Title of this code //gcc 4.9.2 #include<stdio.h> #include<stdlib.h> typedef struct node node; struct node { int key; node *left; node *right; }; typedef struct Tree Tree; struct Tree { node*root; }; node* create_node(int key){ node*n=(node*)malloc(sizeof(node)); n->key=key; n->right=n->left=NULL; return n; } void create_tree(Tree**tree){ *tree=(Tree*)malloc(sizeof (Tree)); if(*tree==NULL)exit(1); (*tree)->root=NULL; } int is_the_tree_empty(Tree*tree){ if (tree->root==NULL)return 1; else return 0; } void add(Tree*tree,int key){ if(tree==NULL)return; node*n=create_node(key); if(tree->root==NULL){tree->root=n;return;} node*R=tree->root; node*parent; while(R!=NULL){ parent=R; if(key<R->key)R=R->left; else if(key>R->key)R=R->right; else return; } if(key<parent->key)parent->left=n; else parent->right=n; } void prefix(node*n){ if(n==NULL)return; printf("%4d",n->key); prefix(n->left); prefix(n->right); } void prefix_print(Tree*tree){ if(tree==NULL)return; prefix(tree->root); printf("\n"); } void infix(node*n){ if(n==NULL)return; infix(n->left); printf("%4d",n->key); infix(n->right); } void infix_print(Tree*tree){ if(tree==NULL)return; infix(tree->root); printf("\n"); } void postfix(node*n){ if(n==NULL)return; postfix(n->left); postfix(n->right); printf("%4d",n->key); } void postfix_print(Tree*tree){ if(tree==NULL)return; postfix(tree->root); printf("\n"); } int Nodes_Number(node*root){ if(root==NULL)return 0; else return 1+Nodes_Number(root->left)+Nodes_Number(root->right); } int Leaves_Number(node*root){ if(root==NULL)return 0; else if (root->left==NULL && root->right==NULL)return 1; else return Leaves_Number(root->left)+Leaves_Number(root->right); } int main(void) { int a,b; Tree*tree=NULL; create_tree(&tree); add(tree,150); add(tree,35); add(tree,196); add(tree,200); add(tree,23); add(tree,172); add(tree,48); prefix_print(tree); infix_print(tree); postfix_print(tree); a=Nodes_Number(tree->root); printf("Number of Nodes : %d \n",a); b=Leaves_Number(tree->root); printf("Number of Leaves : %d \n",b); return 0; }
run
|
edit
|
history
|
help
0
Loop practice
Static scope
ex1
tyiy
mod 4
HW2A
ATM
Assignment 10
Date
square pyramid