C_and_CPP 板


LINE

※ 引述《ke60811 (胖脸嘎在哪?笑)》之铭言: 这是程式码 执行会跳出错误 请大大帮忙解答 root为空节点---> root ---> O <-- | / | ( A ) | | /| |\ | | O | | O | |-||-- -||-- #include<iostream> using namespace std; typedef struct treenode *tree_pointer; typedef struct treenode{ int left_thread; tree_pointer left_child; int data; int right_thread; tree_pointer right_child; }; void insert_node(tree_pointer *node,int num,tree_pointer *r); tree_pointer modified_search(tree_pointer root,int key); void Insert_right(tree_pointer parent, tree_pointer child); void Insert_Left(tree_pointer parent, tree_pointer child); tree_pointer insucc(tree_pointer tree); void tinorder(tree_pointer tree); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 以上对於节点的基本操作都有了 怎麽没有更基本的建立跟摧毁函式? 或者另外用个资料结构包装root跟size 如果有个外覆结构体threadtree 你可以很清楚的把事情改成 "将key放入到threadtree" "在threadtree找key" "移除某个key" "清除整个tree" 而且可以很清楚的知道树的根以及总节点数 由总节点数可以知道该不该让search,remove工作停止。 另外你还可以为search制作私有版本跟公开版本 私有版本如 tree_pointer tree_search_node(tree_pointer tree,int key); /* 回传节点 */ 公开版本 int* tree_search(tree_pointer tree,int key) /* 回传资料位址 */ 如此就可以定义树的操作如 tree_pointer tree_create(); void tree_delete(tree_pointer tree); void tree_insert(tree_pointer tree,int key); int* tree_search(tree_pointer tree,int key); void tree_clear(tree_pointer tree); int tree_size(const tree_pointer tree); int tree_empty(const tree_pointer tree); int main(){ int a[11] = {7,4,1,5,16,8,11,12,15,9,2}; tree_pointer A,root; A = (tree_pointer)malloc( sizeof(tree_pointer) ); root = (tree_pointer)malloc( sizeof(tree_pointer) ); A = NULL; root = NULL; for(int i=0;i<11;i++) insert_node(&A,a[i],&root); tinorder(root); system("pause"); return 0; } void insert_node(tree_pointer *node,int num,tree_pointer *r){ tree_pointer ptr,temp; (*r)->right_child=*r; (*r)->left_child=*node; ptr=(tree_pointer)malloc(sizeof(tree_pointer)); temp=modified_search(*node,num); (*r)->left_thread=(*r)->right_thread=0; temp->left_thread=temp->right_thread=1; ptr->data=num; ptr->left_child=ptr->right_child=NULL; if(*node){ if(num<temp->data){ temp->left_child=*r; Insert_Left(temp,ptr); } else{ temp->right_child=*r; Insert_right(temp,ptr); } } else *node = ptr; } tree_pointer modified_search(tree_pointer root,int key){ tree_pointer tree,p_tree; /* 有必要建立一份记忆体区块吗? */ tree=(tree_pointer)malloc(sizeof(tree_pointer)); p_tree=(tree_pointer)malloc(sizeof(tree_pointer)); if(root == NULL) return NULL; else /* 这里就把刚刚配置的tree指标覆盖掉了 */ tree=root; while(tree!=NULL){ /* 这里就把刚刚配置的p_tree指标覆盖掉了 */ p_tree=tree; if(key == tree->data) /* 为什麽找到了却是回传NULL ?! */ return NULL; if(key<tree->data) tree=tree->left_child; else tree=tree->right_child; } return p_tree; } tree_pointer insucc(tree_pointer tree){ tree_pointer temp; /* 这里取得了一块空间 */ temp=(tree_pointer)malloc(sizeof(tree_pointer)); /* 这里却又把刚刚的指标覆盖掉 */ temp = tree->right_child; if(!tree->right_thread) while(!temp->left_thread) temp = temp->left_child; return temp; } void tinorder(tree_pointer tree){ tree_pointer temp; /* 这里取得了一块空间 */ temp=(tree_pointer)malloc(sizeof(tree_pointer)); /* 这里覆盖掉刚取得的位址 */ temp=tree; for(;;){ temp = insucc(temp); if(temp==tree) break; cout<<" "<<temp->data; } } void Insert_right(tree_pointer parent, tree_pointer child) { tree_pointer temp; temp=(tree_pointer)malloc(sizeof(tree_pointer)); child->right_child = parent->right_child; child->right_thread = parent->right_thread; child->left_child = parent; child->left_thread = 1; parent->right_child = child; parent->right_thread = 0; if (!child->right_thread) { /* 这里把配置给temp的记忆体位址覆盖掉了 */ temp = insucc(child); temp->left_child = child; } } void Insert_Left(tree_pointer parent, tree_pointer child){ tree_pointer temp; temp=(tree_pointer)malloc(sizeof(tree_pointer)); child->left_child = parent->left_child; child->left_thread = parent->left_thread; child->right_child = parent; child->right_thread = 1; parent->left_child = child; parent->left_thread = 0; if (!child->left_thread) { /* 这里把配置给temp的记忆体位址覆盖掉了 */ temp = insucc(child); temp->right_child = child; } } 一般来说 我们写insert,不会是插入节点吧?! 应该是插入key。 不管怎麽样,依照你的问题,最明显的就是modified_search 找到了节点了却还要回传NULL 额外的问题,你的程式有很严重的memory leak。 更严重的是排版不佳 配置记忆体的工作已经混乱到哪里都得配置了,建议你把函式参数的 责任关系给厘清 --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.227.124.207 ※ 编辑: sunneo 来自: 61.227.124.207 (04/09 03:58)
1F:推 ke60811: if(key == tree->data) /* 为什麽找到了却是回传NULL ?! 04/09 13:11
2F:→ ke60811:一样的资料就不增加节点 04/09 13:11
3F:→ ke60811:谢谢大大回答 04/09 13:12
4F:→ sunneo:即使如此 你的insert没有判断它是不是NULL就尝试存取 04/09 13:35
※ 编辑: sunneo 来自: 61.227.118.128 (04/09 13:37)







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BuyTogether站内搜寻

TOP