Compare commits
No commits in common. "69a4647736d723e7e94119c149bf12db15c15c1d" and "80a9ea67b17579ac6510f6e3f695553b7ffa1a1c" have entirely different histories.
69a4647736
...
80a9ea67b1
73
bst.c
73
bst.c
@ -1,73 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include<stdlib.h>
|
|
||||||
|
|
||||||
/* A binary tree node has data, pointer to left child
|
|
||||||
and a pointer to right child */
|
|
||||||
struct node
|
|
||||||
{
|
|
||||||
int data;
|
|
||||||
struct node* left;
|
|
||||||
struct node* right;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct node* newNode(int data)
|
|
||||||
{
|
|
||||||
struct node* node = (struct node*)
|
|
||||||
malloc(sizeof(struct node));
|
|
||||||
node->data = data;
|
|
||||||
node->left = NULL;
|
|
||||||
node->right = NULL;
|
|
||||||
|
|
||||||
return(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct node* insert(struct node* node, int data)
|
|
||||||
{
|
|
||||||
/* 1. If the tree is empty, return a new,
|
|
||||||
single node */
|
|
||||||
if (node == NULL)
|
|
||||||
return(newNode(data));
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* 2. Otherwise, recur down the tree */
|
|
||||||
if (data <= node->data)
|
|
||||||
node->left = insert(node->left, data);
|
|
||||||
else
|
|
||||||
node->right = insert(node->right, data);
|
|
||||||
|
|
||||||
/* return the (unchanged) node pointer */
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int minValue(struct node* node) {
|
|
||||||
struct node* current = node;
|
|
||||||
|
|
||||||
/* loop down to find the leftmost leaf */
|
|
||||||
while (current->left != NULL) {
|
|
||||||
current = current->left;
|
|
||||||
}
|
|
||||||
return(current->data);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void deleteTree(struct node* node)
|
|
||||||
{
|
|
||||||
if (node == NULL) return;
|
|
||||||
|
|
||||||
/* first delete both subtrees */
|
|
||||||
deleteTree(node->left);
|
|
||||||
deleteTree(node->right);
|
|
||||||
|
|
||||||
/* then delete the node */
|
|
||||||
printf("\n Deleting node: %d", node->data);
|
|
||||||
free(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
int size(struct node* node)
|
|
||||||
{
|
|
||||||
if (node==NULL)
|
|
||||||
return 0;
|
|
||||||
else
|
|
||||||
return(size(node->left) + 1 + size(node->right));
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user