Dru89

Untitled

Mar 13th, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. /*
  2.  * File:   main.cpp
  3.  * Author: Steven Leimberg
  4.  *
  5.  * Created on March 13, 2012, 2:16 PM
  6.  */
  7.  
  8. #include <iostream>
  9. #include <cstdlib>
  10.  
  11. using namespace std;
  12.  
  13. struct node{
  14.     string prezname;
  15.     node* Right;
  16.     node* Left;
  17. };
  18.  
  19. void insert(string, node*);
  20.  
  21. node* search(string, node*);
  22.  
  23. int main(){
  24.     node* prezTreeRoot;
  25.     prezTreeRoot = new node;
  26.     insert("AA",prezTreeRoot);
  27. }
  28.  
  29. void insert(string name, node* leafRoot){
  30.     node* leaf = search(name,leafRoot);
  31.     if(leaf!=NULL){
  32.         if(name< leaf->prezname){
  33.             leaf->Left=new node;
  34.             leaf->Left->prezname=name;
  35.             leaf->Left->Left=NULL;
  36.             leaf->Left->Right=NULL;
  37.         }
  38.         if(name>leaf->prezname){
  39.             leaf->Right=new node;
  40.             leaf->Right->prezname=name;
  41.             leaf->Right->Left=NULL;
  42.             leaf->Right->Right=NULL;
  43.         }
  44.     }else{
  45.         leaf=new node;
  46.         leaf->prezname=name;
  47.         leaf->Left=NULL;
  48.         leaf->Right=NULL;
  49.     }
  50. }
  51.  
  52. node* search(string name, node* leaf){
  53.     while(leaf!=NULL){
  54.         if(leaf->prezname<name){
  55.             leaf=leaf->Left;
  56.         }
  57.         if(leaf->prezname>name){
  58.             leaf=leaf->Right;
  59.         }
  60.         if(leaf->prezname==name){
  61.             cout<<name<<" already exists in this tree!"<<endl;
  62.             break;
  63.         }
  64.     }
  65.     return leaf;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment