Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BST.cpp : Defines the entry point for the console application.
- // Drzewo BST
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- struct node{
- node *left;
- node *right;
- int key;
- };
- void loadFile(node *& root);
- void printBT(string sp, string sn, node * root);
- node * minBST(node * root);
- node * maxBST(node * root);
- node * findBST(node * root, int value);
- void inorder(node * root);
- void saveFile(node *root);
- void addNode(node *& root, int value);
- int _tmain(int argc, _TCHAR* argv[])
- {
- node *root=new node;
- root=NULL;
- node *temp=new node;
- temp=NULL;
- int option, value;
- do{
- cout << "1. Add NOde" << endl;
- cout << "2. Load File" <<endl;
- cout << "3. Store File" <<endl;
- cout << "4. Minimum" <<endl;
- cout << "5. Maximum" <<endl;
- cout << "6. Find value" <<endl;
- cout << "7. InOrder "<<endl;
- cout << "10. Print Tree" <<endl;
- cout <<"\nPodaj opcje: ";
- cin >>option;
- switch (option){
- case 1:
- cout <<"Podaj liczbe: "<<endl;
- cin >>value;
- addNode(root,value);
- break;
- case 2:
- loadFile(root);
- break;
- case 3:
- saveFile(root);
- break;
- case 4:
- cout <<"Minimalny element to"<< minBST(root)->key<<endl;
- break;
- case 5:
- cout <<"Maksymalny element to"<< maxBST(root)->key<<endl;
- break;
- case 6:
- cout <<"Podaj liczbe: "<<endl;
- cin >>value;
- temp= findBST(root,value);
- if( temp!=NULL){
- cout <<"Znaleziony element "<<temp->key<<" pod adresem "<<temp<<endl;
- }else cout <<"Nie ma takiego elementu";
- break;
- case 7:
- inorder(root);
- break;
- //case 10:
- // printBT(" "," " , root);
- // break;
- }
- cout<<"\n\n";
- }while(option!=0);
- return 0;
- }
- void addNode(node *& root, int value){
- if(root==NULL){
- root=new node;
- root->left=NULL;
- root->right=NULL;
- root->key=value;
- }
- else{
- if(value<root->key) addNode(root->left,value);
- if (value>=root->key)addNode(root->right,value);
- }
- }
- void loadFile(node *&root) {
- int idx;
- fstream plik;
- plik.open("BSTdata.txt", ios::in);
- if (plik){
- while(!plik.eof()){
- plik >>idx;
- addNode(root,idx);
- }
- cout <<"Plik wczytany"<<endl;
- }
- plik.close();
- }
- void saveFile(node *root){
- ofstream plik;
- plik.open("result.txt",ios::out|ios::app);
- if(root->left)
- saveFile(root->left);
- plik << root->key <<" ";
- if(root->right)
- saveFile(root->right);
- plik.close();
- }
- node * minBST(node * root){
- while(root->left){
- root = root->left;
- }
- return root;
- }
- node * maxBST(node * root){
- while(root->right){
- root = root->right;
- }
- return root;
- }
- node * findBST(node * root, int value)
- {
- while (root) {
- if (value == root->key)
- return root;
- else if (value < root->key)
- root = root->left;
- else if (value > root->key)
- root = root->right;
- }
- return NULL;
- return root;
- }
- void printBT(string sp, string sn, node * root)
- {
- string s;
- string cr,cl,cp;
- cr = cl = cp = " ";
- cr[0] = 218; cr[1] = 196;
- cl[0] = 192; cl[1] = 196;
- cp[0] = 179;
- if(root)
- {
- s = sp;
- if(sn == cr) s[s.length() - 2] = ' ';
- printBT(s + cp, cr, root->right);
- s = s.substr(0,sp.length()-2);
- cout << s << sn << root->key << endl;
- s = sp;
- if(sn == cl) s[s.length() - 2] = ' ';
- printBT(s + cp, cl, root->left);
- }
- }
- void inorder(node * root) {
- if (root->left)
- inorder(root->left);
- cout << root->key << " ";
- if (root->right)
- inorder(root->right);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement