首页 > 编程知识 正文

c语言实现二叉树的基本操作,c语言二叉树

时间:2023-05-05 04:16:52 阅读:159129 作者:1372

首先遍历文章列表定义结构体初始化二叉树的生成方法生成二叉树的测试方法2 :根据给定的字符串序列生成其他方法树的节点大小树寻找高判定树空判定节点的左孩子和右孩子所在节点的父节点,进行元素复制树空判定

结构#定义defineelemtypechartypedefstructbintreenode//树节点结构,每个数据节点对应一个存储单元,一个数据节点包括三个ElemType data; struct BinTreeNode* LeftChild; struct BinTreeNode* RightChild; (}BinTreeNode; typedef struct BinTree {//树结构BinTreeNode* root; //指树中第一个节点——个节点ElemType refvalue; //结束标记“#”表示字符类型(; voidinitbintree(bintree*Bt,ElemType ref ) {bt-root=NULL; //第一个树中没有节点的bt-refvalue=ref; )最初遍历二叉树的制作方法(1/1按最初的顺序输入二叉树中节点的值,二叉树btvoidcreatebintree _1(bintree * Bt ) ) createbintree_1) Bt,(bbbbt //参数传递需要地址} voidcreatebintree _1(bintree * Bt,BinTreeNode **t )/*t表示根节点的地址,因为此处参数传递需要地址scanf('%c ',item ); if(item==Bt-refvalue (() t )==NULL; //根节点为结束标签说明树为空(else(//根节点(创建分配区域) t )=(bintreenode* ) malloc ) sizeof ) bintreenode ); 资产() t )!=空; (t )-data=项目; //递归创建根左右子树,并创建createbintree_1(Bt,) (-LeftChild ) ); createbintree_1(Bt,() t )-RightChild ); }//voidmain (调用() ) {BinTree bt; initbintree(Bt,' # '; 创建bintree _1(Bt; //这样就不需要在外面调用结构成员了)测试

插入a

插入b

插入c

插入d

方法2 :根据给定的字符串序列编写注意参数char *str,对文字采用引用

voidcreatebintree_4(bintree*Bt,char *str ) create bintree _4(Bt,bt-root,str ); } voidcreatebintree _4(bintree * Bt,BinTreeNode *t,char *str ) BinTreeNode *t是根节点,BinTreeNode *类型if ) str t-data=*str; //*str表示指针指向字符串的第一个字符createbintree_4(Bt,t-LeftChild,str )。 createbintree_4(Bt,t-RightChild,str ); }}void main () {BinTree bt; initbintree(Bt,' # '; char *str='ABC##D##G## '; 创建bintree _4(Bt,str ); }解析字符串指针传递

实际上,必须在str1和str2参数之间传递字符串地址的地址

其他方法树的节点大小递归根左右的孩子

intsize(bintree*Bt ) returnsize (Bt-root ); }intsize(bintreenode*t ) if ) t==null ) return 0; ELSE{returnsize(t-leftchild ) size (t-right child ) 1; }树高intheight(bintree*Bt ) return height (Bt-root ); }intheight(bintreenode*t ) if ) t==null ) return 0; else { intleftheight=height (t-leftchild ); intrightheight=height (t-right child; return(leftheightrightheight )? 左高度:右高度(1; }判断树空的boolbintreeempty(bintree*Bt ) ({return bt-root==NULL; } bin treenode * leftchild (bin treenode * p ) if ) p==null ) return NULL,用于查找某个节点的左子代和右子代; return p-LeftChild; } bin treenode * right child (bin treenode * p ) if ) p==null ) return NULL; return p-RightChild; }用于查找某个节点的父节点的BinTreeNode *parent(bintree*Bt,bintreenode*p ) /给出某个节点的地址来查找父节点的return parent (Bt,p ); } bintreenode*parent (bin treenode * t,bin treenode * p ) if ) t==null||p==null ) return NULL; if (t-leftchild==p|| t-right child==p ) ) /从根节点返回t; (else (/bin treenode * q=parent (t-leftchild,p ),看根节点的左右孩子是否是p的左右节点; if(q!=null}{returnq; }returnparent(t-rightchild,p ); }元素bin treenode * search (查找bintree * Bt,ElemType key ) returnsearch ) Bt-root,key ); } bin treenode * search (bin treenode * t,ElemType key ) if(t==null ) return NULL; if(t-data==key ) return t; bin treenode * p=search (t-leftchild,key ); if(p!=null}{returnp; (else(//找不到左边的孩子,再去找右边的孩子的Returnsearch(t-rightchild,key )。 }复制树voidcopy(bintree*Bt1,BinTree *bt2 ) copy ) bt2-root,bt2-root; }voidcopy(bintreenode*T1,BinTreeNode *t2 ) /指向复制树的根节点的指针bintreenode*T1if ) t1=NULL ) t1=NULL; ELSE{T1=(bintreenode* ) malloc ) sizeof ) bintreenode ); 资产(t1!=空; t1-data=t2-data; 复印(t2-LeftChild,t2-LeftChild; copy(T1-rightchild,t2-RightChild; }空树voidbintreeclear(bintree*Bt ) returnbintreeclear (Bt-root ); } voidbintreeclear (bintreenode* t )/t是指向每个节点的指针,因为它是bin treenode *,所以该指针的地址if(t!=空(bintree clear (t-leftchild ) ); bintreeclear(t-rightchild ); free(t; t=NULL; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。