0

Write the implementation of the function T findMedian() const that computes the median value in the tree in O(n) time. Assume that the tree is a BST but is not necessarily balanced. Recall that the median of n numbers is defined as follows: If n is odd, the median is x such that the number of values smaller than x is equal to the number of values greater than x. If n is even, then one plus the number of values smaller than x is equal to the number of values greater than x. For example, given the numbers 8, 7, 2, 5, 9, the median is 7, because there are two values smaller than 7 and two values larger than 7. If we add number 3 to the set, the median becomes 5.

Here is the class of binary search tree node:

template <class T>
class BSTNode
{
public:
    BSTNode(T& val, BSTNode* left, BSTNode* right);
    ~BSTNode();
    T GetVal();
    BSTNode* GetLeft();
    BSTNode* GetRight();

private:
    T val;
    BSTNode* left;
    BSTNode* right;
    BSTNode* parent; //ONLY INSERT IS READY TO UPDATE THIS MEMBER DATA
    int depth, height;
    friend class BST<T>;
};

Binary search tree class:

template <class T>
class BST
{
public:
    BST();
    ~BST();

    bool Search(T& val);
    bool Search(T& val, BSTNode<T>* node);
    void Insert(T& val);
    bool DeleteNode(T& val);
    int Count(void) const;
    T findMedian() const;

    void BFT(void);
    void PreorderDFT(void);
    void PreorderDFT(BSTNode<T>* node);
    void PostorderDFT(BSTNode<T>* node);
    void InorderDFT(BSTNode<T>* node);
    void ComputeNodeDepths(void);
    void ComputeNodeHeights(void);
    bool IsEmpty(void);
    void Visit(BSTNode<T>* node);
    void Clear(void);

private:
    BSTNode<T> *root;
    int depth;
    int count;
    int index = 0;; // I've added this member data.

    void DelSingle(BSTNode<T>*& ptr);
    void DelDoubleByCopying(BSTNode<T>* node);
    void ComputeDepth(BSTNode<T>* node, BSTNode<T>* parent);
    void ComputeHeight(BSTNode<T>* node);
    void Clear(BSTNode<T>* node);
    int Count(BSTNode<T>* node) const;
    T findMedian(BSTNode<T>* node) const;

};

Here is the count code:

template <class T>
int BST<T>::Count() const
{
    Count(root);
}

template <class T>
int BST<T>::Count(BSTNode<T>*node) const
{
    if (node == NULL)
        return 0;
    return 1 + Count(node->left) + Count(node->right);
} 

And here is the findMedian code:

template<class T>
T BST<T>::findMedian() const
{
    findMedian(root);
}

template <class T>
T BST<T>::findMedian(BSTNode<T>* node) const
{
    int counter = Count();
    if (node == NULL)
        return;
    T tmp = findMedian(node->left);
    if (tmp != NULL)
        return tmp;
    if (index == counter / 2)
        return node->val;
    index++;
    return findMedian(node->right);
}

When building it I get the following errors:

enter image description here

Anyone has any clue how to fix this? And will this code work with an even number of elements?

Nat
  • 107
  • 1
  • 2
  • 10
  • 1
    This is a follow up question of [Find median in binary search tree](http://stackoverflow.com/q/29989788/572670) – amit May 01 '15 at 17:05
  • There's no way your implementation is `O(n)` time. You call `Count()` far too often. – Ben Voigt May 01 '15 at 17:53
  • Since you want to use only one const function I strongly suggest using an iterative version of the algorithm: http://www.lixinglian.com/idea/?p=358 – pjsofts May 01 '15 at 18:06
  • That median finding function can not possibly be correct. – Niklas B. May 01 '15 at 20:00
  • @NiklasB. [link](http://stackoverflow.com/questions/29993677/median-function-gives-wrong-output) how about this one? – Nat May 01 '15 at 20:03
  • @BenVoigt any idea how to do it in O(n) time? – Nat May 01 '15 at 20:04
  • Oh it's O(n), then it could at least be correct. But there is a O(log n) solution too that requires subtree size augmentation – Niklas B. May 01 '15 at 20:46
  • @NiklasB. I am asked to do it in O(n) time. So is the solution in here [Median function gives wrong output](http://stackoverflow.com/questions/29993677/median-function-gives-wrong-output) correct? And does it run in O(n) time? – Nat May 02 '15 at 14:30

1 Answers1

-1

Make index a static local variable in the function findMedian. This is not recommended but one solution when it is insisted on having one const recursive function.

pjsofts
  • 170
  • 4
  • 18