I have made a working binary tree.
Now I want when I press option # 4, it'll show binary tree is sorted according to the array (that means the middle should be root, the left part of the left subtree, then the right part of the field to the right subtree).
I have created functions required to do what it should be done but still it does not work as it should.
I have created NodSize(), it controls how many nodes are in the tree.
I have created balanceTree(), it takes all that is in the root.
I have created ArrayInorder(),
And Balance(), it makes balancing the tree according to array.
Where did I do wrong and how can I solve it?
..
You can see down here how I made it:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define MAX 100
struct tree
{
int data;
struct tree *left;
struct tree *right;
};
struct tree *CreateNode(int data)
{
struct tree *node = (struct tree*) malloc(sizeof(struct tree));
if (node != NULL)
{
node->data = data;
node->left = NULL;
node->right = NULL;
}
return node;
}
struct tree *insert(struct tree *root, int data)
{
if (root == NULL)
{
root = CreateNode(data);
}
if (root->data > data)
{
if (root->left == NULL)
{
root->left = CreateNode(data);
}
else
{
insert(root->left, data);
}
}
else if (root->data < data)
{
if (root->right == NULL)
{
root->right = CreateNode(data);
}
else
{
insert(root->right, data);
}
}
return root;
}
struct tree *delet(struct tree *ptr, int x)
{
struct tree *p1, *p2;
if (!ptr)
{
printf("\n NOTHING ");
return(ptr);
}
else
{
if (ptr->data < x)
{
ptr->right = delet(ptr->right, x);
}
else if (ptr->data > x)
{
ptr->left = delet(ptr->left, x);
return ptr;
}
else
{
if (ptr->data == x)
{
if (ptr->left == ptr->right)
{
free(ptr);
return(NULL);
}
else if (ptr->left == NULL)
{
p1 = ptr->right;
free(ptr);
return p1;
}
else if (ptr->right == NULL)
{
p1 = ptr->left;
free(ptr);
return p1;
}
else
{
p1 = ptr->right;
p2 = ptr->right;
while (p1->left != NULL)
p1 = p1->left;
p1->left = ptr->left;
free(ptr);
return p2;
}
}
}
}
return(ptr);
}
void Findroot(struct tree *root)
{
if (root != NULL)
{
printf("\nRoot is %d\n", root->data);
}
else
{
printf("\nNOTHING\n");
}
}
void inorder(struct tree *root)
{
if (root != NULL)
{
inorder(root->left);
printf(" %d", root->data);
inorder(root->right);
}
return;
}
int NodSize(struct tree *root)
{
if (root == NULL)
return 0;
else
return (NodSize(root->left) + 1 + NodSize(root->right));
}
void DestoryTree(struct tree * root)
{
if (root == NULL)
return;
DestoryTree(root->left);
DestoryTree(root->right);
printf("\n Destory node: %d", root->data);
free(root);
}
struct tree *Balance(int arr[], int start, int end)
{
if (start > end)
return NULL;
int mid = (start + end) / 2;
struct tree *root = CreateNode(arr[mid]);
root->left = Balance(arr, start, mid - 1);
root->right = Balance(arr, mid + 1, end);
return root;
}
int ArrayInorder(struct tree *root, int *arr, int helper)
{
if (root != NULL)
{
helper = ArrayInorder(root->left, arr, helper);
arr[helper] = root->data;
helper++;
helper = ArrayInorder(root->right, arr, helper);
}
return helper;
}
void balanceTree(struct tree *root)
{
int *arr, size;
size = NodSize(root);
arr = (int*)malloc(sizeof(int)*size);
ArrayInorder(root, arr, 0);
//DestoryTree(root);
root = Balance(arr, 0, size - 1);
}
void CreateBalancedTreeFromArray(struct tree *root, int *arr, int size)
{
DestoryTree(root);
root = Balance(arr, 0, size - 1);
}
int main(void)
{
struct tree *root;
int valja, item, dele;
root = NULL;
do
{
do
{
printf("\n1. Add a node to BINARY TREE ");
printf("\n2. Delete a node from BINARY TREE ");
printf("\n3. Show ROOT");
printf("\n4. Show Balanced Tree IN (Array)");
printf("\n5. Stang");
printf("\nYour choice? : ");
scanf(" %d", &valja);
if (valja<1 || valja>5)
printf("\n Fel - Try again");
} while (valja<1 || valja>5);
switch (valja)
{
case 1:
system("cls");
printf("\n Write a new element: ");
scanf("%d", &item);
root = insert(root, item);
printf("\n root is %d \n", root->data);
printf("INORDER: ");
inorder(root);
printf("\n");
break;
case 2:
system("cls");
printf("\n Write an element to delete: ");
scanf(" %d", &dele);
root = delet(root, dele);
printf("\n");
break;
case 3:
system("cls");
Findroot(root);
printf("\n");
break;
case 4:
balanceTree(root);
Findroot(root);
inorder(root);
break;
default:
printf("\n bye ");
}
} while (valja != 5);
return(0);
}
The problem is here:
void balanceTree(struct tree *root)
Although this function works, the new root that is created is NOT returned to the caller because root has been passed by value. You need to change the function to:
struct tree *balanceTree(struct tree *root)
and add this at the end:
return root;
Call the function with:
root = balanceTree(root);
Related
Below is the menu driven program for various operation in BST. The major problem I am getting is when I free function runs in delete function please help and update the given program. Whenever I try to delete any node after insertion, the program abruptly ends. I tried to debug but the problem still persists.
#include <stdio.h>
#include <stdlib.h>
struct btNode
{
int data;
struct btNode *right;
struct btNode *left;
};
int found;
struct btNode *temp2;
struct btNode *create(int);
struct btNode *insert(struct btNode *, int);
void inorder(struct btNode *);
void preorder(struct btNode *);
void postorder(struct btNode *);
void delete (struct btNode *, int);
int main()
{
int choice, item;
struct btNode *root = NULL;
do
{
printf("\nChoose one of the options:\n");
printf("1. Insert 2. Delete 3. Inorder 4. Postorder 5. Preorder 6. Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter any number to insert:");
scanf("%d", &item);
root = insert(root, item);
break;
case 2:
printf("\nEnter any number to delete:");
scanf("%d", &item);
delete(root, item);
break;
case 3:
inorder(root);
break;
case 4:
postorder(root);
break;
case 5:
preorder(root);
break;
case 6:
break;
default:
printf("\nWRONG INPUT");
}
} while (choice != 6);
return 0;
}
struct btNode *create(int num)
{
struct btNode *temp1 = (struct btNode *)malloc(sizeof(struct btNode));
temp1->data = num;
temp1->left = NULL;
temp1->right = NULL;
return temp1;
}
struct btNode *search(struct btNode *root, int num)
{
struct btNode *temp1 = root;
while (temp1 != NULL)
{
if (temp1->data == num)
{
found = 1;
return temp1;
}
else
{
if (temp1->data >= num)
{
temp2 = temp1;
temp1 = temp1->left;
}
else
{
temp2 = temp1;
temp1 = temp1->right;
}
}
}
found = 0;
}
struct btNode *insert(struct btNode *root, int num)
{
struct btNode *temp1 = create(num);
if (root == NULL)
{
root = temp1;
printf("%d inserted\n", root->data);
}
else
{
temp2 = root;
while (temp2 != NULL)
{
if (temp2->data >= num)
{
if (temp2->left)
{
temp2 = temp2->left;
}
else
{
temp2->left = temp1;
printf("%d inserted\n", temp2->left->data);
break;
}
}
else
{
if (temp2->right)
{
temp2 = temp2->right;
}
else
{
temp2->right = temp1;
printf("%d inserted\n", temp2->right->data);
break;
}
}
}
}
return root;
}
void delete (struct btNode *root, int num)
{
struct btNode *temp1 = search(root, num);
if (found == 0)
{
printf("element not found");
return;
}
else
{
if (temp1->left == NULL && temp1->right == NULL)
{
free(temp1);
}
if (temp1->left != NULL && temp1->right == NULL)
{
if (temp2->left = temp1)
{
temp2->left = temp1->left;
free(temp1);
}
if (temp2->right = temp1)
{
temp2->right = temp1->left;
free(temp1);
}
}
if (temp1->left == NULL && temp1->right != NULL)
{
if (temp2->left = temp1)
{
temp2->left = temp1->right;
free(temp1);
}
if (temp2->right = temp1)
{
temp2->right = temp1->right;
free(temp1);
}
}
if (temp1->left != NULL && temp1->right != NULL)
{
struct btNode *node1 = temp1;
struct btNode *node2 = temp1->right;
while (node2->left != NULL)
{
node1 = node2;
node2 = node2->left;
}
temp1->data = node2->data;
temp1 = node2;
free(temp1);
}
}
}
void inorder(struct btNode *r)
{
if (r == NULL)
{
printf("Tree is empty");
return;
}
else
{
if (r->left)
inorder(r->left);
printf("%d ", r->data);
if (r->right)
inorder(r->right);
}
}
void preorder(struct btNode *r)
{
if (r == NULL)
{
printf("Tree is empty");
return;
}
else
{
printf("%d ", r->data);
if (r->left)
preorder(r->left);
if (r->right)
preorder(r->right);
}
}
void postorder(struct btNode *r)
{
if (r == NULL)
{
printf("Tree is empty");
return;
}
else
{
if (r->left)
postorder(r->left);
if (r->right)
postorder(r->right);
printf("%d ", r->data);
}
}
Your delete() function has errors at several levels. Among these:
general API: deletion can change the tree's root node, but you make no provision for that. At minimum deletion changes the root node when the only node of a one-node tree is deleted, but with an approach such as yours, the root node will change whenever you delete the value that it contains.
Implementation flaw: in a similar vein, when you perform a search and discover the target value in the root node, you do not set the (very poorly named) temp2 file-scope variable. When that search is performed in the context of a deletion, it may result in either delete() attempting to dereference a null pointer, or in it corrupting the tree by modifying a random node as if it were the parent on the target node.
Implementation flaw: when you delete a leaf node (other than the root), you need to set its parent's pointer to it to NULL.
Bad code: you have several if statements in delete() that use = where they mean ==.
Poor style: do not rely on global variables (temp2, found). Where you want your functions to return multiple values then either use structures or endow the functions with out parameters.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdbool.h>
struct node
{
struct node *left;
int data;
struct node *right;
};
struct node *fnode = NULL;
void insert(int val)
{
struct node *ptr = (struct node *) malloc(sizeof(struct node));
struct node *parentptr, *nodeptr;
ptr->data = val;
ptr->left = NULL;
ptr->right = NULL;
if(fnode == NULL)
{
fnode = ptr;
}
else
{
parentptr = NULL;
nodeptr = fnode;
while(nodeptr!=NULL)
{
parentptr = nodeptr;
if(val<nodeptr->data)
nodeptr = nodeptr->left;
else
nodeptr = nodeptr->right;
}
if(val<parentptr->data)
parentptr->left = ptr;
else
parentptr->right = ptr;
}
}
void pre_order(struct node *tree)
{
if(tree!=NULL)
{
printf("%i ",tree->data);
if(tree->left)
pre_order(tree->left);
if(tree->right)
pre_order(tree->right);
}
}
void in_order(struct node *tree)
{
if(tree)
{
in_order(tree->left);
printf("%i ",tree->data);
in_order(tree->right);
}
}
void post_order(struct node *tree)
{
if(tree)
{
post_order(tree->left);
post_order(tree->right);
printf("%i ",tree->data);
}
}
struct node *smallest_element(struct node *tree)
{
if(tree==NULL || tree->left == NULL)
return tree;
else
return smallest_element(tree->left);
}
struct node *largest_element(struct node *tree)
{
if(tree== NULL || tree->right==NULL)
return tree;
else
{
return largest_element(tree->right);
}
}
struct node *delete_element(struct node *tree, int val)
{
// some declarations
struct node *parent, *child;
child = parent = tree;
bool doesnt_exist = false;
// finding the node to be deleted and its corresponding parent
while(child->data!=val)
{
parent = child;
if(val < child->data)
child = child->left;
else
child = child->right;
if(child==NULL)
{
doesnt_exist = true;
break;
}
}
// if no node exists, print not found
if(doesnt_exist)
{
printf("\nThe node does not exist in the BST.");
return tree;
}
// if node exists
else
{
//if parent node is to be deleted
if(child==parent)
{
//if only parent node exists in the tree
if(child->left == NULL && child->right == NULL)
{
free(child);
tree = NULL;
return tree;
}
// deleting the parent node and replacing it by left-subtree's highest element
struct node *temp = tree->left;
struct node *pre_temp = tree->left;
//searching for left subtree's max element and its parent
while(temp->right!=NULL)
{
pre_temp = temp;
temp = temp->right;
}
//changing the deleted node's value with left subtree's highest element
child->data = temp->data;
//making the ex-position of changed element point to null
if(pre_temp->right == temp)
{
pre_temp->right = NULL;
}
else
{
pre_temp->left = NULL;
}
//freeing the ex-position of changed element.
free(temp);
}
else
{
// if the node to be deleted has 2 nodes
if(child->left && child->right)
{
//some declarations
struct node *temp = child->left;
struct node *pretemp = child;
//browsing to the maximum node on left subtree
while(temp->right!=NULL)
{
pretemp = temp;
temp = temp->right;
}
//exchanging left subtree's max with element to be deleted
child->data = temp->data;
//making the temp's parent point to null
if(pretemp->left == temp)
pretemp->left = NULL;
else
pretemp->right = NULL;
//freeing temp;
free(temp);
}
//if the node to be deleted has only right node
else if(child->left == NULL && child->right)
{
if(parent->left==child)
{
parent->left = child->right;
}
else
{
parent->right = child->right;
}
free(child);
return tree;
}
// if the node to be deleted has only left node
else if(child->right==NULL && child->left)
{
if(parent->left==child)
{
parent->left = child->left;
}
else
{
parent->right = child->left;
}
free(child);
return tree;
}
//if the node to be deleted has no children
else
{
if(parent->left == child)
parent->left = NULL;
else
parent->right = NULL;
free(child);
return tree;
}
}
}
}
int main()
{
int decision;
struct node *ptr;
int ip;
do
{
printf("\n ********MAIN MENU********");
printf("\n 1. Insert element");
printf("\n 2. Pre-order Traversal");
printf("\n 3. In-order Traversal");
printf("\n 4. Post-order Traversal");
printf("\n 5. Find smallest element");
printf("\n 6. Find largest element");
printf("\n 7. Delete an element");
printf("\n 8. Exit");
printf("\nEnter your choice:");
scanf("%i",&decision);
switch(decision)
{
case 1:
printf("\nEnter the element to be inserted:");
scanf("%i",&ip);
insert(ip);
break;
case 2:
if(fnode)
{
printf("\nThe pre-order traversal is as follows:\n");
pre_order(fnode);
}
else
{
printf("\nThe BST is empty!!!");
}
break;
case 3:
if(fnode)
{
printf("\nThe in-order traversal is as follows:\n");
in_order(fnode);
}
else
{
printf("\nThe BST is empty!!!");
}
break;
case 4:
if(fnode)
{
printf("\nThe post-order traversal is as follows:\n");
post_order(fnode);
}
else
{
printf("\nThe BST is empty!!!");
}
break;
case 5:
ptr = smallest_element(fnode);
if(ptr)
{
printf("\nThe smallest element is: %i",ptr->data);
}
else
{
printf("\nThe BST is empty!!!");
}
break;
case 6:
ptr = largest_element(fnode);
if(ptr)
{
printf("\nThe largest element is: %i",ptr->data);
}
else
{
printf("\nThe BST is empty!!!");
}
break;
case 7:
printf("\nEnter the element you want to delete:");
scanf("%i",&ip);
fnode = delete_element(fnode,ip);
break;
}
}
while(decision!=8);
return 0;
}
After I delete a node which had two child nodes, the resultant BST is formed properly (I verified it through debug tools). I have added all the possible cases for deleting a node from BST (has 0/1/2 child nodes and I have handled everything).
But when I make it to print the preorder/postorder/inorder traversal, the program just exits. Can u suggest me why is it so?
I'm sorry for asking too much, so this is almost the same with my previous questions.
I have a balancing binary tree code with deletion function, so the problem is in my searchdelete() function. I don't know why it won't return the root back. So for example
5, 4, 3, 2, 1
And then i input on the menu "Data to be deleted: 1"
it goes to the searchdelete(); function, and then it successfully
shows that the data is found, found = 1
but after that, it just won't return the root address. I have no idea why it won't return.
So in the Delete(); function, it wont print the printf("root after = %d", root->data)
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>
struct node{
int data, balance;
struct node *left, *right;
};
int insert(struct node **root, struct node **curr, int data){
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode -> data = data;
newNode -> left = NULL;
newNode -> right = NULL;
newNode -> balance = 0;
if((*root) == NULL){
(*root) = (*curr) = newNode;
(*root) -> left = NULL;
(*root) -> right = NULL;
return 0;
} else {
if((*curr)->left == NULL && (*curr)->balance == 0){
(*curr) -> balance = (*curr) -> balance - 1;
(*curr) -> left = newNode;
return 0;
} else if ((*curr)->right == NULL && (*curr)->balance == -1){
(*curr) -> balance = (*curr) -> balance + 1;
(*curr) -> right = newNode;
return 0;
} else if ((*curr)->balance == 0 && (*curr)->left->balance == 0){
(*curr) -> balance = (*curr) -> balance - 1;
(*curr) = (*curr)->left;
return insert(root,curr,data);
} else if ((*curr)->balance < 0 && (*curr)->left->balance < 0){
(*curr) -> balance = (*curr) -> balance - 1;
(*curr) = (*curr) -> left;
return insert(root,curr,data);
} else if ((*curr)->balance < 0 && (*curr)->left->balance == 0){
(*curr) -> balance = (*curr) -> balance + 1;
(*curr) = (*curr)->right;
return insert(root, curr, data);
}
}
}
void preorder(struct node *root){
if(root == NULL) return;
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(struct node *root){
if(root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
void inorder(struct node *root){
if(root == NULL) return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
void search(struct node *root, int *key, int *found){
if(root == NULL) return;
search(root->left, key, found);
if(root->data == *key){
*found = 1;
return ;
}
search(root->right, key, found);
}
struct node *findMin(struct node *root){
while(root->left != NULL) root = root->left;
return root;
}
struct node *searchdelete(struct node *root, int data){
if(root == NULL) return root; //This is the searchdelete
searchdelete(root->left, data);
if(root->data == data){
printf("found = %d", root->data);
return root;
}
searchdelete(root->right, data);
}
struct node *Delete(struct node *root, int data){
printf("root before = %d\n", root->data);
if(root == NULL) return root;
else if(data != root->data) {
root = searchdelete(root, data);
printf("root after = %d\n", root->data); //this is where it won't print
system("pause");
}
else{
//Case 1: no child / leaf node
if(root->left == NULL && root->right == NULL){
printf("NULL\n");
free(root);
root = NULL;
}
//Case 2: one child, left or right
else if(root->left == NULL){
printf("left null\n");
struct node *temp = root;
root = root->right;
free(temp);
} else if (root->right == NULL){
printf("right null\n");
struct node *temp = root;
root = root->left;
free(temp);
}
//Case 3: two children
else{
printf("two children \n");
if(root->right->data > root->data){
struct node *temp = root;
root = root->right;
free(temp);
} else {
struct node *temp = root;
root = root->left;
free(temp);
}
}
}
system("pause");
return root;
}
int main(){
struct node *root, *curr;
int choice, data, key, found, delKey;
root = curr = NULL;
while(1){
found = 0;
printf("Balanced Binary Tree Menu\n");
printf("1. Insert Data\n");
printf("2. View on pre order\n");
printf("3. View on post order\n");
printf("4. View on in order\n");
printf("5. Search\n");
printf("6. Delete\n");
printf("7. Exit\n");
printf("Pilihan: ");scanf("%d", &choice);fflush(stdin);
if(choice == 1){
printf("Enter data : "); scanf("%d", &data);
curr = root;
insert(&root, &curr, data);
} else if (choice == 2){
preorder(root);
system("pause");
} else if (choice == 3){
postorder(root);
system("pause");
} else if (choice == 4){
inorder(root);
system("pause");
} else if (choice == 5){
printf("Search: "); scanf("%d", &key);
search(root, &key, &found);
if(found == 1){
printf("Data found !\n");
} else {
printf("Data not found !\n");
}
system("pause");
} else if (choice == 6){
printf("Enter data to be deleted: "); scanf("%d", &delKey);
Delete(root, delKey);
} else if (choice == 7){
return 1;
}
system("cls");
}
return 0;
}
Why does my binary tree search function not return the root adress?
I have no idea why it won't return.
because 2 return are missing
Can be:
struct node * searchdelete(struct node *root, int data){
if(root == NULL)
return root;
if(root->data == data){
printf("found = %d", root->data); /* debug, to be removed */
return root;
}
if ((root = searchdelete(root->left, data)) != NULL)
return root;
return searchdelete(root->right, data);
}
In search why key is an int* rather than just an int like for searchdelete ? For me he function search is useless, searchdelete is already enough to know if the key is present in the tree just calling it and comparing its return value with NULL. So better to remove search and to rename searchdelete to be search (or find)
In both functions you search without using the fact the tree can be sorted, I mean on the left the data are less, on the right they are greater, that improves the performance
There is no interest at all to have a binary tree if the elements are randomly placed in
Your definition of Delete has a lot of problems, including undefined behavior because you free cells without removing them from the tree
Im working on a program in C that reads a binary tree from a file and opperates functions on it:insert elements,delete elements,search elements,display elements
With my old debugger it seems that i have 4 warnings and i dont know kow to fix them
Here is the code
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct bst
{
double number;
struct bst *leftChild;
struct bst *rightChild;
} node;
node *root = NULL;
void insertNode(double value);
void preOrderPrint(node *root);
node * findMinimum(node *root);
node * deleteNode(node *root, double value);
node* search(node ** tree, double val);
int main()
{
char line[255];
char* endptr;
FILE* f = fopen("tree.txt", "r");
while (fgets(line, sizeof(line), f))
{
if (*line!='*')
{
if(strtod (line, &endptr))
insertNode(strtod (line, &endptr));
else if (strtoimax(line,&endptr,0))
insertNode((strtoimax(line,&endptr,0)));
else
insertNode( strtol (line, &endptr,0));
}
}
fclose(f);
int flag = 0, key;
int choice = 0;
do
{
printf("\nEnter your choice:\n1. Display tree\n2. Insert node\n3. Delete node\n4. Exit\nChoice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("In Order Display\n");
preOrderPrint(root);
break;
case 2:
printf("Enter key to insert: ");
scanf("%d",&key);
flag = search(&root,key);
if (flag)
{
printf("Key found in tree,cannot insert\n");
}
else
{
printf("Node %d was inserted \n",key);
insertNode(key);
preOrderPrint(root);
}
break;
case 3:
printf("Enter key to delete: ");
scanf("%d", (int*)&key);
flag = search(&root, key);
if (!flag)
{
printf("Key not found in tree,cannot delete\n");
}
else
{
printf("Node %d was deleted \n",key);
deleteNode(root,key);
preOrderPrint(root);
}
break;
case 4:
printf("Memory Cleared\nPROGRAM TERMINATED\n");
break;
default: printf("Not a valid input, try again\n");
}
}
while (choice != 5);
return 0;
}
node * deleteNode(node *currentNode, double value)
{
if(currentNode==NULL) // empty tree
return NULL;
else if(value < currentNode->number) // value is less than node's number. so go to left subtree
currentNode->leftChild = deleteNode(currentNode->leftChild, value);
else if(value > currentNode->number) // value is greater than node's number. so go to right subtree
currentNode->rightChild = deleteNode(currentNode->rightChild, value);
else // node found. Let's delete it!
{
//node has no child
if(currentNode->leftChild == NULL && currentNode->rightChild == NULL)
{
currentNode = NULL;
}
else if(currentNode->leftChild == NULL) // node has only right child
{
currentNode = currentNode->rightChild;
}
else if(currentNode->rightChild == NULL) // node has only left child
{
currentNode = currentNode->leftChild;
}
else // node has two children
{
node *tempNode = findMinimum(currentNode->rightChild);
currentNode->number = tempNode->number;
currentNode->rightChild = deleteNode(currentNode->rightChild, tempNode->number);
}
}
return currentNode;
}
node * findMinimum(node *currentNode)
{
if(currentNode->leftChild==NULL)
return currentNode;
return findMinimum(currentNode->leftChild);
}
void insertNode(double value)
{
node *tempNode;
node *currentNode;
node *parentNode;
tempNode = (node *) malloc(sizeof(node));
tempNode->number = value;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
//For the very first call
if(root==NULL)
{
root = tempNode;
}
else
{
currentNode = root;
parentNode = NULL;
while(1)
{
parentNode = currentNode;
if(value <= parentNode->number)
{
currentNode = currentNode->leftChild;
if(currentNode==NULL)
{
parentNode->leftChild = tempNode;
return;
}
}
else
{
currentNode = currentNode->rightChild;
if(currentNode==NULL)
{
parentNode->rightChild = tempNode;
return;
}
}
}
}
}
void preOrderPrint(node *root)
{
if(root==NULL)
return;
preOrderPrint(root->leftChild);
printf("%g ", root->number);
preOrderPrint(root->rightChild);
}
node* search(node ** tree, double val)
{
if(!(*tree))
{
return NULL;
}
if(val < (*tree)->number)
{
search(&((*tree)->leftChild), val);
}
else if(val > (*tree)->number)
{
search(&((*tree)->rightChild), val);
}
else if(val == (*tree)->number)
{
return *tree;
}
//return true;
}
C:\UsersX\Desktop\39\main.c|32|warning: implicit declaration of
function 'strtoimax' [-Wimplicit-function-declaration]|
C:\UsersX\Desktop\39\main.c|56|warning: assignment makes integer from
pointer without a cast [-Wint-conversion]|
C:\UsersX\Desktop\39\main.c|72|warning: assignment makes integer from
pointer without a cast [-Wint-conversion]|
C:\UsersX\Desktop\39\main.c|224|warning: control reaches end of
non-void function [-Wreturn-type]| ||=== Build finished: 0 error(s), 4
warning(s) (0 minute(s), 0 second(s)) ===|
Also the main question would be why i cannot delete the root?
With my debugger it seems that the root can be deleted.
Title explains it all, something weird is going on with my delete function that i cant seem to figure out. I think its getting stuck on the recursion part of the program based off of standard print statements. If anyone can help that would be greatly appreciated
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct btree
{
char name[10];
//int data;
struct btree *rlink;
struct btree *llink;
}node;
void insert(node**, char[]);
void inorder(node*);
void preorder(node*);
void postorder(node*);
int charToNum(node*);
node* findmin(node*);
node* findmax(node*);
node* del(node*, char[]);
int main(void)
{
node *root;
root = (node*)malloc(sizeof(node));
root = NULL;
int ch, x;
x = 0;
char num[10];
while (1)
{
printf("\n 1.insert2.inorder3.preorder4.postorder5.findmin6.findmax7.Delete");
printf("\n Enter your choice>");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter node to insert>");
scanf("%s", &num);
insert(&root, num);
break;
case 2:
printf("\n inorder display");
inorder(root);
break;
case 3:
printf("\n pre-order display");
preorder(root);
break;
case 4:
printf("\n post-order display");
postorder(root);
break;
case 5:
printf("\n Min in the tree is %s", findmin(root)->name);
break;
case 6:
printf("\n Max in the tree is %s", findmax(root)->name);
break;
case 7:
printf("\n enter the node to delete>");
scanf("%s", &num);
root = del(root, num);
break;
case 8:
exit(0);
}
}
}
void insert(node** h, char info[10])
{
if (*h == NULL)
{
node *temp;
temp = (node*)malloc(sizeof(node));
strcpy((temp->name), info);
//temp->name = info;
temp->llink = NULL;
temp->rlink = NULL;
*h = temp;
printf("successfully added %s\n", temp->name);
}
else if (info > (*h)->name) {
insert(&(*h)->rlink, info);
}
else if (info <(*h)->name)
insert(&(*h)->llink, info);
}
void inorder(node* h)
{
if (h != NULL)
{
inorder(h->llink);
printf("%s->", h->name);
inorder(h->rlink);
}
}
void preorder(node* h)
{
if (h != NULL)
{
printf("%s->", h->name);
preorder(h->llink);
preorder(h->rlink);
}
}
void postorder(node* h)
{
if (h != NULL)
{
postorder(h->llink);
postorder(h->rlink);
printf("%s->", h->name);
}
}
node* findmin(node* h)
{
if (h->llink == NULL)
return h;
else
findmin(h->llink);
}
node* findmax(node* h)
{
if (h->rlink == NULL)
return h;
else
findmax(h->rlink);
}
node* del(node *h, char info[])
{
if (h == NULL)
return h;
else if (info > h->name) {
printf("info > h->name\n");
h->rlink = del(h->rlink, info);
}
else if (info < h->name) {
h->llink = del(h->llink, info);
printf("info < h->name\n");
}
else
{
if (h->llink == NULL && h->rlink == NULL)
{
printf("first if excuting");
/*node *tmp;
tmp = (node*)malloc(sizeof(node));
tmp = h;*/
h = NULL;
return h;
}
else if (h->llink == NULL)
{
node *tmp;
tmp = (node*)malloc(sizeof(node));
tmp = h;
h = h->rlink;
free(tmp);
printf("else if finished\n");
}
else if (h->rlink == NULL)
{
printf("second else if is ecuting \n");
node *tmp;
tmp = (node*)malloc(sizeof(node));
tmp = h;
h = h->llink;
free(tmp);
printf("second else if finished\n");
}
else
{
printf("final else is excuting\n");
node *tmp;
tmp = (node*)malloc(sizeof(node));
tmp = findmin(h->rlink);
strcpy((h->name),tmp->name);
//h->name = tmp->name;
h->rlink = del(h->rlink, tmp->name);
free(tmp);
printf("final else finished\n");
}
}
return h;
}