[二叉树算法]先序中序后序遍历 算法应用总结
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[二叉树算法]先序中序后序遍历 算法应用总结,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1375字,纯文字阅读大概需要2分钟。
内容图文
![[二叉树算法]先序中序后序遍历 算法应用总结](/upload/InfoBanner/zyjiaocheng/649/385a4b849a134c3e8c7514b47dfe8a52.jpg)
//先序遍历下的第k个结点 int preorder(BTNode *t,int k,int n){ int result; if(t==null) return 0; if(n==k) return t->data; result=preorder(t->lchild,k,n+1); if(result!=0) return result; else{ return preorder(t->rchild,k,n+1); } }
//先序中序 非递归 //中序遍历下的第k个结点 //中序遍历的第k个结点 int count=0; BTNode InOrder(BTNode *t,int k,int n){ if(t==null && k<0) return null; BTNode target=null; if(t->lchild!=null){ target=InOrder(t->lchild,k,n+1); } count++; if(target==null){ if(count==k){ target=t; return target; } } if(target==null && t->rchild!=null){ target=InOrder(t->rchild,k,n+1); } return target; }
//求二叉树中值为x的层号 void NodeLevel(BTNode *t,int x,int &h,int temp_h){//当前层次 if(t==null) h=0; else if(t->data==x) h=temp_h; else{ Node7h(t->lchild,x,h,temp_h+1); if(h==0){ NodeLevel(t->rchild,x,h,temp_h+1); } } }
//删除一颗二叉树 void deleteTree(BTNode *t){ if(t==null) return; else{ deleteTree(t->lchild); deleteTree(t->rchild); free(t); } }
//交换左右子树 void swapChild(BTNode *t,BTNode *t1){ if(t==null) t1=null; else{ t1=(BTNode*)malloc(sizeof(BTNode)); t1->data=t->data; swapChild(t->lchild,t1->rchild); swapChild(t->rchild,t1->lchild); } } //非递归 不新建结点 void swapChild(BTNode *t){ if(t==null) return; InitStack(S); push(&s,t); BTNode *p; while(!isEmpty(s)){ t=pop(&s); p=t->lchild; t->lchild=t->rchild; t->rchild=p; if(t->rchild!=null){ push(&s,t->rchild); } if(t->lchild!=null){ push(&s,t->lchild); } } }
内容总结
以上是互联网集市为您收集整理的[二叉树算法]先序中序后序遍历 算法应用总结全部内容,希望文章能够帮你解决[二叉树算法]先序中序后序遍历 算法应用总结所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。