Home › Forums › C Programming › SOS!!! › Reply To: SOS!!!
August 25, 2008 at 5:34 am
#3424
Humayan
Participant
I have this binary seach tree ( hope this helps ) :
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | /*<br /> * Název programu: ADT Binarni strom<br /> * Autor: Petr Bobek<br /> * Studijnà skupina: IT-01<br /> * Email autora: [email protected]<br /> * Datum vypracovánÃ: 26.10.2005<br /> * Stručný popis řeÅ¡enÃ: Je uveden dale v programu<br /> */<br /> #include "iostream"<br /> #include "stdio.h"<br /> #include "conio.h"<br /> #include "malloc.h"<br /> #include "process.h"<br /> #include "stdlib.h"<br /> #include "string.h" //standardnà knihovny<br /> using namespace std;<br /> typedef char tStr;<br /> struct uzel<br /> {<br /> int item;<br /> struct uzel *right;<br /> struct uzel *left;<br /> };<br /> typedef struct uzel * BST;<br /> typedef struct uzel uzel;<br /> bool Empty ( BST t )<br /> {<br /> if( t != NULL )<br /> return false;<br /> else<br /> return true;<br /> }<br /> void Initialize ( BST * t )<br /> {<br /> * t = NULL;<br /> <br /> }<br /> void DeleteTree ( BST t )<br /> {<br /> if ( t != NULL )<br /> {<br /> DeleteTree ( t-> left );<br /> DeleteTree ( t->right );<br /> free ( t );<br /> }<br /> }<br /> BST Search ( BST t, int x )<br /> {<br /> if( t == NULL )<br /> {<br /> cout << "item was not found!" << endl;<br /> return NULL;<br /> }<br /> if( x < t -> item )<br /> {<br /> return Search( t -> left , x );<br /> }<br /> else if( x > t -> item )<br /> return Search ( t -> right , x );<br /> else<br /> return t;<br /> <br /> }<br /> BST Insert ( BST * t, int x )<br /> {<br /> if( * t == NULL )<br /> {<br /> * t = (struct uzel * ) malloc ( sizeof ( struct uzel ) );<br /> (* t ) -> item = x;<br /> ( * t ) -> left = ( * t ) -> right = NULL;<br /> return * t;<br /> }<br /> <br /> if( ( *t ) -> item > x)<br /> Insert ( & ( ( * t ) -> left ) , x );<br /> else<br /> Insert ( & ( ( * t ) -> right ) , x );<br /> <br /> <br /> }<br /> BST findmin ( BST t )<br /> {<br /> <br /> while ( t -> left )<br /> t = t -> left;<br /> return t;<br /> <br /> }<br /> BST findmax ( BST t )<br /> {<br /> while ( t -> right )<br /> t = t -> right;<br /> return t;<br /> <br /> }<br /> void removeMin ( BST * t )<br /> {<br /> while ( ( * t ) -> left )<br /> ( * t ) = ( * t ) -> left;<br /> <br /> BST temp = * t;<br /> * t = ( ( * t ) -> right );<br /> free ( temp );<br /> <br /> }<br /> //THIS DOESN'T LOOK RIGHT<br /> BST Delete ( BST * t, int x )<br /> {<br /> BST pom_bunka;<br /> if( t == NULL )<br /> {<br /> cout << " Cannot delete, bin. tree is empty !"<< endl;<br /> return NULL;<br /> }<br /> if ( x < ( * t ) -> item )<br /> Delete ( & ( ( * t ) -> left ) , x );<br /> else if ( x > ( * t ) -> item )<br /> Delete ( & ( ( * t ) -> right ) , x ); <br /> else if ( ( * t ) -> right != NULL && ( * t ) -> left != NULL )<br /> {<br /> BST temp = findmin ( ( * t ) -> right );<br /> ( * t ) -> item = temp -> item;<br /> removeMin ( & (( * t ) -> right ) );<br /> }<br /> else<br /> {<br /> BST temp = * t ;<br /> if ( ( * t ) -> left != NULL )<br /> * t = ( * t ) -> left;<br /> else<br /> * t = ( * t ) -> right;<br /> free ( temp );<br /> }<br /> }<br /> void Preorder ( BST t )<br /> {<br /> if ( t != NULL )<br /> {<br /> cout << t -> item << endl;<br /> Preorder ( t -> left );<br /> Preorder ( t -> right ) ;<br /> }<br /> <br /> <br /> }<br /> void Inorder ( BST t )<br /> {<br /> if ( t != NULL )<br /> {<br /> Inorder ( t -> left );<br /> cout << t -> item << endl;<br /> Inorder ( t -> right );<br /> }<br /> }<br /> void Postorder ( BST t )<br /> {<br /> if ( t != NULL )<br /> {<br /> Postorder ( t -> left );<br /> Postorder ( t -> right );<br /> cout << t -> item << endl;<br /> }<br /> }<br /> int getData ( void )<br /> {<br /> <br /> cout << "Get data: ";<br /> int data;<br /> cin >> data;<br /> return data;<br /> }<br /> /*<br /> void VypisSeznam(BST t){<br /> tStr data;<br /> cout << "Obsah seznamu:";<br /> if (Empty(t)==true){<br /> cout<<" NULL"<<endl;<br /> }<br /> else{<br /> data = t->item;<br /> cout << data << ", ";<br /> }<br /> }<br /> */<br /> void Menu ( void )//vypis menu<br /> {<br /> cout << "nGet number 0-7: nn";<br /> cout << "0: Inicializen";<br /> cout << "1: Insertn";<br /> cout << "2: Searchn";<br /> cout << "3: Preordern";<br /> cout << "4: Inordern";<br /> cout << "5: Postordern";<br /> cout << "6: Deleten";<br /> cout << "7: Delete treen";<br /> cout << "M: Menun";<br /> cout << "E: Endn";<br /> }<br /> int main()<br /> {<br /> <br /> char znak;<br /> BST strom = 0 ; //root ?<br /> int data;<br /> //tStr data;<br /> do {<br /> Menu();<br /> cout << "nnEnter Choice ! >> ";<br /> cin >> znak;<br /> switch ( toupper ( znak ) )<br /> {<br /> case '0' : cout << "Inicialize - inicializuje stromn";<br /> Initialize ( & strom );<br /> //VypisSeznam(strom);<br /> break;<br /> case '1' : cout << "Insert - vlozi do stromu novy uzeln";<br /> cout << "Get data: ";<br /> cin >> data;<br /> Insert( & strom , data );<br /> //VypisSeznam(strom);<br /> break;<br /> case '2' : cout << "Search - vyhleda uzeln";<br /> Search(strom, getData());<br /> cout<<strom<<endl;<br /> break;<br /> case '3' : cout << "Preorder - projde strom pruchodem preordern";<br /> Preorder(strom);<br /> break;<br /> case '4' : cout << "Inorder - projde strom pruchodem inordern";<br /> Inorder(strom);<br /> break;<br /> case '5' : cout << "Postorder - projde strom pruchodem postordern";<br /> Postorder(strom);<br /> break;<br /> case '6' : cout << "Delete - smaze ze stromu uzeln";<br /> Delete( & strom, getData());<br /> break;<br /> case '7' : cout << "Delete Tree - smaze cely stromn";<br /> DeleteTree(strom);<br /> break;<br /> case 'M' : Menu();<br /> break;<br /> case 'E' :<br /> break;<br /> default : cout << "Zadali jste nespravny znak!!!n";<br /> }<br /> } while (toupper(znak) != 'E');<br /> return 0;<br /> } |