C語言 二叉查找樹性質詳解及實例代碼
二叉查找樹性質
1、二叉樹
每個樹的節(jié)點最多有兩個子節(jié)點的樹叫做二叉樹。
2、二叉查找樹
一顆二叉查找樹是按照二叉樹的結構來組織的,并且滿足一下性質:
一個節(jié)點所有左子樹上的節(jié)點不大于蓋節(jié)點,所有右子樹的節(jié)點不小于該節(jié)點。
對查找樹的操作查詢,插入,刪除等操作的時間復雜度和樹的高度成正比, 因此,構建高效的查找樹尤為重要。
查找樹的遍歷
先序遍歷
查找樹的遍歷可以很簡單的采用遞歸的方法來實現(xiàn)。
struct list
{
struct list *left;//左子樹
struct list *right;//右子樹
int a;//結點的值
};
void preorder(struct list *t)//t為根節(jié)點的指針
{
if(t!=NULL)
{
printf("%d,",t->a);
preorder(t->left);
perorder(t->right);
}
}
中序遍歷
struct list
{
struct list *left;//左子樹
struct list *right;//右子樹
int a;//結點的值
};
void preorder(struct list *t)//t為根節(jié)點的指針
{
if(t!=NULL)
{
preorder(t->left);
printf("%d,",t->a);
perorder(t->right);
}
}
后序遍歷
struct list
{
struct list *left;//左子樹
struct list *right;//右子樹
int a;//結點的值
};
void preorder(struct list *t)//t為根節(jié)點的指針
{
if(t!=NULL)
{
preorder(t->left);
perorder(t->right);
printf("%d,",t->a);
}
}
查找樹的搜索
給定關鍵字k,進行搜索,返回結點的指針。
struct list
{
struct list *left;//左子樹
struct list *right;//右子樹
int a;//結點的值
};
struct list * search(struct list *t,int k)
{
if(t==NULL||t->a==k)
return t;
if(t->a<k)
search(t->right);
else
search(t>left);
};
也可以用非遞歸的形式進行查找
struct list
{
struct list *left;//左子樹
struct list *right;//右子樹
int a;//結點的值
};
struct list * search(struct list *t,int k)
{
while(true)
{
if(t==NULL||t->a==k)
{
return t;
break;
}
if(t->a<k)
t=t->rigth;
else
t=t->left;
}
};
最大值和最小值查詢
根據(jù)查找樹的性質,最小值在最左邊的結點,最大值的最右邊的 結點,因此,可以直接找到。
下面是最大值的例子:
{
struct list *left;//左子樹
struct list *right;//右子樹
int a;//結點的值
};
struct lsit *max_tree(struct lsit *t)
{
while(t!=NULL)
{
t=t->right;
}
return t;
};
查找樹的插入和刪除
插入和刪除不能破壞查找樹的性質,因此只需要根據(jù)性質,在樹中找到相應的位置就可以進行插入和刪除操作。
struct list
{
struct list *left;//左子樹
struct list *right;//右子樹
int a;//結點的值
};
void insert(struct list *root,struct list * k)
{
struct list *y,*x;
x=root;
while(x!=NULL)
{
y=x;
if(k->a<x->a)
{
x=x->left;
}
else
x=x->right;
}
if(y==NULL)
root=k;
else if(k->a<y->a)
y->left=k;
else
y->right=k;
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
欄 目:C語言
下一篇:Ubuntu配置sublime text 3的c編譯環(huán)境的具體步驟
本文標題:C語言 二叉查找樹性質詳解及實例代碼
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1709.html
您可能感興趣的文章
- 04-02c語言函數(shù)調用后清空內存 c語言調用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求階乘


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-10C#中split用法實例總結
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?


