Napisałem drzewo bst i próbuję przekształcić je, aby zwracało mi wysokość tego drzewa, ale występuję błąd już dla małych wysokości, a co dopiero jak według zadania próbuję dla 20000 liczb.
#include<stdlib.h>
#include<stdio.h>
#include<chrono>
typedef struct Node
{
int key;
Node* left;
Node* right;
Node* father;
};
void print(Node* node)
{
if(node != NULL)
{
printf(" Key:%d",node->key);
if (node->left != NULL)
{
printf("L(%d)",node->left->key);
}
if (node->right != NULL)
{
printf("R(%d)",node->right->key);
}
print(node->left);
print(node->right);
}
}
void printNode(Node* node)
{
printf("Key:%d",node->key);
if (node->father == NULL)
printf(" Father:NULL");
else
printf(" Father:%d",node->father->key);
if (node->left == NULL)
printf(" Left:NULL");
else
printf(" Left:%d",node->left->key);
if (node->right == NULL)
printf(" Right:NULL");
else
printf(" Right:%d",node->right->key);
}
void add(Node** newNode, Node** head)
{
(*newNode)->left=NULL;
(*newNode)->right=NULL;
(*newNode)->father=NULL;
if(*head == NULL)
{
*head = *newNode;
}
else
{
Node* actualNode = *head;
while((*newNode)->father == NULL)
{
if((*newNode)->key<actualNode->key)
{
if(actualNode->left == NULL)
{
actualNode->left = *newNode;
(*newNode)->father = actualNode;
}
actualNode = actualNode->left;
}
if((*newNode)->key>actualNode->key)
{
if(actualNode->right == NULL)
{
actualNode->right = *newNode;
(*newNode)->father = actualNode;
}
actualNode = actualNode->right;
}
}
}
}
Node* treeMinimum(Node* node)
{
while(node->left != NULL)
{
node=node->left;
}
return node;
}
Node* treeSuccessor(Node* node)
{
if(node->right != NULL)
return treeMinimum(node->right);
Node* actualNode = node->father;
while (actualNode != NULL && node->key == actualNode->right->key)
{
node = actualNode;
actualNode = actualNode->father;
}
return actualNode;
}
Node* find(int key, Node** head)
{
Node* node = *head;
while (node->key != key)
if(key < node->key)
node = node->left;
else
node = node->right;
return node;
}
void remove(int key, Node** head)
{
Node* nodeToRemove = find(key,head);
Node* nodeToOmit;
if(nodeToRemove->left == NULL || nodeToRemove->right == NULL)
nodeToOmit=nodeToRemove;
else
nodeToOmit=treeSuccessor(nodeToRemove);
Node* childNode;
if(nodeToOmit->left != NULL)
childNode = nodeToOmit->left;
else
childNode = nodeToOmit->right;
if (childNode != NULL)
childNode->father=nodeToOmit->father;
if (nodeToOmit->father == NULL)
*head = childNode;
else
{
if (nodeToOmit->father->left!=NULL && nodeToOmit->key == nodeToOmit->father->left->key)
(nodeToOmit->father)->left = childNode;
else
(nodeToOmit->father)->right = childNode;
}
if(nodeToOmit->key != key)
nodeToRemove->key = nodeToOmit->key;
}
void printSort(Node* head)
{
if (head != NULL)
{
printSort(head->left);
printf("%d ",head->key);
printSort(head->right);
}
}
int main()
{
Node* head=NULL;
int key = 1;
for(;;)
{
printf("New key: ");
scanf("%d",&key);
if(key==-1)
break;
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->key=key;
add(&newNode,&head);
}
print(head);
return 0;
}
Czy ktoś podpowie mi co przekształcić, aby dobrze zwracało wysokość?