简体   繁体   中英

Getting parent node on a binary search tree

Here is the problem, I have a binary search tree with all my values stored and, given a certain value, I need to get the value of its parent node. I guess I could implement the tree myself but would be really nice if I could use something like Set or Map because since it's for a programming contest, I need to be able to code it fast and bug-free. I can use either C++11 or Java, can't use a library outside of the standard libraries and if possible I'm more comfortable coding in c++ instead of java but I can do both.

( OBS: This is only the training not the actual competition, the problem can be found here https://www.urionlinejudge.com.br/judge/pt/problems/view/2120 . I looked at the cplusplus.com to check the reference but I can't find anything alike )

The binary tree is a user defined class (ie not in a library). As a result, it will probably either be defined by the competition holder, or you'll be required to write it yourself. Therefore you'll have to write your own function to deal with it. If it doesn't necessarily need to run, you can probably assume a basic binary tree node structure like:

struct Node {
    Data data; 
    struct Node left;
    struct Node right;
}

The purpose of questions like these is to make sure that you can figure out algorithms, not that you know how to use the STL. You'll be expected to implement (possibly) the tree and (definitely) the algorithm yourself.

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