二叉树的输入输出,遍历结果,和双亲结点的查找
来源:互联网 发布:音乐播放软件知乎 编辑:程序博客网 时间:2024/05/22 00:38
/*20130701_denyz*//*二叉树的存储,输入,输出,遍历(DLR,LDR,RDL)*/ /*查找值为x的结点的双亲结点*//*建立二叉树的二叉链表*/#include <stdio.h>#include <malloc.h>typedef struct node{ struct node *Lchild,*Rchild; char data; }Btree; Btree *CreatTree(){ Btree *BT; char a; scanf("%c",&a); if(a==' ') BT=NULL; else { BT=(Btree *)malloc(sizeof(Btree)); /*DLR method construct the binary tree*/ BT->data=a; BT->Lchild=CreatTree();//create Left child tree BT->Rchild=CreatTree();//create Right child tree } return BT;/*the first time run the function,BT is the head pointor of the binary tree*/}/*DLR sequence*/void DLR(Btree *BT){ if(BT!=NULL) { printf("%c ",BT->data); DLR(BT->Lchild); DLR(BT->Rchild); }}/*LDR sequence*/void LDR(Btree *BT){ if(BT!=NULL) { LDR(BT->Lchild); printf("%c ",BT->data); LDR(BT->Rchild); }}/*LRD sequence*/void LRD(Btree *BT){ if(BT!=NULL) { LRD(BT->Lchild); LRD(BT->Rchild); printf("%c ",BT->data); }}/*查找值为 x 的双亲结点*/Btree *find(Btree *BT,char X){ char ch; ch=X; if(BT) { if((BT->Lchild!=NULL && BT->Lchild->data==ch) || (BT->Rchild!=NULL && BT->Rchild->data==ch)) { printf("存在值为%c的结点的孩子结点值为%c",BT->data,ch); } if((BT->Lchild!=NULL && BT->Lchild->data!=ch) || (BT->Rchild!=NULL && BT->Rchild->data!=ch)) { find(BT->Lchild,ch); find(BT->Rchild,ch); } } }int main(){ Btree *BT,*pos,*parent=NULL; printf("Note:对于广义表为:a(b(d,x),c( ,e))的二叉树,\ \n输入的时候叶子结点的左右结点以空格的形式输入,\n为空的结点也是以空格输入,输入的时候将括号省略\ \nFor Example:abd x c e \n\n"); BT=CreatTree(); printf("DLR\n"); DLR(BT); printf("\nLDR\n"); LDR(BT); printf("\nLRD\n"); LRD(BT); printf("\n"); find(BT,'x'); getch(); return 1;}
0 0
- 二叉树的输入输出,遍历结果,和双亲结点的查找
- 二叉树的查找、二叉树高度、二叉树获得双亲结点、构造二叉树、二叉树的广义表表示法、二叉树的插入删除、二叉树的非递归实现
- 二叉查找树的创建和遍历
- 二叉树的遍历和查找
- 二叉树的【深/高度】【度为0/1/2的结点数】【遍历】【查找】【销毁】
- 二叉树如何实现双亲结点及求树的高度
- 二叉树的遍历和二叉查找树
- java数据结构二叉树的遍历和二叉查找树
- 【学习点滴-数据结构-二叉树】序列是否是二叉查找树的后序遍历结果
- 二叉树的遍历、查找
- 判断整数数列是不是二叉查找树后序遍历的结果
- java 二叉树(六)查找二叉树一个节点的双亲节点
- 二叉树的下一个结点(二叉树中序遍历的使用)
- 根据二叉树的前序遍历和中序遍历的结果,重建二叉树
- 二叉查找树的建立,遍历,查找
- 由二叉树的前序遍历和中序遍历来求后序遍历的结果
- data_struction_test3:二叉查找树,查找值最小的结点
- 二叉搜索树的创建,结点删除和查找(二叉搜索树)
- Android系统RPC与Binder
- 继承关系 为没个类映射一张表
- 几个常见的压缩算法
- Android跑起Cocos2d-x的SimpleGame
- 浅析Struts1和Struts2的Action线程安全问题
- 二叉树的输入输出,遍历结果,和双亲结点的查找
- RTP/RTSP编程
- Leetcode: Valid Palindrome
- P2P之UDP穿透NAT的原理与实现(附源代码)
- myeclipse常用快捷键总结
- 样式和模板快速入门Style,Template
- 为BBB制作专属自己的cape(三)
- load方法的懒加载及原理
- 桑葚干