简体   繁体   English

在C ++中搜索树中节点的最快方法

[英]Fastest way to search for a node in a tree in C++

#ifndef __TREE_H
#define __TREE_H
#include <cstdlib>
#include<string>

// structure of a tree node

struct TreeNode{
        string str;
        TreeNode *parent;
        TreeNode *leftChild;
        TreeNode *nextSibling;

        TreeNode(string str1){
                this->str = str1;
                this->parent = NULL;
                this->leftChild = NULL;
                this->nextSibling = NULL;
        }

};

class Tree{

        TreeNode* root;
        int size;

public:

        Tree();         //constructor

        void insert(string str1);       //insert a node

        string locate(string str1);     //locate a node

        TreeNode *ancestor(string str1, string str2);   //get lowest common ancestor
};


#endif

this is a class of a generic tree (not a binary tree). 这是一类通用树(不是二叉树)。 What will be the fastest way to implement the locate function? 实现定位功能的最快方法是什么? should i go through all the child first and then the siblings or what? 我应该先检查所有孩子,然后再检查兄弟姐妹吗?

If the tree is unordered there is no algorithm other than brute force testing of all nodes and break out when the element is found (if found). 如果树是无序的,则除了对所有节点进行蛮力测试外,没有其他算法,并且在找到元素时(如果找到)会爆发。 When dealing with trees, recursion is usually the simplest approach. 处理树时,递归通常是最简单的方法。 The pseudo algorithm could be something like: 伪算法可能类似于:

find(current_node,value):

if current_node.value == value
   return found
else 
   if find(current_node.left,value) == found
      return found
   else if find(current_node.right,value) == found
      return found
   else
      return not_found

Of course, when really implementing this you will need to test for null pointers, and so on. 当然,当真正实现此功能时,您将需要测试空指针,等等。 Without any other constraints on the tree, the asymptotic complexity cannot be reduced. 在树上没有任何其他约束的情况下,渐近复杂度无法降低。 You might be able to come with a non-recursive approach or a tail-recursion algorithm (based on the above) that might improve the constant factors, but don't expect a huge improvement there. 您可能可以使用非递归方法或尾递归算法(基于上述方法)来改善常数因子,但不要期望在那里有很大的改进。

If the nodes are ordered, ie children are less than this node and siblings are greater than this node, you compare with str and, depending on the result, have this node as a result or search down the children or compare with the siblings 如果节点是有序的,即子节点小于此节点,而兄弟节点大于此节点,则与str进行比较,并根据结果将这个节点作为结果,或搜索子节点或与兄弟节点进行比较

const TreeNode *TreeNode::locate(const string &str1) const
{
    int c = str.compare(str1);
    if (c == 0)
        return this;

    if (c > 0) {
        if (leftChild)
            return leftChild->locate(str1);

        return 0;
    }

    if (nextSibling)
        return nextSibling->locate(str1);

    return 0;
}

and in Tree 在树上

const TreeNode *Tree::locate(const string &str1) const
{
    return root->locate(str1);
}

You can try several tree traversal algorithms depending on how your information is distributed among the tree nodes. 您可以尝试几种树遍历算法,具体取决于您的信息在树节点之间的分布方式。

BFS, DFS, are some examples. BFS,DFS是一些示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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