step3 . day7数据结构之二叉顺序数的创建和二叉树的栈形式遍历
1。最近從網(wǎng)易云課堂學(xué)習(xí)了一個(gè)老師的數(shù)據(jù)結(jié)構(gòu)相關(guān)的知識(shí),了解到數(shù)據(jù)結(jié)構(gòu)的應(yīng)用和二分查找之間的關(guān)系,就自己想著寫(xiě)一個(gè)創(chuàng)建二叉順序數(shù)和利用棧對(duì)二叉順序樹(shù)進(jìn)行順序輸出的代碼,終于一個(gè)周末的時(shí)間寫(xiě)完了。
2.寫(xiě)代碼的重點(diǎn)在于實(shí)現(xiàn)邏輯,由于遞歸思想和邏輯的混亂,代碼容易出現(xiàn)段錯(cuò)誤或者,偶爾輸出一半的結(jié)果,梳理了好久,結(jié)合前序、中序輸出推導(dǎo)出樹(shù)的結(jié)構(gòu),從而尋找代碼問(wèn)題的形式,終于搞定了
3.因此代碼思想遠(yuǎn)比代碼實(shí)現(xiàn)更重要,而代碼的思想又需要扎實(shí)的基本功,繼續(xù)努力。。。,
?4.添加:二叉排序樹(shù)的查找,利用比較大小,判斷是在左右子樹(shù)位置,從而判斷有無(wú)
刪除:查找刪除位置,如果是葉子節(jié)點(diǎn),修改其根節(jié)點(diǎn)的孩子指針,如果是根節(jié)點(diǎn),將其左子樹(shù)最大、或者右子樹(shù)最小替換到該位置,刪除替換元素
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _node{
int date;
struct _node * lchild;
struct _node * rchild;
struct _node * parent;
}btreenode;
void btreenode_add(btreenode * root,btreenode * temp){
btreenode *temproot = root; //根節(jié)點(diǎn)臨時(shí)變量
if(temp->date > temproot->date){ //對(duì)比節(jié)點(diǎn)數(shù)據(jù)域
if(temproot->rchild != NULL){ //尋找插入點(diǎn)
temproot = temproot->rchild;
btreenode_add(temproot,temp); //插入點(diǎn)不為空,遞歸調(diào)用
}
else
temproot->rchild = temp;
}
else{
if(temproot->lchild != NULL){
temproot = temproot->lchild;
btreenode_add(temproot,temp);
}
else
temproot->lchild = temp;
}
}
//二叉順序樹(shù)的創(chuàng)建
btreenode * btreenode_creat(int num){
if(num <= 0) return NULL; //創(chuàng)建節(jié)點(diǎn)數(shù)小于0退出
srand(time(NULL)); //隨機(jī)數(shù)函數(shù),創(chuàng)建頭節(jié)點(diǎn)并初始化
btreenode * root = (btreenode *)malloc(sizeof(btreenode));
memset(root,'\0',sizeof(root));
root->date = rand()%100+1;
root->lchild = root->rchild = root->parent = NULL;
int i;
for(i=2;i<=num;i++){ //依次添加節(jié)點(diǎn)
btreenode * temp = (btreenode *)malloc(sizeof(btreenode));
memset(temp,'\0',sizeof(temp));
temp->date = (rand()%100+1);
btreenode_add(root,temp);//調(diào)用添加節(jié)點(diǎn)函數(shù)
}
return root; //返回根節(jié)點(diǎn)
}
//二叉樹(shù)的中序遍歷
void mid_show(btreenode * root){
if(root->lchild != NULL)
mid_show(root->lchild);
printf("%d ",root->date);
if(root->rchild != NULL)
mid_show(root->rchild);
}
//二叉樹(shù)的先序遍歷
void fir_show(btreenode * root){
printf("%d ",root->date);
if(root->lchild != NULL)
fir_show(root->lchild);
if(root->rchild != NULL)
fir_show(root->rchild);
}
typedef struct _linkstack{ //棧結(jié)構(gòu)體
btreenode * node;
struct _linkstack *next;
}linkstack;
linkstack * create(){
linkstack * ls = (linkstack *)malloc(sizeof(linkstack));
memset(ls,'\0',sizeof(ls));
ls->next = NULL;
ls->node = NULL;
return ls;
}
int is_empty(linkstack * ls){
return ls->next ==NULL ? 1 : 0;
}
void push(linkstack * ls,btreenode *node){
linkstack * temp = create();
temp->node = node;
linkstack * lstemp = ls;
while(lstemp->next != NULL){ lstemp = lstemp->next;}
lstemp->next = temp;
}
linkstack * pop(linkstack * ls){
if(is_empty(ls)){
printf("stact_is_empty\n");
return NULL;
}
linkstack *temp,*p,*q;
temp = ls;
while(temp->next->next != NULL) temp =temp->next;
p = temp;
q = p->next;
p->next = NULL;
return q;
}
?
//二叉樹(shù)使用棧實(shí)現(xiàn)中序輸出
void stact_show(linkstack * ls,btreenode * root){
btreenode * temp = root;
push(ls,temp); //根節(jié)點(diǎn)壓棧
while(temp->lchild != NULL){ //左孩子不為空壓棧
temp = temp->lchild;
push(ls,temp);
}
while( !(is_empty(ls))){//棧非空出站
temp = pop(ls)->node;
printf("%d ",temp->date);
if(temp->rchild !=NULL) //出站節(jié)點(diǎn)有右孩子,遞歸調(diào)用自身壓棧出棧
stact_show(ls,temp->rchild);
}
}
int main(int argc, const char *argv[])
{
btreenode * root = btreenode_creat(10);
printf("1first show. ************************\n");
fir_show(root);
printf("\n");
printf("2.midddle show ************************\n");
mid_show(root);
printf("\n");
printf("3.stact_show ************************\n");
linkstack * ls = create();
stact_show(ls,root);
printf("\n");
return 0;
}
轉(zhuǎn)載于:https://www.cnblogs.com/huiji12321/p/11262880.html
總結(jié)
以上是生活随笔為你收集整理的step3 . day7数据结构之二叉顺序数的创建和二叉树的栈形式遍历的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Oracle学习(五)DBLINK
- 下一篇: Day 43 索引