home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
advfun7.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-15
|
1KB
|
58 lines
// C++ program which passes parameter to dynamic data
#include <iostream.h>
#include <string.h>
const unsigned MAX = 30;
typedef struct node* nodeptr;
struct node {
char value[MAX+1];
nodeptr left;
nodeptr right;
};
void insert(nodeptr& root, const char* item)
// Recursively insert element in binary tree
{
if (!root) {
root = new node;
strncpy(root->value, item, MAX);
root->left = NULL;
root->right = NULL;
}
else {
if (strcmp(item, root->value) < 0)
insert(root->left,item);
else
insert(root->right,item);
}
}
void showTree(nodeptr& root)
{
if (!root)
return;
showTree(root->left);
cout << root->value << "\n";
showTree(root->right);
}
main()
{
char *names[] = { "Virginia", "California", "Maine", "Michigan",
"New york", "Florida", "Ohio", "Illinois",
"Alaska", "Arizona", "Oregon", "Vermont",
"Maryland", "Delaware", "NULL" };
nodeptr treeRoot = NULL;
int i = 0;
// insert the names in the binary tree
while (strcmp(names[i], "NULL") != 0)
insert(treeRoot, names[i++]);
showTree(treeRoot);
return 0;
}