在一个两叉树中,如何用递归的方法求一个点是否是另一个点的祖先呢?

letok 2004-04-06 01:15:38
用递归方法判断一个点是否是另一个点的祖先
...全文
54 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
w3guy 2004-04-07
  • 打赏
  • 举报
回复
如果传入参数是node 的值,要分两步,而且不要写在一个函数里:
struct node
{
int value;
node *left;
node *right;
};

node* findnode(node* tree, int value)
{
if(tree == NULL) return NULL;
node *result = NULL;
if(tree->value == value) return node;
result = findnode(tree->left,value);
if(result) return result;
return findnode(tree->right,value);
}
bool isAncestor(node *tree, int acestor, int child)
{
//Find accestor node first
node *pAccestor = findnode(tree,acesstor);
if(pAccestor == NULL) return false;
//find if child in left tree
if(findnode(pAccestor->left,child)) return true;
//find if child in right tree
if(findnode(pAccestor->right,child)) return true;
return false;
}
w3guy 2004-04-07
  • 打赏
  • 举报
回复
如果你传入参数是node 的指针的话可以:
struct node
{
int value;
node *left;
node *right;
};

bool isAncestor(node *ancestor, node *child)
{
bool isAncestor = false;
if(ancestor==NULL || child == NULL) return false;
if(ancestor->left)
{
if(ancestor->left == child) return true;
isAncestor = checknode(ancestor->left,child)
if(isAncestor) return true;
}
if(ancestor->right)
{
if(ancestor->right == child) return true;
return checknode(ancestor->right,child)
}
return false;
}
anggogo 2004-04-07
  • 打赏
  • 举报
回复
我不是给了准确的答案你, 只是给了一个类似的代码你

t->leftchild->parent == t

这个就是判断 t 的左子树的父亲是否为 t .

我给的程序是一个用递归判断是否一个树是合法的而已.
代码这种东西参照一下就可以自己推出来啦
letok 2004-04-07
  • 打赏
  • 举报
回复
等一下,上面的有错误
letok 2004-04-07
  • 打赏
  • 举报
回复
我的解答
假设btee, 所有节点都为各不相同的整数

// if rootData != ancestor
// find ancestor in left
// find ancestor in right
// return (left + right)
// else
// find descendant in left
// search tree is left subtree, ancestor is left root
// find decendant in right
// search tree is right subtree, ancestor is left root
// return (left + right)



bool isAncestor(BinaryTree tree, int descendant, int ancestor)
{
bool validLeft = false;
bool validRight = false;

int temp = tree.rootData();

BinaryTree LeftSubTree(tree.leftSubtree());
BinaryTree RightSubTree(tree.rightSubtree());

//begin search from ancestor
if (tree.rootData() != ancestor )
{
if ( !LeftSubTree.isEmpty() )
{
validLeft = isAncestor(LeftSubTree,descendant,ancestor);
}
if ( !RightSubTree.isEmpty() )
{
validRight = isAncestor(RightSubTree,descendant,ancestor);
}

return (validLeft || validRight); //can not find ancestor

}
else //after find ancestor
{
//find decendant in left
if ( !LeftSubTree.isEmpty() )
{
if( LeftSubTree.rootData() == descendant)
{
validLeft = true;
}
else
{
ancestor = tree.rootData();
validLeft=isAncestor( LeftSubTree,
descendant,ancestor);

}
}

//find decendant in right
if ( !RightSubTree.isEmpty() )
{
if( RightSubTree.rootData() == descendant )
{
validRight = true;
}
else
{
ancestor = tree.rootData();
validRight=isAncestor( RightSubTree,
descendant,ancestor);

}
}

return (validLeft || validRight);

}


}
letok 2004-04-06
  • 打赏
  • 举报
回复
看不懂
要判断是否是祖先,函数参数应该是两个点呀


t->leftchild->parent == t 是什么意思?
anggogo 2004-04-06
  • 打赏
  • 举报
回复
类似如此, 自己再变化吧

bool bstree::checktree(treenode* t)
{
// if current node is empty, return true
if (t==NULL)
return true;

// if current node has no child, return true
if (t->leftchild == NULL && t->rightchild == NULL)
return true;

bool valid = false;
if (t->leftchild!=NULL)
{
if (t->leftchild->parent == t)
valid = true;
else
valid = false;
}

if (valid && t->rightchild!=NULL)
{
if (t->rightchild->parent == t)
valid = true;
else
valid = false;
}

return valid && checktree(t->leftchild) && checktree(t->rightchild);
}

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧