简体   繁体   中英

Find if the binary search tree has duplicated value

For binary search tree to see if the tree has duplicated value or not. I took this post order approach.

My goal was to keep the value of the current node and then use other function traverse the tree to see if there is any matching value to that current value, and if it finds any duplicate value it brings "true value". I choose to use recursion as it seems easier to track. but when I ran the program there was no output coming out.

#include "pch.h"
#include <iostream>

using namespace std;

class BSTNode {
public:
int data;
BSTNode* left;
BSTNode* right;

BSTNode() {};


};

BSTNode* newnode(int newdata) { BSTNode *curr = new BSTNode; curr->data = newdata; curr->left = curr->right = nullptr; return curr; }
void print(BSTNode* root) {
    if (root != nullptr) {
        print(root->left);
        cout << root->data << endl;
        print(root->right);
    }
}


bool checking(BSTNode* parent, int val) {
    if (val == parent->data){
        bool left = checking(parent->left, val);
        bool right = checking(parent->right, val);
        return left||right;
    }
    else
        return false;
}


bool assist(BSTNode* parent) {
    if (parent != nullptr) {
        assist(parent->left);
        assist(parent->right);
        return checking(parent, parent->data);
    }
    else return false;
}


int main() {

    BSTNode *test = newnode(1);
    test->left=newnode(2);
    test->right=newnode(3);
    test->left->left=newnode(2);
    test->right->right=newnode(5);

    print(test);


    if (assist(test))
        cout << "There is duplicated" << endl;
    else
        cout << "There is no duplicated" << endl;

    return 0;

}

Your checking function should look like this:

bool checking(BSTNode* parent, int val) {
    if(parent == nullptr)   // point 1
        return false;
    if (val == parent->data){   // point 2
        return true;
    }
    else{
        bool left = checking(parent->left, val);
        bool right = checking(parent->right, val);
        return left||right;
    }
}

Your assist function should look something like this:

bool assist(BSTNode* parent) {
    if (parent != nullptr) {   
        if(checking(parent->left, parent->data)) return true;  // point 3
        if(checking(parent->right, parent->data)) return true;
         return assist(parent->left)||assist(parent->right);   // point 4
    }
    else return false;
}
  1. You need to check for null values.

  2. If val is same, why are you still checking? Just stop

  3. You need to check node's value in the left and right subtree.

  4. Recurse it for the child nodes

If you want to check that parent value is different than child values, you might do:

bool checking(const BSTNode* node, int parent_value) {
    if (node == nullptr) { return false; }
    if (node->data == parent_value) { return true; } 
    return checking(node->left, node->data)
        || checking(node->right, node->data);
}

bool assist(const BSTNode* parent) {
    if (parent == nullptr) {
        return false;
    }
    return checking(parent->left, parent->data)
        || checking(parent->right, parent->data);
}

You could just go through the BST breadth wise with a Deque. Store the values in a set and check if the value is already in the set, if it is return true otherwise wait for the loop to finish and return true. This had the benefit of hash table lookup for values at thr cost of extra storage in O(n) time. Its also easier to follow in my opinion as it's not recursion.

bool hasDuplicate(BSTNode *parent) 

{ 
if (!parent) return false;

std::dueue<BSTNode*> nodes;
std::unordered_set<int> vals;
nodes.push_back(parent);

while(!nodes.empty()) {

BSTNode *node = nodes.pop_front();
int v = nodes->val;

// Check if value exists and return true
if(vals.find(v) != vals.end()) return true;

// Otherwise insert it
vals.insert(v);

// insert left node if exists
if (node->left) nodes.push_back(node->left);

// insert right node if exists
if (node->right) nodes.push_back(node->right);
}

// no dups found
return false;

}

Sorry for bad indents. Did this on phone lol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM