From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog2/SortedList/Ref.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Bachelor/Prog2/SortedList/Ref.cpp (limited to 'Bachelor/Prog2/SortedList/Ref.cpp') diff --git a/Bachelor/Prog2/SortedList/Ref.cpp b/Bachelor/Prog2/SortedList/Ref.cpp new file mode 100644 index 0000000..49547a5 --- /dev/null +++ b/Bachelor/Prog2/SortedList/Ref.cpp @@ -0,0 +1,23 @@ +template< class NODETYPE > +void Tree< NODETYPE >::insertNode( const NODETYPE &value ) + { insertNodeHelper( rootPtr, value ); } + +// This function receives a reference to a pointer so the +// pointer itself (not a copy of it!) can be modified. +template< class NODETYPE > +void Tree< NODETYPE >::insertNodeHelper( + TreeNode< NODETYPE >*& ptr, const NODETYPE &value ) +{ + if ( ptr == 0 ) { // tree is empty + ptr = new TreeNode< NODETYPE >( value ); + assert( ptr != 0 ); + } + else // tree is not empty + if ( value < ptr->data ) + insertNodeHelper( ptr->leftPtr, value ); + else + if ( value > ptr->data ) + insertNodeHelper( ptr->rightPtr, value ); + else + cout << value << " dup" << endl; +} \ No newline at end of file -- cgit v1.2.3