mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
small changes and some comments
I puted few comments and changed a little bit.
This commit is contained in:
parent
f46a602704
commit
b79f0c1214
@ -10,14 +10,24 @@
|
|||||||
|
|
||||||
// Node, the basic data structure in the tree
|
// Node, the basic data structure in the tree
|
||||||
typedef struct node{
|
typedef struct node{
|
||||||
|
|
||||||
|
// left child
|
||||||
struct node* left;
|
struct node* left;
|
||||||
|
|
||||||
|
// right child
|
||||||
struct node* right;
|
struct node* right;
|
||||||
|
|
||||||
|
// data of the node
|
||||||
int data;
|
int data;
|
||||||
} node;
|
} node;
|
||||||
|
|
||||||
// The node constructor, which receives the key value input and returns a node pointer
|
// The node constructor, which receives the key value input and returns a node pointer
|
||||||
node* newNode(int data){
|
node* newNode(int data){
|
||||||
|
|
||||||
|
// creates a slug
|
||||||
node* tmp = (node*)malloc(sizeof(node));
|
node* tmp = (node*)malloc(sizeof(node));
|
||||||
|
|
||||||
|
// initializes the slug
|
||||||
tmp->data = data;
|
tmp->data = data;
|
||||||
tmp->left = NULL;
|
tmp->left = NULL;
|
||||||
tmp->right = NULL;
|
tmp->right = NULL;
|
||||||
@ -61,6 +71,7 @@ node* delete(node* root, int data){
|
|||||||
else if (data < root->data)
|
else if (data < root->data)
|
||||||
root->left = delete(root->left, data);
|
root->left = delete(root->left, data);
|
||||||
// If the input key matches the root's, check the following cases
|
// If the input key matches the root's, check the following cases
|
||||||
|
// termination condition
|
||||||
else if (data == root->data){
|
else if (data == root->data){
|
||||||
// Case 1: the root has no leaves, remove the node
|
// Case 1: the root has no leaves, remove the node
|
||||||
if ((root->left == NULL) && (root->right == NULL)){
|
if ((root->left == NULL) && (root->right == NULL)){
|
||||||
@ -82,7 +93,11 @@ node* delete(node* root, int data){
|
|||||||
}
|
}
|
||||||
// Case 3: the root has 2 leaves, find the greatest key in the left subtree and switch with the root's
|
// Case 3: the root has 2 leaves, find the greatest key in the left subtree and switch with the root's
|
||||||
else {
|
else {
|
||||||
|
|
||||||
|
// finds the biggest node in the left branch.
|
||||||
node* tmp = getMax(root->left);
|
node* tmp = getMax(root->left);
|
||||||
|
|
||||||
|
// sets the data of this node equal to the data of the biggest node (lefts)
|
||||||
root->data = tmp->data;
|
root->data = tmp->data;
|
||||||
root->left = delete(root->left, tmp->data);
|
root->left = delete(root->left, tmp->data);
|
||||||
}
|
}
|
||||||
@ -145,14 +160,19 @@ void inOrder(node* root){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void main(){
|
void main(){
|
||||||
node* root = NULL;
|
|
||||||
int opt = 1;
|
|
||||||
int data;
|
|
||||||
|
|
||||||
|
// this reference don't change.
|
||||||
|
// only the tree changes.
|
||||||
|
node* root = NULL;
|
||||||
|
int opt = -1;
|
||||||
|
int data = 0;
|
||||||
|
|
||||||
|
// event-loop.
|
||||||
while (opt != 0){
|
while (opt != 0){
|
||||||
printf("\n\n[1] Insert Node\n[2] Delete Node\n[3] Find a Node\n[4] Get current Height\n[5] Print Tree in Crescent Order\n[0] Quit\n");
|
printf("\n\n[1] Insert Node\n[2] Delete Node\n[3] Find a Node\n[4] Get current Height\n[5] Print Tree in Crescent Order\n[0] Quit\n");
|
||||||
scanf("%d",&opt);
|
scanf("%d",&opt); // reads the choice of the user
|
||||||
|
|
||||||
|
// processes the choice
|
||||||
switch(opt){
|
switch(opt){
|
||||||
case 1: printf("Enter the new node's value:\n");
|
case 1: printf("Enter the new node's value:\n");
|
||||||
scanf("%d",&data);
|
scanf("%d",&data);
|
||||||
@ -181,5 +201,6 @@ void main(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// deletes the tree from the heap.
|
||||||
purge(root);
|
purge(root);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user