日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

二叉树链表结构表示法

發(fā)布時間:2025/3/19 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二叉树链表结构表示法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

二叉樹鏈表的結(jié)構(gòu)聲明:

? struct? tree

{

???????? int data;

???????? struct tree *left;

???????? stryct tree *right;

};

??????? typedef struct tree treenode;

??????? typedef? treenode *btree;

?

二叉樹鏈表結(jié)構(gòu)表示法 #include"iostream"
using namespace std;

struct tree //二叉樹結(jié)構(gòu)聲明
{
int data;
struct tree *left;
struct tree *right;

};
typedef
struct tree treenode;
typedef treenode
*btree;
/*----插入二叉樹的結(jié)點-------*/


btree insertnode(btree root,
int value)
{
btree newnode;
//樹根指針
btree current; //目前樹結(jié)點指針
btree back; //父結(jié)點指針

/*-----創(chuàng)建結(jié)點內(nèi)存----*/
newnode
=(btree)malloc(sizeof(treenode));
/*-------初始化-----*/
newnode
->data=value;
newnode
->left=NULL;
newnode
->right=NULL;

if(root == NULL) //是否為根結(jié)點
{
return newnode;
}
else
{
current
=root; //保留目前樹指針
while(current!=NULL)
{
back
=current;
if(current->data>value)
current
=current->left;
else
current
=current->right;
}
if(back->data>value)
back
->left=newnode;
else
back
->right=newnode;

}
return root;

}
/*----創(chuàng)建二叉樹---*/
btree createbtree(
int *data,int len)
{

btree root
=NULL;
int i;

for(i=0;i<len;i++)
root
=insertnode(root,data[i]);
return root;
}
/*-------兒二叉樹的輸出-------*/
void printbtree(btree root)
{
btree ptr;
ptr
=root->left;
printf(
"輸出左子樹:\n");
while(ptr!=NULL)
{
printf(
"[%2d]\n",ptr->data);
ptr
=ptr->left;
}
ptr
=root->right;
printf(
"輸出右子樹:\n");
while(ptr!=NULL)
{
printf(
"[%2d]\n",ptr->data);
ptr
=ptr->right;
}

}
/*------鏈表二叉樹---------*/
int main()
{
btree root
=NULL;
int data[10]={5,6,4,8,2,3,7,1,9};
root
=createbtree(data,9);

printf(
"樹的結(jié)點內(nèi)容:\n");
printbtree(root);

}

?

轉(zhuǎn)載于:https://www.cnblogs.com/FCWORLD/archive/2010/11/21/1883472.html

總結(jié)

以上是生活随笔為你收集整理的二叉树链表结构表示法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。