mirror of
https://github.com/xfarrow/lasd.git
synced 2025-06-05 21:49:14 +02:00
Library 4
This commit is contained in:
@@ -262,7 +262,7 @@ template <typename Data>
|
||||
typename BST<Data>::NodeLnk* const& BST<Data>::FindPointerToMax(struct BST<Data>::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<Data>::NodeLnk* const& BST<Data>::FindPointerTo(struct BST<Data>::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<Data>::NodeLnk* const* BST<Data>::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.
|
||||
|
@@ -328,8 +328,8 @@ void StringFunctions(BST<Data>& bst){
|
||||
std::cout<<" 18. Node-level operations (debug)"<<std::endl;
|
||||
std::cout<<" 19. Go back"<<std::endl;
|
||||
std::cout<<" 20. Quit"<<std::endl;
|
||||
|
||||
cout<<endl<<" -> ";
|
||||
|
||||
std::cin>>std::ws;
|
||||
std::cin>>choice;
|
||||
std::cout<<std::endl;
|
||||
@@ -604,40 +604,49 @@ void RemoveSuccessor(BST<Data>& bst){
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void NodeOperations(T& currentNode){
|
||||
bool NodeOperations(T& currentNode){
|
||||
uint choice;
|
||||
bool wantToExit = false;
|
||||
cout<<endl<<endl;
|
||||
cout<<" *** Element in the current node: "<<currentNode.Element()<<" ***"<<endl;
|
||||
cout<<" 1. Go left"<<endl;
|
||||
cout<<" 2. Go Right"<<endl;
|
||||
cout<<" 3. Is this a leaf?"<<endl;
|
||||
cout<<" 4. Go back"<<endl;
|
||||
cout<<endl<<" -> ";
|
||||
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: "<<currentNode.Element()<<" ***"<<endl;
|
||||
cout<<" 1. Go left"<<endl;
|
||||
cout<<" 2. Go Right"<<endl;
|
||||
cout<<" 3. Is this a leaf?"<<endl;
|
||||
cout<<" 4. Go up"<<endl;
|
||||
cout<<" 5. Go to menu"<<endl;
|
||||
cout<<endl<<" -> ";
|
||||
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<int> GenerateIntegerBST(BST<int>& bst){
|
||||
default_random_engine gen(random_device{}());
|
||||
uniform_int_distribution<int> 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<dim ; ++i){
|
||||
tmp[i] = dist(gen);
|
||||
cout<<tmp[i]<<" ";
|
||||
|
@@ -92,7 +92,7 @@ template <typename Data>
|
||||
void RemoveSuccessor(lasd::BST<Data>&);
|
||||
|
||||
template <typename T>
|
||||
void NodeOperations(T&);
|
||||
bool NodeOperations(T&);
|
||||
|
||||
|
||||
/* ----- generator functions ----- */
|
||||
|
Reference in New Issue
Block a user