二叉树查找-二分法查找二叉树.docx

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
二叉树查找-二分法查找二叉树 二分法查找二叉树方法:左大右小,找不到的时候就分支限定的查找 #include cstdlib #include iostream using namespace std; struct tree{ // 声明树的结构 struct tree *left; int data; struct tree *right; }; typedef struct tree treenode;//声明新类型的树的结构 typedef treenode *b_tree;//声明二叉树的链表 /*递归建立二叉树*/ b_tree creat_btree(int *nodelist,int position)//看好了某些定义b_tree { b_tree newnode;//声明树根指针 if(nodelist[position]==0||position15) {//cout d; return NULL;} else{ newnode = (b_tree) malloc(sizeof(treenode));//申请空间 newnode-data=nodelist[position];//建立节点内容 //cout newnode= newnode-data; newnode-left=creat_btree(nodelist,2*position); //递归建立左子树 newnode-right=creat_btree(nodelist,2*position+1);//递归建立右子树 return newnode; } } //建立二叉树 //二叉树遍历方式查找 b_tree btree_travesal_search(b_tree point,int findnode) { b_tree point1,point2;//声名往左和往右查询的指针 if(point!=NULL) { if(point-data==findnode) return point; else //找左子树 point1=btree_travesal_search(point-left,findnode); //找右子树 point2=btree_travesal_search(point-right,findnode); if(point1 != NULL) return point1; else if(point2!=NULL) return point2; else return NULL; } else return NULL; } //二叉树二分查找法 b_tree btree_travesal_search1(b_tree point, int findnode) { while(point!=NULL) { if(point-data==findnode)//找到了数据 return point;//返回找到节点的指针 else if(point-datafindnode) {point=point-left;}//向左子树找 else {point=point-right;}//向右子树找 } return NULL; } void inoder(b_tree point) { if(point!=NULL) { inoder(point-left); cout point-data ; inoder(point-right); } }; int main(int argc, char *argv[]) { b_tree root = NULL;//树根指针 b_tree point = NULL; int findnode; int i; int nodelist[16]={0,5,2,9,0,4,7,0,0,0,3,0,6,8,0,0}; //---------------建立树状结构-----------// root = creat_btree(nodelist,1); //建立 printf(\n The node content of arrary_structure is:\n); printf(\nPlease input the node value(1...9) you want search:); scanf(%d,findnode); //进行遍历查找 point=btree_travesal_search(root,findnode); if(point!=NULL) { cout \n=Travesal search result: \n; printf( The finding node value is [%d]\n,point-data

文档评论(0)

shujukd + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档