浅谈二叉树的存储表示与实现
来源:互联网 发布:lemon incest知乎 编辑:程序博客网 时间:2024/06/04 20:05
#include<stdio.h>//输入输出流#include<stdlib.h>//标准库 typedef struct node { int data; struct node *lchild; struct node *rchild; }*bitree,bitnode;//构造一个新的二叉树bitree initbitree(bitnode *root){ bitree tree=root; return tree;}//生成节点包含数据域和左右孩子bitnode *makenode(int item, bitnode *lchild,bitnode *rchild){bitnode *pnode=(bitnode*)malloc(sizeof(bitnode));if(pnode){ pnode->data=item; pnode->lchild=lchild; pnode->rchild=rchild;}return pnode;}//释放节点void freenode(bitnode *pnode){ if(pnode!=NULL) free(pnode); }//销毁二叉树void destorybitnode(bitree tree){bitnode *pnode=tree;if(pnode->lchild!=NULL) { destorybitnode(pnode->lchild);}if(pnode->rchild!=NULL) {destorybitnode(pnode->rchild);//递归调用} freenode(pnode);}//判断是否为空int isempty(bitree tree){ if(tree==NULL) return 0; else return 1;}//返回树的深度 int GetDepth(bitree tree) { int cd,ld,rd; cd=ld=rd=0; if(tree) { ld=GetDepth(tree->lchild); rd=GetDepth(tree->rchild); cd=(ld>rd?ld:rd); return cd+1; } else { return 0; } } //返回根 bitree get(bitree tree) { return tree; }//返回节点值int getint(bitnode *pnode){ return pnode->data;}//设置节点值void setint(bitnode *pnode, int item){pnode->data=item;}//设置左子树bitree setlchild(bitree parent,bitree lchild){parent->lchild=lchild;return lchild;}//设置右子树bitree setrchild(bitree parent,bitree rchild){parent->rchild=rchild;return rchild;}//返回左子树bitree getlchild(bitree tree){ if(tree!=NULL) return tree->lchild; elsereturn NULL; }//返回右子树bitree getrchild(bitree tree){ if(tree!=NULL) return tree->rchild; elsereturn NULL; }//插入新的子树,lr为0插入左子树,为1插入右子树bitree insertchild(bitree parent,int lr,bitree child){ if(parent) { if(lr==0&&parent->lchild==NULL){ parent->lchild=child; return child;}if(lr==1&&parent->rchild==NULL){ parent->rchild=child; return child;} }}//删除子树lr为0删除左子树,为1删除右子树void deletechild(bitree parent,int lr){ if(parent){ if(lr=0 && parent->lchild!=NULL){ parent->lchild=NULL; freenode(parent->lchild);//释放空间} if(lr=1 && parent->rchild!=NULL) { parent->rchild=NULL; free(parent->rchild); }}}int main(){bitnode *n1=makenode(10,NULL,NULL);bitnode *n2=makenode(20,NULL,NULL);bitnode *n3=makenode(30,n1,n2);bitnode *n4=makenode(40,NULL,NULL);bitnode *n5=makenode(50,NULL,NULL);bitnode *n6=makenode(60,n4,n5);bitnode *n7=makenode(70,NULL,NULL);bitree tree=initbitree(n7);//n7为根节点setlchild(tree,n3);//n3为n7的左子树setlchild(tree,n6);//n6为n7的右子树printf("第一次返回树的深度:%d\n",GetDepth(tree));deletechild(tree,0);printf("第二次返回树的深度:%d\n",GetDepth(tree));return 0;}
0 0
- 浅谈二叉树的存储表示与实现
- 二叉树的存储表示与实现
- c语言二叉树的存储表示与实现
- 二叉树的二叉线索存储表示
- 数据结构之---C语言实现二叉树的二叉链表存储表示
- 二叉树的表示链式表示和顺序存储3.0
- 串的链式存储表示与实现
- 二叉树的二叉链表存储表示
- 数据结构之---C语言实现二叉树的三叉链表存储表示
- 二叉树顺序表示的实现
- 二叉树的顺序表示和实现
- 线索二叉树的表示和实现
- 12.完全二叉树的顺序存储表示
- 第六章(1).二叉树的顺序存储表示
- 【数据结构】树与树的表示、二叉树存储结构及其遍历、二叉搜索树、平衡二叉树、堆、哈夫曼树与哈夫曼编码、集合及其运算
- 二叉树的链式存储与实现(C++)
- 二叉树的简单介绍和二叉树的二叉链表存储表示
- 二叉树的表示
- Linux Apache PHP编译配置
- 九度OJ 1525 子串逆序打印
- 每天一个linux命令(1):ls命令
- Android问题:Unexpected text found in layout file: """
- 易扩展的办公流程化管理核心模型(第1版)
- 浅谈二叉树的存储表示与实现
- BP神经网络及matlab实现
- poj 2251 Dungeon Master(bfs)
- Delphi 添加系统托盘
- Sublime Text 全程指引
- dpkg: error: -i (--install) 和 -i (--install) 两个操作之间有矛盾
- OC类和对象-对象的产生和使用
- HDOJ Red and Black 1312(入门级DFS)
- 每天一个linux命令(2):cd命令