This commit is contained in:
Oleksandr Hryshchenko 2021-11-26 00:16:14 +00:00
parent 997d68724f
commit fb0a74044b

View File

@ -40,32 +40,35 @@ struct binary* createBinary(char* name){
* The function recursively iterated through the whole binary tree until it finds a place to add new node to. Once the
* place is found, it created a new node with a given value.
* @param myTree a pointer to the binary tree that the new node should be appended to.
* @param product a name of the product that will be added as a new node.
* @param item a name of the item that will be added as a new node.
* @return NULL if the node was not yet added, pointer to the new node if the node was added.
*/
struct binary* productAppend(struct binary* myTree, char* product){
struct binary* itemAppend(struct binary* myTree, char* item){
if(!myTree || ((myTree->right != NULL && myTree->left != NULL) && (myTree->left->value[0] == '*' && myTree->right->value[0] == '*')))
return NULL;
if(!strcmp(item, "Je to zelene?"))
printf("ti");
struct binary* itrNode = myTree;
while(itrNode->left != NULL){
if(itrNode->left->value[0] != '*')
itrNode = (!productAppend(itrNode->left, product)) ? itrNode : productAppend(itrNode->left, product);
itrNode = (!itemAppend(itrNode->left, item)) ? itrNode : itemAppend(itrNode->left, item);
if(endingFound || !itrNode->right || !itrNode->left)
break;
if(itrNode->right->value[0] != '*')
itrNode = (!productAppend(itrNode->right, product)) ? itrNode : productAppend(itrNode->right, product);
itrNode = (!itemAppend(itrNode->right, item)) ? itrNode : itemAppend(itrNode->right, item);
if(!itrNode)
return NULL;
}
if(!endingFound && !itrNode->left) {
itrNode->left = createBinary(product);
itrNode->left = createBinary(item);
endingFound = true;
return itrNode;
}
else if(!endingFound && !itrNode->right){
itrNode->right = createBinary(product);
itrNode->right = createBinary(item);
endingFound = true;
return itrNode;
}
@ -74,50 +77,6 @@ struct binary* productAppend(struct binary* myTree, char* product){
}
/**
*
* @param myTree a pointer to the binary tree that the new question should be appended to.
* @param question the text of a question to be added as a new node.
* @return a pointer to the newly created node in case function runs successfully, NULL otherwise.
*/
struct binary* questionAppend(struct binary* myTree, char* question){
struct binary* itrNode1 = myTree;
struct binary* itrNode2 = myTree;
while(itrNode1->left && itrNode1->right && itrNode2->left && itrNode2->right){
if(itrNode1->left->value[0] != '*')
itrNode1 = questionAppend(itrNode1->left, question);
if(endingFound)
break;
if(itrNode2->right->value[0] != '*')
itrNode2 = questionAppend(itrNode2->right, question);
}
if(!itrNode1->left) {
itrNode1->left = createBinary(question);
endingFound = true;
return itrNode1;
}
else if(!itrNode1->right){
itrNode1->right = createBinary(question);
endingFound = true;
return itrNode1;
}
else if(!itrNode2->left && !endingFound){
itrNode2->left = createBinary(question);
endingFound = true;
return itrNode2;
}
else if(!itrNode2->right && !endingFound){
itrNode2->right = createBinary(question);
endingFound = true;
return itrNode2;
}
return NULL;
}
int main() {
char* usrIn = (char*)calloc(120, sizeof(char));
struct binary* binaryTree;
@ -138,7 +97,7 @@ int main() {
if(usrIn[0] == '*')
inputsAmount++;
if(strlen(usrIn) > 1)
productAppend(itrNode, usrIn);
itemAppend(itrNode, usrIn);
endingFound = false;
}
}