summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19
diff options
context:
space:
mode:
Diffstat (limited to 'Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19')
-rw-r--r--Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Tree.h159
-rw-r--r--Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Treenode.h54
-rw-r--r--Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/fig17_19.cpp76
3 files changed, 289 insertions, 0 deletions
diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Tree.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Tree.h
new file mode 100644
index 0000000..d44276c
--- /dev/null
+++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Tree.h
@@ -0,0 +1,159 @@
+// Fig. 17.18: tree.h
+// Template Tree class definition.
+#ifndef TREE_H
+#define TREE_H
+
+#include <iostream>
+
+using std::endl;
+
+#include <new>
+#include "treenode.h"
+
+template< class NODETYPE >
+class Tree {
+
+public:
+ Tree();
+ void insertNode( const NODETYPE & );
+ void preOrderTraversal() const;
+ void inOrderTraversal() const;
+ void postOrderTraversal() const;
+
+private:
+ TreeNode< NODETYPE > *rootPtr;
+
+ // utility functions
+ void insertNodeHelper(
+ TreeNode< NODETYPE > **, const NODETYPE & );
+ void preOrderHelper( TreeNode< NODETYPE > * ) const;
+ void inOrderHelper( TreeNode< NODETYPE > * ) const;
+ void postOrderHelper( TreeNode< NODETYPE > * ) const;
+
+}; // end class Tree
+
+// constructor
+template< class NODETYPE >
+Tree< NODETYPE >::Tree()
+{
+ rootPtr = 0;
+
+} // end Tree constructor
+
+// insert node in Tree
+template< class NODETYPE >
+void Tree< NODETYPE >::insertNode( const NODETYPE &value )
+{
+ insertNodeHelper( &rootPtr, value );
+
+} // end function insertNode
+
+// utility function called by insertNode; receives a pointer
+// to a pointer so that the function can modify pointer's value
+template< class NODETYPE >
+void Tree< NODETYPE >::insertNodeHelper(
+ TreeNode< NODETYPE > **ptr, const NODETYPE &value )
+{
+ // subtree is empty; create new TreeNode containing value
+ if ( *ptr == 0 )
+ *ptr = new TreeNode< NODETYPE >( value );
+
+ else // subtree is not empty
+
+ // data to insert is less than data in current node
+ if ( value < ( *ptr )->data )
+ insertNodeHelper( &( ( *ptr )->leftPtr ), value );
+
+ else
+
+ // data to insert is greater than data in current node
+ if ( value > ( *ptr )->data )
+ insertNodeHelper( &( ( *ptr )->rightPtr ), value );
+
+ else // duplicate data value ignored
+ cout << value << " dup" << endl;
+
+} // end function insertNodeHelper
+
+// begin preorder traversal of Tree
+template< class NODETYPE >
+void Tree< NODETYPE >::preOrderTraversal() const
+{
+ preOrderHelper( rootPtr );
+
+} // end function preOrderTraversal
+
+// utility function to perform preorder traversal of Tree
+template< class NODETYPE >
+void Tree< NODETYPE >::preOrderHelper(
+ TreeNode< NODETYPE > *ptr ) const
+{
+ if ( ptr != 0 ) {
+ cout << ptr->data << ' '; // process node
+ preOrderHelper( ptr->leftPtr ); // go to left subtree
+ preOrderHelper( ptr->rightPtr ); // go to right subtree
+
+ } // end if
+
+} // end function preOrderHelper
+
+// begin inorder traversal of Tree
+template< class NODETYPE >
+void Tree< NODETYPE >::inOrderTraversal() const
+{
+ inOrderHelper( rootPtr );
+
+} // end function inOrderTraversal
+
+// utility function to perform inorder traversal of Tree
+template< class NODETYPE >
+void Tree< NODETYPE >::inOrderHelper(
+ TreeNode< NODETYPE > *ptr ) const
+{
+ if ( ptr != 0 ) {
+ inOrderHelper( ptr->leftPtr ); // go to left subtree
+ cout << ptr->data << ' '; // process node
+ inOrderHelper( ptr->rightPtr ); // go to right subtree
+
+ } // end if
+
+} // end function inOrderHelper
+
+// begin postorder traversal of Tree
+template< class NODETYPE >
+void Tree< NODETYPE >::postOrderTraversal() const
+{
+ postOrderHelper( rootPtr );
+
+} // end function postOrderTraversal
+
+// utility function to perform postorder traversal of Tree
+template< class NODETYPE >
+void Tree< NODETYPE >::postOrderHelper(
+ TreeNode< NODETYPE > *ptr ) const
+{
+ if ( ptr != 0 ) {
+ postOrderHelper( ptr->leftPtr ); // go to left subtree
+ postOrderHelper( ptr->rightPtr ); // go to right subtree
+ cout << ptr->data << ' '; // process node
+
+ } // end if
+
+} // end function postOrderHelper
+
+#endif
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/ \ No newline at end of file
diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Treenode.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Treenode.h
new file mode 100644
index 0000000..a79cee9
--- /dev/null
+++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Treenode.h
@@ -0,0 +1,54 @@
+// Fig. 17.17: treenode.h
+// Template TreeNode class definition.
+#ifndef TREENODE_H
+#define TREENODE_H
+
+// forward declaration of class Tree
+template< class NODETYPE > class Tree;
+
+template< class NODETYPE >
+class TreeNode {
+ friend class Tree< NODETYPE >;
+
+public:
+
+ // constructor
+ TreeNode( const NODETYPE &d )
+ : leftPtr( 0 ),
+ data( d ),
+ rightPtr( 0 )
+ {
+ // empty body
+
+ } // end TreeNode constructor
+
+ // return copy of node's data
+ NODETYPE getData() const
+ {
+ return data;
+
+ } // end getData function
+
+private:
+ TreeNode< NODETYPE > *leftPtr; // pointer to left subtree
+ NODETYPE data;
+ TreeNode< NODETYPE > *rightPtr; // pointer to right subtree
+
+}; // end class TreeNode
+
+#endif
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/ \ No newline at end of file
diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/fig17_19.cpp b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/fig17_19.cpp
new file mode 100644
index 0000000..3fb8def
--- /dev/null
+++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/fig17_19.cpp
@@ -0,0 +1,76 @@
+// Fig. 17.19: fig17_19.cpp
+// Tree class test program.
+#include <iostream>
+
+using std::cout;
+using std::cin;
+using std::fixed;
+
+#include <iomanip>
+using std::setprecision;
+
+#include "tree.h" // Tree class definition
+
+int main()
+{
+ Tree< int > intTree; // create Tree of int values
+ int intValue;
+
+ cout << "Enter 10 integer values:\n";
+
+ for( int i = 0; i < 10; i++ ) {
+ cin >> intValue;
+ intTree.insertNode( intValue );
+
+ } // end for
+
+ cout << "\nPreorder traversal\n";
+ intTree.preOrderTraversal();
+
+ cout << "\nInorder traversal\n";
+ intTree.inOrderTraversal();
+
+ cout << "\nPostorder traversal\n";
+ intTree.postOrderTraversal();
+
+ Tree< double > doubleTree; // create Tree of double values
+ double doubleValue;
+
+ cout << fixed << setprecision( 1 )
+ << "\n\n\nEnter 10 double values:\n";
+
+ for ( int j = 0; j < 10; j++ ) {
+ cin >> doubleValue;
+ doubleTree.insertNode( doubleValue );
+
+ } // end for
+
+ cout << "\nPreorder traversal\n";
+ doubleTree.preOrderTraversal();
+
+ cout << "\nInorder traversal\n";
+ doubleTree.inOrderTraversal();
+
+ cout << "\nPostorder traversal\n";
+ doubleTree.postOrderTraversal();
+
+ cout << endl;
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/ \ No newline at end of file