#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
这是一类通用树(不是二叉树)。实现定位功能的最快方法是什么?我应该先检查所有孩子,然后检查兄弟姐妹还是什么?