aboutsummaryrefslogtreecommitdiff
path: root/CS2501/trees/tree2.c
blob: 25bf50b42714efc1e66bf3ba461c7b3c96f3c80c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdlib.h>
#include <string.h>
#include <stdio.h>


typedef struct node {
	int id;
	struct node *left;
	struct node *right;
} treenode;


int countNodes(t)
treenode *t;
{
	int i = 0;
	if(!t) return 0;
	return 1 + countNodes(t->left) + countNodes(t->right);

}




main (argc, argv, envp)
int argc;
char **argv, **envp;
{

	treenode *t;
	t = malloc(sizeof(treenode));
	/* build a binary tree with at least 6 nodes */
	t->right = malloc(sizeof(treenode));
	t->left = malloc(sizeof(treenode));
	t->right->right = malloc(sizeof(treenode));
	t->right->left = malloc(sizeof(treenode));
	t->left->right = malloc(sizeof(treenode));
	t->left->left = malloc(sizeof(treenode));

	t->right->right->right = 0;
	t->right->right->left = 0;
	t->right->left->right = 0;
	t->right->left->left = 0;
	t->left->right->right = 0;
	t->left->right->left = 0;
	t->left->left->right = 0;
	t->left->left->left = 0;

	printf("the tree has %d nodes\n", countNodes(t));
}