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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <iostream>
#include <string>
using namespace std;
struct node
{
string info;
node *left;
node *right;
};
void insert (node *&q, string s)
{
node *r;
r = new (node);
r -> info = s;
r -> left = NULL;
r -> right = NULL;
if (q == NULL)
{
q = r;
}
else
{
if ( s < q -> info )
{
insert (q -> left , s);
}
else
{
insert (q -> right , s);
}
}
}
void sort (node *q)
{
if (q != NULL)
{
sort (q -> left);
cout<<q -> info<<" ";
sort (q -> right);
}
}
void displayLeaves (node *q)
{
if (q != NULL)
{
displayLeaves(q -> left);
if (q -> left == NULL && q -> right == NULL)
{
cout<< q -> info<<" ";
}
displayLeaves(q -> right);
}
}
void displayChildren (node *q)
{
if (q != NULL)
{
displayChildren(q -> left);
if (q -> left != NULL && q -> right != NULL)
{
cout<<q -> info<<" ";
}
displayChildren(q -> right);
}
}
void theOneChild (node *q)
{
if (q != NULL)
{
theOneChild(q -> left);
if (q -> left == NULL && q -> right == NULL)
{
cout<<q -> info<<" ";
}
else if ( q -> left != NULL && q -> right != NULL)
{
cout<<q -> info<<" ";
}
theOneChild(q -> right);
}
}
/*-----------------------------------------------
void displayDays(node *q, int days)
{
if (q != NULL)
displayDays ( //!@#$%^&*
}
------------------------------------------------*/
int main()
{
int choice;
node *tree;
char yesNo;
string month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int days[12] = {31, 28, 31, 30, 31,30, 31, 31,30, 31, 30, 31};
tree = NULL;
for (int i = 0; i < 12; ++i)
{
insert(tree, month[i]);
}
cout<<"\n--------------------------------- OPTIONS ------------------------------------\n\n";
cout<<"1. Display name of month in alphabetical order\n"<<
"2. Display leaves of the tree in alphabetical order\n"<<
"3. Display parents with two children\n"<<
"4. Display parents with only one child\n"<<
"5. Enter the name of a month to display the number of days in that month\n\n"<<
"------------------------------------------------------------------------------\n\n";
do{
cout<<"Please enter your choice (1 ~ 5): "; cin>>choice;
switch (choice)
{
case 1: sort(tree); break;
case 2: displayLeaves(tree); break;
case 3: displayChildren(tree); break;
case 4: theOneChild(tree); break;
case 5: cout<<"Enter the name of the month to display the number of days in that month: "; // !@#$%^&*
}
cout<<endl;
cin.ignore();
cout<<"Continue? (y/n): "; cin>>yesNo;
}
while (yesNo == 'y' || yesNo == 'Y');
//Termina
system("pause");
return 0;
}
|