small changes and some comments

I puted few comments and changed a little bit.
This commit is contained in:
Christian Bender 2018-03-18 21:49:50 +01:00 committed by GitHub
parent f46a602704
commit b79f0c1214
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 9 deletions

View File

@ -7,17 +7,27 @@
- Search by key value
- Listing of node keys in order of value (from left to right)
*/
// Node, the basic data structure in the tree
typedef struct node{
// left child
struct node* left;
// right child
struct node* right;
// data of the node
int data;
} node;
// The node constructor, which receives the key value input and returns a node pointer
node* newNode(int data){
// creates a slug
node* tmp = (node*)malloc(sizeof(node));
// initializes the slug
tmp->data = data;
tmp->left = NULL;
tmp->right = NULL;
@ -52,7 +62,7 @@ node* getMax(node* root){
// Deletion procedure, which searches for the input key in the tree and removes it if present
node* delete(node* root, int data){
// If the root is null, nothing to be done
if (root == NULL)
if (root == NULL)
return root;
// If the input key is greater than the root's, search in the right subtree
else if (data > root->data)
@ -61,6 +71,7 @@ node* delete(node* root, int data){
else if (data < root->data)
root->left = delete(root->left, data);
// If the input key matches the root's, check the following cases
// termination condition
else if (data == root->data){
// Case 1: the root has no leaves, remove the node
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
else {
// finds the biggest node in the left branch.
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->left = delete(root->left, tmp->data);
}
@ -115,7 +130,7 @@ int height(node* root){
// Get the height from both left and right subtrees to check which is the greatest
int right_h = height(root->right);
int left_h = height(root->left);
// The final height is the height of the greatest subtree(left or right) plus 1(which is the root's level)
if (right_h > left_h)
return (right_h + 1);
@ -145,14 +160,19 @@ void inOrder(node* root){
}
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){
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){
case 1: printf("Enter the new node's value:\n");
scanf("%d",&data);
@ -164,7 +184,7 @@ void main(){
scanf("%d",&data);
root = delete(root,data);
}
else
else
printf("Tree is already empty!\n");
break;
@ -172,7 +192,7 @@ void main(){
scanf("%d",&data);
find(root,data) ? printf("The value is in the tree.\n") : printf("The value is not in the tree.\n");
break;
case 4: printf("Current height of the tree is: %d\n", height(root));
break;
@ -181,5 +201,6 @@ void main(){
}
}
// deletes the tree from the heap.
purge(root);
}