From 584f7c3003ec82b1d4a8129976733f2bbb3d87e5 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Wed, 19 May 2021 10:58:54 +0200 Subject: [PATCH] Library 4 --- librerie/exercise4/bst/bst.cpp | 31 ++++++++----- librerie/exercise4/zmytest/test.cpp | 71 ++++++++++++++++------------- librerie/exercise4/zmytest/test.hpp | 2 +- 3 files changed, 60 insertions(+), 44 deletions(-) diff --git a/librerie/exercise4/bst/bst.cpp b/librerie/exercise4/bst/bst.cpp index 3cf96fb..db91a14 100755 --- a/librerie/exercise4/bst/bst.cpp +++ b/librerie/exercise4/bst/bst.cpp @@ -262,7 +262,7 @@ template typename BST::NodeLnk* const& BST::FindPointerToMax(struct BST::NodeLnk* const& node) const noexcept{ NodeLnk* const* ptr = &node; NodeLnk* curr = node; - if(curr!=nullptr){ + if(curr!=nullptr){ while(curr->right != nullptr){ ptr = &curr->right; curr = curr->right; @@ -283,21 +283,28 @@ typename BST::NodeLnk* const& BST::FindPointerTo(struct BST::N declare a variable (pointer) which points to a const pointer which points to a NodeLnk. This const pointer that points to a NodeLnk is the parameter of the function. + Hence, (*pointer) will be a const pointer. - Hence, *pointer will be a const pointer. + Note: this function (and others too) could've been written using one single pointer, in this way: + while(*pointer != nullptr && (*(*pointer)).Element() != data){ + if( (*(*pointer)).Element() < data ) pointer = &((*pointer)->right); + else if((*(*pointer)).Element() > data ) pointer = &((*pointer)->left); + } + but I preferred to use a clearer version. */ + NodeLnk* const* pointer = &ref; //a pointer to a const pointer to a NodeLnk NodeLnk* current = ref; - while(current != nullptr && current->Element() != data){ - if(current->Element() < data){ - pointer = &(current->right); - current = current->right; - }else if(current->Element() > data){ - pointer = &(current->left); - current = current->left; - } + while(current != nullptr && current->Element() != data){ + if(current->Element() < data){ + pointer = &(current->right); + current = current->right; + }else if(current->Element() > data){ + pointer = &(current->left); + current = current->left; } + } return *pointer; } @@ -307,10 +314,10 @@ typename BST::NodeLnk* const* BST::FindPointerToPredecessor(struct B /* If the element we are looking the predecessor for is the current element, then its predecessor resides in the max node of its left subtree (if it has - a left subtree. Return the lastRight otherwise). + a left subtree. Return the candidate otherwise). If the element we are looking the predecessor for is greater than the current element, - then we have to go down right the tree, saving the current "lastRight". + then we have to go down right the tree, saving the current "candidate". If the element we are looking the predecessor for is less than the current element, then we have to go down left the tree. diff --git a/librerie/exercise4/zmytest/test.cpp b/librerie/exercise4/zmytest/test.cpp index 997cc6f..87a6179 100755 --- a/librerie/exercise4/zmytest/test.cpp +++ b/librerie/exercise4/zmytest/test.cpp @@ -328,8 +328,8 @@ void StringFunctions(BST& bst){ std::cout<<" 18. Node-level operations (debug)"< "; + std::cin>>std::ws; std::cin>>choice; std::cout<& bst){ } template -void NodeOperations(T& currentNode){ +bool NodeOperations(T& currentNode){ uint choice; + bool wantToExit = false; cout< "; - cin>>ws; - cin>>choice; - switch(choice){ - case 1: - if(currentNode.HasLeftChild()) NodeOperations(currentNode.LeftChild()); - else{ - cout<<"\nThe node does not have a left child, operation aborted."; - NodeOperations(currentNode); - } - break; + do{ + cout<<" *** Element in the current node: "< "; + cin>>ws; + cin>>choice; + switch(choice){ + case 1: + if(currentNode.HasLeftChild()){ + wantToExit = NodeOperations(currentNode.LeftChild()); + } + else{ + cout<<"\nThe node does not have a left child, operation aborted.\n"; + } + break; - case 2: - if(currentNode.HasRightChild()) NodeOperations(currentNode.RightChild()); - else { - cout<<"\nThe node does not have a right child, operation aborted."; - NodeOperations(currentNode); - } - break; + case 2: + if(currentNode.HasRightChild()){ + wantToExit = NodeOperations(currentNode.RightChild()); + } + else { + cout<<"\nThe node does not have a right child, operation aborted.\n"; + } + break; - case 3: - if(currentNode.IsLeaf()) cout<<"\n Yes, the current node is a leaf"; - else cout<<"\n No, the current node is not a leaf"; - NodeOperations(currentNode); - break; + case 3: + if(currentNode.IsLeaf()) cout<<"\n Yes, the current node is a leaf\n"; + else cout<<"\n No, the current node is not a leaf\n"; + break; + + case 4: + return false; } + }while(choice!=5 && !wantToExit); + return true; } @@ -650,7 +659,7 @@ BST GenerateIntegerBST(BST& bst){ default_random_engine gen(random_device{}()); uniform_int_distribution dist(0,1000); - cout<<"\n\nElements in the binary tree (in breadth order):\n"; + cout<<"\n\nElements in the binary tree (in order of insertion):\n"; for(ulong i=0 ; i void RemoveSuccessor(lasd::BST&); template -void NodeOperations(T&); +bool NodeOperations(T&); /* ----- generator functions ----- */