Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct tree
- {
- int x;
- struct tree *left;
- struct tree *right;
- };
- int altezza(struct tree*);
- int numnodi(struct tree*);
- struct tree *insabr(struct tree*,int);
- int main()
- {
- struct tree *root=NULL;
- printf("%d %d", altezza(root), numnodi(root));
- return 0;
- }
- int altezza(struct tree *root)
- {
- int h1, h2;
- if(root==NULL)
- return -1;
- h1=altezza(root->left);
- h2=altezza(root->right);
- if(h1>h2)
- return h1+1;
- else
- return h2+1;
- }
- int numnodi(struct tree *root)
- {
- int res=0, s1, s2;
- if(root!=NULL)
- {
- s1=numnodi(root->left);
- s2=numnodi(root->right);
- res=s1+s2+1;
- }
- return res;
- }
- struct tree *insabr(struct tree *root, int k)
- {
- if(root==NULL)
- {
- root=(struct tree*)malloc(sizeof(struct tree));
- root->x=k;
- root->left=NULL;
- root->right=NULL;
- }
- else
- {
- if(k<root->x)
- root->left=insabr(root->left,k);
- else
- root->right=insabr(root->right,k);
- }
- return root;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement