dorbird

Algo_Lab_01

Oct 8th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <string>
  5. #include <iostream>
  6.  
  7. #include "main.h"
  8.  
  9. // wenn ECHO definiert ist, werden Ausgaben zum Debuggen erzeugt
  10. #define ECHO
  11.  
  12. //---- Baumklasse --------------------------------------------------------
  13. Tree::Tree(){ // Konstruktor
  14. #ifdef ECHO
  15.     printf("Konstruktor Tree\n");
  16. #endif
  17.  
  18.     n_nodes = 0;
  19.     root = NULL;
  20. }
  21.  
  22. Tree::~Tree(){ // Destruktor
  23. #ifdef ECHO
  24.     printf("Destruktor Tree\n");
  25. #endif
  26.  
  27.     if (root != NULL) delete root;
  28.    
  29. #ifdef ECHO
  30.     printf("Der letzte macht das Licht aus ;-)\n");
  31. #endif
  32. }
  33.  
  34. void Tree::Print(){ // Ausgabe
  35.     if( root == NULL){
  36.         printf("\nleerer Baum\n");
  37.     }else{
  38.         printf("\nAusgabe\nBaum mit %d Knoten:\n", n_nodes);
  39.         root->Print();
  40.     }
  41.     printf("\n\n");
  42. }
  43.  
  44. void Tree::Insert( std::string data){ // Einfuegen eines Datums
  45.     n_nodes++;
  46.  
  47.     if( root == NULL){
  48.         root = new TreeNode( data); // Wurzel anlegen
  49.     }else{
  50.         root->Insert( data); // rekursives Einfuegen
  51.     }
  52. }
  53.  
  54. //---- Knoten fuer Baum --------------------------------------------------
  55. TreeNode::TreeNode(){ // Konstruktor
  56. #ifdef ECHO
  57.     printf("Konstruktor TreeNode\n");
  58. #endif
  59.  
  60.     left = right = NULL;
  61. }
  62.  
  63. TreeNode::TreeNode( std::string data){ // Konstruktor mit Datum
  64. #ifdef ECHO
  65.     printf("Konstruktor TreeNode( %d)\n", data.c_str());
  66. #endif
  67.  
  68.     this->data = data; // this wegen des Namenskonfliktes
  69.     left = right = NULL;
  70. }
  71. // Implementierung eines Binaerbaumes zum Sortieren von Integers
  72. // Autor: MHB, (c) 23.10.2013
  73.  
  74. TreeNode::~TreeNode(){ // Destruktor
  75.     if( left  != NULL) delete left;
  76.     if( right != NULL) delete right;
  77.  
  78. #ifdef ECHO
  79.     printf("Destruktor TreeNode( %d)\n", data.c_str());
  80. #endif
  81. }
  82.  
  83. // Baumknoten “in-order“ ausgeben
  84. void TreeNode::Print(){
  85.     printf(" ");
  86.     if( right != NULL) right->Print(); // rechter Teilbaum
  87.         printf(data.c_str(),"\n");
  88.         printf("\n");
  89.     if( left  != NULL) left->Print(); // linker Teilbaum
  90.  
  91. }
  92.  
  93. // Datenelement einsortieren
  94. void TreeNode::Insert( std::string data){
  95.  
  96.     if( this->data > data ){
  97.         /* in linken Teilbaum einfuegen*/
  98.         if( left == NULL) left = new TreeNode( data);
  99.         else left->Insert( data);
  100.     }else{
  101.         /* in rechten Teilbaum einfuegen */
  102.         if( right == NULL) right = new TreeNode( data);
  103.         else right->Insert( data);
  104.     }
  105. }
  106.  
  107. //---- Testprogramm für Binaerbaum --------------------------------------
  108. void BuildTree(){
  109.     Tree tree; // Konstruktor wird aufgerufen
  110.     int     i, n = 5;
  111.     std::string     a[] = {"BA","BC","A","BB","E"};
  112.  
  113.     /* Elemente a[i] sortiert in
  114.        den Baum einfuegen */      
  115.     for( i=0; i<n; i++){
  116.         tree.Insert( a[i]);
  117.     }
  118.  
  119.     /* sortierte Liste ausgeben */
  120.     tree.Print();
  121. }
  122.  
  123. int main(){
  124.     BuildTree(); // bei Verlassen wird die Datenstruktur wieder abgebaut
  125.  
  126.     printf("Druecken Sie Enter\n");
  127.     char c;
  128.     scanf("%c", &c); // Ende
  129. }
Advertisement
Add Comment
Please, Sign In to add comment