#include
#include
using namespace std;
int counter=1;
int temp=1000000;
int u,v;
int x=0;
ofstream file;
typedef struct Node
{
int data;
Node *left;
Node *right;
};
void addNode(Node **root,int newData)
{
if((*root) == NULL)
{
Node *newD;
newD = new Node;
newD->data = newData;
newD->left = NULL;
newD->right = NULL;
(*root) = newD;
(*root)->left = NULL;
(*root)->right = NULL;
cout<<"Data Added!";
}
else if(newData < (*root)->data)
addNode(&(*root)->left,newData);
else if(newData > (*root)->data)
addNode(&(*root)->right,newData);
else if(newData == (*root)->data)
cout<<"Data exist!";
}
void preOrder(Node *root){
if(root != NULL){
file << root->data << endl;
cout<<" "<<root->data;
preOrder(root->left);
preOrder(root->right);
}
}
void showLeaf(Node *root){
if(root == NULL) cout<<"Empty!";
if(root->left!=NULL) showLeaf(root->left);
if(root->right!=NULL) showLeaf(root->right);
if(root->right == NULL && root->left == NULL){
file << root->data << endl;
}
}
void showLeafZero(Node *root){
if(root == NULL) cout<<"Empty!";
if(root->left!=NULL) showLeaf(root->left);
if(root->right!=NULL) showLeaf(root->right);
if(root->right == NULL && root->left == NULL){
file << root->data << endl;
}
}
void countNodes(Node *root)
{
if(root == NULL) cout<<"Empty!";
if(root->left!=NULL){
countNodes(root->left);
x++;
file.open("D:\\File Pelajaran\\Polinema\\Semester 2\\Struktur Data\\File.txt");
file << x << endl;
file.close();
}
if(root->right!=NULL){
countNodes(root->right);
x++;
file.open("D:\\File Pelajaran\\Polinema\\Semester 2\\Struktur Data\\File.txt");
file << x << endl;
file.close();
}
}
int main(){
int choose;
Node *tree;
tree = NULL;
do
{
int data;
cout<<endl<<"======M E N U======\n";
cout<<"1. Add Node\n";
cout<<"2. Insert root and leaf data to \"File.txt\"\n";
cout<<"3. Show leaf\n";
cout<<"4. Show leaf Zero\n";
cout<<"5. Count Node\n";
cout<<"6. Exit\n";
cout<<"Choose : ";
cin>>choose;
switch(choose)
{
case 1:
x=0;
cout<<"New Data : ";
cin>>data;
addNode(&tree,data);
break;
case 2:
if(tree!=NULL){
file.open("D:\\File Pelajaran\\Polinema\\Semester 2\\Struktur Data\\File.txt");
preOrder(tree);
file.close();
}
else cout<<"Data Empty!!";
break;
case 3:
if(tree!=NULL){
file.open("D:\\File Pelajaran\\Polinema\\Semester 2\\Struktur Data\\File.txt");
showLeaf(tree);
file.close();
const int MAKS = 80;
char buf[MAKS+1];
ifstream file("D:\\File Pelajaran\\Polinema\\Semester 2\\Struktur Data\\File.txt");
while(file){
file.getline(buf,MAKS);
cout<<buf<<" ";
}
file.close();
}
else cout<<"Data Empty!!";
break;
case 4:
if(tree!=NULL){
file.open("D:\\File Pelajaran\\Polinema\\Semester 2\\Struktur Data\\File.txt");
showLeafZero(tree);
file.close();
const int MAKS = 80;
char buf[MAKS+1];
ifstream file("D:\\File Pelajaran\\Polinema\\Semester 2\\Struktur Data\\File.txt");
while(file){
file.getline(buf,MAKS);
cout<<buf<<" ";
if(buf==0){
cout << "Detect number 0 in leaf of tree!";
}
}
file.close();
}
else cout<<"Data Empty!!";
break;
case 5:
if(tree!=NULL)countNodes(tree);
else cout<<"Data Empty!!";
const int MAKS = 80;
char buf[MAKS+1];
ifstream file("D:\\File Pelajaran\\Polinema\\Semester 2\\Struktur Data\\File.txt");
file.getline(buf,MAKS);
cout<<buf;
file.close();
break;
}
}
while(choose!=6);
}
0 comments:
Posting Komentar