树(中)笔记

层序遍历
void LevelOrderTraversal(BinTree BT)
{
Queue Q;
BinTree T;
if(!BT)return;
Q=CreateQueue();
AddQ(Q,BT);
while(!IsEmpty(Q))
{
T=DeleteQ(Q);
printf("%d ",T->Data);
if(T->left)Add(Q,T->left);
if(T->right)Add(Q,T->right);
}
}
二叉搜索树的查找
int found(int x, bintree bst)
{
while (bst)
{
if (x > bst->data)
return found(x, bst->right);
else if (x < bst->data)
return found(x, bst->left);
else
return bst;
}
return NULL;
}
二叉搜索树最大最小节点的查找
int findmin(bintree bst)
{
if (!bst)
return;
else if (!bst->left)
return bst;
else
return findmin(bst->left);
}
非递归
int findmin(bintree bst)
{
if(bst)
{
while(bst->left)bst=bst->left;
return bst;
}
}
int findmax(bintree bst)
{
if (!bst)
return;
else if (!bst->right)
return bst;
else
return findmax(bst->right);
}
int findmax(bintree bst)
{
if (!bst)
return;
else if (!bst->right)
return bst;
else
{
while (bst->right)
bst = bst->right;
return bst;
}
}
二叉搜索树的插入算法
bintree insert(int x,bintree bst)
{
if(!bst)
{
bst=(bintree)malloc(sizeof(struct node));
bst->data=x;
bst->left=bst->right=NULL;
}
else
{
if(xdata)
{
bst->left=intsert(x,bst->left);
}
else if(x>bst->data)
{
bst->right=insert(x,bst->right);
}
}
return bst;
}

posted @ 2020-11-17 23:30  代码画师  阅读(65)  评论(0)    收藏  举报