寻找二叉搜索树的后继节点
2013-09-16 16:11
183 查看
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef struct _tree_node
{
int m_nValue;
struct _tree_node *m_pParent;
struct _tree_node *m_pLeft;
struct _tree_node *m_pRight;
}TreeNode;
void initTree(TreeNode **ppHead,int value,TreeNode *pNodeParent)
{
if(*ppHead==NULL)
{
*ppHead=(TreeNode *)malloc(sizeof(TreeNode));
assert(*ppHead!=NULL);
(*ppHead)->m_nValue=value;
(*ppHead)->m_pParent=pNodeParent;
(*ppHead)->m_pLeft=NULL;
(*ppHead)->m_pRight=NULL;
}
else if(value<(*ppHead)->m_nValue)
{
initTree(&(*ppHead)->m_pLeft,value,*ppHead);
}
else
{
initTree(&(*ppHead)->m_pRight,value,*ppHead);
}
}
/*void printTree(TreeNode *pNode)
{
if(pNode==NULL)
return;
printTree(pNode->m_pLeft);
printf("%d",pNode->m_nValue);
printTree(pNode->m_pRight);
}*/
TreeNode *minNode(TreeNode *pHead)
{
if(pHead==NULL)
return NULL;
while(pHead->m_pLeft)
{
pHead=pHead->m_pLeft;
}
return pHead;
}
TreeNode *successor(TreeNode *pNode)
{
if(pNode==NULL)
return NULL;
if(pNode->m_pRight)
return minNode(pNode->m_pRight);
TreeNode *pNext=pNode->m_pParent;
while(pNext&&pNext->m_pRight==pNode)
{
pNode=pNext;
pNext=pNode->m_pParent;
}
return pNext;
}
TreeNode *findNode(TreeNode *pHead,int value)
{
if(pHead==NULL)
return NULL;
if(pHead->m_nValue==value)
return pHead;
else if(value<pHead->m_nValue)
return findNode(pHead->m_pLeft,value);
else
return findNode(pHead->m_pRight,value);
}
int main()
{
TreeNode *pHead=NULL;
TreeNode *pNodeParent=NULL;
int value;
while(scanf("%d",&value)&&value!=-1)
{
initTree(&pHead,value,pNodeParent);
}
// printTree(pHead);
TreeNode *pNode=findNode(pHead,6);
TreeNode *pNext=successor(pNode);
if(pNext!=NULL)
printf("%d ",pNext->m_nValue);
else
printf("no number");
system("pause");
return 0;
}
#include<assert.h>
#include<stdlib.h>
typedef struct _tree_node
{
int m_nValue;
struct _tree_node *m_pParent;
struct _tree_node *m_pLeft;
struct _tree_node *m_pRight;
}TreeNode;
void initTree(TreeNode **ppHead,int value,TreeNode *pNodeParent)
{
if(*ppHead==NULL)
{
*ppHead=(TreeNode *)malloc(sizeof(TreeNode));
assert(*ppHead!=NULL);
(*ppHead)->m_nValue=value;
(*ppHead)->m_pParent=pNodeParent;
(*ppHead)->m_pLeft=NULL;
(*ppHead)->m_pRight=NULL;
}
else if(value<(*ppHead)->m_nValue)
{
initTree(&(*ppHead)->m_pLeft,value,*ppHead);
}
else
{
initTree(&(*ppHead)->m_pRight,value,*ppHead);
}
}
/*void printTree(TreeNode *pNode)
{
if(pNode==NULL)
return;
printTree(pNode->m_pLeft);
printf("%d",pNode->m_nValue);
printTree(pNode->m_pRight);
}*/
TreeNode *minNode(TreeNode *pHead)
{
if(pHead==NULL)
return NULL;
while(pHead->m_pLeft)
{
pHead=pHead->m_pLeft;
}
return pHead;
}
TreeNode *successor(TreeNode *pNode)
{
if(pNode==NULL)
return NULL;
if(pNode->m_pRight)
return minNode(pNode->m_pRight);
TreeNode *pNext=pNode->m_pParent;
while(pNext&&pNext->m_pRight==pNode)
{
pNode=pNext;
pNext=pNode->m_pParent;
}
return pNext;
}
TreeNode *findNode(TreeNode *pHead,int value)
{
if(pHead==NULL)
return NULL;
if(pHead->m_nValue==value)
return pHead;
else if(value<pHead->m_nValue)
return findNode(pHead->m_pLeft,value);
else
return findNode(pHead->m_pRight,value);
}
int main()
{
TreeNode *pHead=NULL;
TreeNode *pNodeParent=NULL;
int value;
while(scanf("%d",&value)&&value!=-1)
{
initTree(&pHead,value,pNodeParent);
}
// printTree(pHead);
TreeNode *pNode=findNode(pHead,6);
TreeNode *pNext=successor(pNode);
if(pNext!=NULL)
printf("%d ",pNext->m_nValue);
else
printf("no number");
system("pause");
return 0;
}
相关文章推荐
- [LeetCode] Inorder Successor in BST 二叉搜索树中的中序后继节点
- 求二叉搜索树任一节点的前驱后继节点
- 二叉树寻找中序遍历的后继节点
- 寻找二叉搜索树的第K小的节点
- 【二叉树】寻找一个二叉树的节点在中序遍历中的后继节点
- [LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点
- 【算法面试题】寻找二叉搜索树中两个节点的最近公共祖先节点(转)
- 在二叉搜索树里,寻找两个节点 Find a pair with given sum in a Balanced BST
- [CareerCup] 4.6 Find Next Node in a BST 寻找二叉搜索树中下一个节点
- 二叉搜索树建立、插入、删除、前继节点、后继节点之c++实现
- java链表寻找中间节点
- 寻找链表的倒数第k个节点
- 一条单链表可以表示一个一元多项式,每个节点包含三个域:指数、系数和后继节点(指针或引用)。
- BZOJ 1588 朴素TREAP,寻找前驱和后继
- [置顶] 二叉搜索树的搜索、插入、后继、删除
- 二叉查找树带父节点指针的创建,销毁,查找,删除,插入,找前驱后继,找最小值最大值(递归和非递归的实现)
- 寻找节点d=n的节点算法
- 寻找二叉树两个节点的最低公共祖先
- 寻找下一个节点
- 寻找单链表的中间节点