1

I am working on Data structures in C and i need to find the Median of the values in the B.S.T and delete the median then showcase the new B.S.T but i haven't managed.I don't really know where am going wrong.Please help me out on this. I also need to be able to find a value that is closest to the mean and delete it

#include<stdio.h>
#include<stdlib.h>

struct Node
{
int key;
struct Node* left, *right;
};

//Function to create new BST NODE
struct Node *newNode (int item)
{
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
//Function to perform INORDER traversal operation
void inorder(struct Node *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d\n",root->key);
inorder (root->right);
}
} 
//Function to insert new node with given key in BST
struct Node* insert (struct Node* node, int key)
{
//if tree is empty
if(node==NULL) return newNode(key);
//else, recur down the tree
if(key< node->key)
node->left  = insert(node->left,key);
else if(key > node->key)
node->right  = insert(node->right,key);
//Return the (unchanged) node pointer
return node;    
}


//Given a non-empty BST ,return node with minimum key value found in that          tree. Whole tree not really searched
struct Node * minValueNode(struct Node* node)
{
struct Node* current =node;
//loop down to find the left-most leaf
while (current->left !=NULL)
    current = current->left;
return current;
}
//Given the BST and key, function to deleted the key to return new root
struct Node* deleteNode(struct Node* root, int key)
{
if(root == NULL) return root;
if(key<root->key)
    root->left = deleteNode(root->left,key);
else if (key> root->key)
    root->right = deleteNode(root->right, key);
else
{
    //node with one or no child
    if(root->left == NULL)
    {
        struct Node *temp = root->right;
        free(root);
        return temp;
    }
 else if (root->right == NULL)
 {
    struct Node *temp = root->left;
    free(root);
    return temp;
}
//node with two children
struct Node* temp = minValueNode(root->right);
//copy the inorder successor to this node
root->key = temp->key;
//delete the inorder successor
root->right = deleteNode(root->right, temp->key);
}
return root;
}
//Function to count nodes in a BST using Morris Inorder traversal
int counNodes(struct Node *root)
{
struct Node *current, *pre;
int count =0;
if(root == NULL)
    return count;
current = root;
while(current!=NULL)
{
    if(current->left == NULL)
    {
        count++;
        current = current->right;;
    }
    else
    {
        pre = current->left;
        while(pre->right !=NULL && pre->right !=current) 
        pre=pre->right;
        if(pre->right == NULL)
        {
            pre->right = current;
            current= current->left;
        }
        else
        {
            pre->right = NULL;
            count++;
            current = current->right;
        }
    }
}
return count;
} 
/*FUNCTION TO FIND MEDIAN */
int FINDM(struct Node *root)
{
if(root == NULL)
    return 0;
    int count = counNodes(root);
    int currCount=0;
    struct Node *current = root, *pre, *prev;
    while(current !=NULL)
    {
        if(current->left == NULL)
        {
            currCount++;
            if(count%2!=0 && currCount ==(count+1)/2)
                return (prev->key);
                //EVEN NUMBERS
                else if(count%2==0 && currCount==(count/2)+1)
                    return (prev->key + current->key)/2;
                    prev = current;
                    current = current->right;
                }
                else
                {
                    pre=current->left;
                    while(pre->right !=NULL && pre->right !=current)
                    pre= pre->right;
                    if(pre->right = NULL)
                    {
                        pre->right = current;
                        current = current->left;
                    }
                    else
                    {
                        pre->right = NULL;
                        prev = pre;
                        currCount++;
                        if(count%2!=0 && currCount == (count+1)/2)
                            return (current->key);
    else if(count%2==0 && currCount ==  (count/2)+1)
                                return (prev->key+current->key)/2;
                                prev = current;
                                current = current->right;
                            }
                        }
                    }
                }

  //Driver Program to test functions above
int main()
{
// values are 28,90,0,32,56,78,212,34,62
struct Node *root =NULL;

root=insert(root,65);
insert(root, 28);
insert(root, 90);
insert(root,0);
insert(root, 32);
insert(root,56);
insert(root, 78);
insert(root,212);
insert(root,34);
insert(root,62);

printf("InOrder Traversal of Tree\n");
inorder(root);

printf("\nDelete 65!\n");
root=deleteNode(root,65);
printf("Modified Tree\n");
inorder(root);

printf("\nMEDIAN ");
FINDM(root);
return 0;
}
  • 1
    Your solution will find the median in O(n) time. It's not hard to find the k'th largest number in a BST (which of course lets you find the median, too) in O(log n) time if you make it an order statistic tree. The order statistics support doesn't make any other tree operation asymptotically slower. – Gene May 02 '18 at 00:46
  • Possible duplicate of [Find median in binary search tree](https://stackoverflow.com/questions/29989788/find-median-in-binary-search-tree) – MFisherKDX May 02 '18 at 00:47

1 Answers1

0

You already have a working function counNodes() which traverses the nodes in order, for counting the number of nodes. What if you use that exact code in a new function that finds the Nth node in the tree :

struct Node *findNth(struct Node* root, int nth) {
    struct Node *current, *pre;
    int count = 0;
    if (root == NULL)
        return count;
    current = root;
    while (current != NULL)
    {
        if (current->left == NULL)
        {
            count++;
            if (count == nth) {
                return current;
            } else {
                current = current->right;;
            }
        } else
        {
            pre = current->left;
            while (pre->right != NULL && pre->right != current)
                pre = pre->right;
            if (pre->right == NULL)
            {
                pre->right = current;
                current = current->left;
            } else
            {
                pre->right = NULL;
                count++;
                if (count == nth) {
                    return current;
                } else {
                    current = current->right;
                }
            }
        }
    }

    printf("Could not find nth(%d)\n", nth);
    exit(1);
}


int FINDM(struct Node *root)
{
    if (root == NULL)
        return 0;
    int count = counNodes(root);

    struct Node *n = findNth(root, (count + 1) / 2);

    return n->key;
}
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31