Expand|Select|Wrap|Line Numbers
- #include <stdlib.h>
- #include <stdio.h>
- #include <conio.h>
- #define TRUE 1
- #define FALSE 0
- #define TOTAL_SLOTS 500000
- struct nodetype
- {
- long int info;
- long int used;
- }node[TOTAL_SLOTS] ;
- //initialize a tree
- void initialize ();
- void initialize()
- {
- long int i;
- for (i = 0; i<TOTAL_SLOTS; i++)
- node[i].used = FALSE; //mark every node as unused
- }
- //add node to the tree
- void addnode (long int node_id,long int value) ;
- void addnode (long int node_id,long int value )
- {
- node[node_id].info = value; //assign the value of the number to the appropriate node_id in the tree
- node[node_id].used = TRUE ; //mark node as used one
- }
- int main(void)
- {
- //function ask user for the list of numbers
- printf("To start, please enter the number of elements in the list: ");
- scanf("%d",&n);
- for (i=0;i<n;i++)
- {
- printf("Please enter number #%d: ",i+1);
- scanf("%d",&a[i]);
- }
- //initialize the tree
- initialize();
- //examine the numbers in the list
- for(i=0;i<n;i++)
- {
- node_id=0;
- while ((node_id < TOTAL_SLOTS))
- {
- if (node[node_id].used == FALSE) //while node_id has not been used
- {
- addnode(node_id,a[i]); //add node_id to the binary search tree
- break;
- }
- if (a[i] == node[ node_id ].info) //while current value has already been in the BST
- {
- printf("\n%d is a duplicate", a[i]);
- break;
- }
- if (a[i] < node[node_id].info) //while value is smaller than the root
- node_id = 2* node_id +1; //go to the left subtree
- else
- node_id = 2* node_id +2; //go to the right subtree
- }
- if (node_id >=TOTAL_SLOTS)
- {
- printf("overflow error\n");
- exit(1);
- }
- }
- getch ();
- }