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
|
#include <iostream>
#include <fstream>
using namespace std;
struct node{
int data;
node *left;
node *right;
};
node *tree=NULL;
void add_node(node *tree, char how, int where, int what);
node *search(node *tree, int where);
node *search(node *some, int somewhere);
void print_inorder(node *tree);
int main(){
int start;
char how;
int where, what;
ifstream fin("test.txt");
fin>>start;
tree = new node;
tree->left=tree->right=NULL;
tree->data=start;
//cout<<tree->data;
do {
fin>>how>>where>>what;
add_node(tree, how, where, what);
} while(how!='F');
print_inorder(tree);
system("pause");
return 0;
}
void add_node(node *tree, char how, int where, int what){
node *p=tree;
p=search(p, where);
if(p->data==where) {
if (how=='L'){
p->left=new node;
p->left->data=what;
p->left->left=p->left->right=NULL;
}
if (how=='R'){
p->right=new node;
p->right->data=what;
p->right->left=p->right->right=NULL;
}
}
return;
}
node *search(node *some, int somewhere){
if(some->data==somewhere) return some;
else{
search(some->left, somewhere);
search(some->right, somewhere);
}
}
void print_inorder(node *tree) {
if (tree!=NULL) {
print_inorder(tree->left);
cout<<tree->data<<endl;
print_inorder(tree->right);
}
}
| |