!BST跟BST == NULL到底有什么区别?

if(!BST){}

表示如果!BST=0将不执行大括号内程序
也就是BST=0将执行大括号内程序
也就是BST为空时执行大括号内程序

if(BST == NULL){}

表示如果BST==NULL将执行大括号内程序
也就是BST为空时执行大括号内程序

#include<stdio.h> #include<stdlib.h> typedef int KeyType; typedef struct node{ KeyType key; struct node*lchild,*rchild; }BSTNode,*BSTree; void InsertBST(BSTree*bst,KeyType key){ BSTree s;//?????????????????怎么不一样 if(*bst==NULL){ s=(BSTree)malloc(sizeof(BSTNode)); s->key=key; s->lchild=NULL; s->rchild=NULL; *bst=s; return; } else if(key<(*bst)->key) InsertBST(&((*bst)->lchild),key); else if(key>(*bst)->key) InsertBST(&((*bst)->rchild),key); } void CreateBST(BSTree*bst){ KeyType key; *bst=NULL; scanf("%d",&key); while(key!=0){ InsertBST(bst,key); scanf("%d",&key); } } BSTree DelBST(BSTree t,KeyType k){ BSTNode *p,*f,*s,*q; p=t;f=NULL; while(p){ if(p->key==k)break; f=p; if(p->key>k)p=p->lchild; else p=p->rchild; } if(p==NULL)return t; if(p->lchild==NULL){ if(f==NULL)t=p->rchild; else if(f->lchild==p)f->lchild=p->rchild; else f->rchild=p->rchild; free(p); } else{ q=p;s=p->lchild; while(s->rchild) {q=s;s=s->rchild; }if(q==p)q->lchild=s->lchild; else q->rchild=s->lchild; p->key=s->key; free(s); } return t; } int layer(BSTree bst,int k,int lay){ if(bst){ if(bst->key==k)return lay; if(bst->key>k){ lay++; return layer(bst->rchild,k,lay); } if(bst->key<k){ lay++; return layer(bst->lchild,k,lay); } } } void preOrder(BSTree bst){ if(bst!=NULL){ printf("%d ",bst->key); preOrder(bst->lchild); preOrder(bst->rchild); } if(bst==NULL)printf("# "); } void InOrder(BSTree bst){ if(bst!=NULL){ InOrder(bst->lchild); printf("%d ",bst->key); InOrder(bst->rchild); } } int main(){ BSTree bst; CreateBST(&bst); preOrder(bst); int key; scanf("%d",&key); bst=DelBST(bst,key); InOrder(bst); int n,lay=1; scanf("%d",&n); printf("%d",layer(bst,n,lay)); return 0; }为什么层数始终是0?怎么改
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值