1. 程式人生 > >專為新手入門二叉樹(C實現)

專為新手入門二叉樹(C實現)

本篇部落格主要涉及二叉樹的基本操作,建立,三種遍歷,求節點等(C寫法)。

二叉樹作為資料結構的難點,想必讓很多人望而生畏,各種複雜的程式碼和演算法實在讓人頭大,博主也是近期剛接觸二叉樹,對於二叉樹的探究也不是很深刻,所以有紕漏還請體諒。

1.首先了解下二叉樹 二叉樹其實是樹的一種特殊形式,資料結構中除了圖也就是樹最難了,二叉樹的定義是採用的一種遞迴方式(如果不瞭解遞迴,可自行百度或者看看其他大牛關於遞迴的詳細闡述,新手可能在剛接觸遞迴的時候老是擔心遞迴的細節會不會出錯,其實遞迴的中心思想就是把大問題切割成小問題而已)。 在這裡插入圖片描述 二叉樹的特點有:點的度只可能為0,1,2,度為2的點個數+1=葉子結點個數等。 2.下面直接進入二叉樹的實現 二叉樹的資料結構也包含順序和鏈序的,其實就是分別通過陣列和指標來實現,考慮到二叉樹的結構,選擇鏈序的會更好。 在這裡插入圖片描述

所以可以寫出二叉樹資料結構 struct btnode{ char data; btnode* lchild; btnode* rchild; }; (結構體不懂的自行百度) 3.二叉樹的建立 知道了二叉樹的儲存結構,怎麼去建立一棵二叉樹呢,遞迴方法是最為便捷和最好理解的。 void create(btnode* &T) { char c; scanf("%c",&c); if(c==’/’) /* 若輸入為’/’,則認為該結點為空 */ return; T=new btnode; T->data=c; T->lchild=NULL; T->rchild=NULL; create(T->lchild); create(T->rchild); }

4.二叉樹的三種遍歷 二叉樹的遍歷包括先序、中序、後序(三種方法本質一樣,在這隻詳細介紹先序遍歷) 先序遍歷的規則是先visit根節點,再vistit該根節點的左孩子,再vistit該根節點的右孩子,如下圖二叉樹的先序遍歷輸出為: F>C>A>D>B>E>H>G>M 思想就是 在這裡插入圖片描述 程式碼實現為 void prior_order1(btnode* T) //遞迴先序遍歷二叉樹 { if(T) //節點為空直接跳出 { cout<data; prior_order1(T->lchild); prior_order1(T->rchild); } } 中序和後序只需要把(cout<data;)換下順序就可以了,我相信大家還是很好理解的。 5.二叉樹的層數 求二叉樹的層數其實也是採用遞迴方法 int depth(btnode* T)//求二叉樹的深度,規定根節點所在層次為0層 { int leftlen,rightlen; if(T==NULL) return 0; else { leftlen=depth(T->lchild)+1; rightlen=depth(T->rchild)+1; } if(leftlen>rightlen) return leftlen; return rightlen; //就是比較左右子樹的深度,選擇較大的, }

6.求葉子結點個數

void leaf(btnode* T,int &count)//求葉子節點個數 { if(T) {

	if(T->lchild==NULL &&T->rchild== NULL)
		count=count+1;
	leaf(T->lchild,count);
	leaf(T->rchild,count);	
}

} 其實掌握了這些方法,二叉樹的其他操作的思想也是差不多的。 最後附上全部程式碼 #include<stdio.h> #include using namespace std;

struct btnode{ char data; struct btnode lchild,rchild; }; extern int level=0; extern int count=0; int depth(btnode ); void create(btnode &T) { char c; scanf("%c",&c); if(c==’/’) //若為’/’,則該結點為空 return; T=new btnode; T->data=c; T->lchild=NULL; T->rchild=NULL; create(T->lchild); create(T->rchild); } void prior_order1(btnode* T)//遞迴先序遍歷二叉樹 {

if(T)
{
	cout<<T->data<<;
	prior_order1(T->lchild);
	prior_order1(T->rchild);
}

} void mid_order(btnode* T)//遞迴中序遍歷二叉樹 { if(T) { mid_order(T->lchild); cout<data<<; mid_order(T->rchild); } } void behind_bt(btnode*T)//後續遍歷二叉樹 { if(T) { behind_bt(T->lchild); behind_bt(T->rchild); cout<data<<; }

} void prior_order(btnode* T)//先序輸出二叉樹 ,並且同時輸出每個節點對應的位數 { int count=1; if(TNULL) return; printf("(%c,%d) “,T->data,count++); if(T->lchild) printf(”(%c,%d) “,T->lchild->data,count); if(T->rchild) printf(”(%c,%d) ",T->rchild->data,count); } void numbers_of_node(btnode* T,int &count)//計算二叉樹中結點個數,賦值給count { if(T) { ++count; numbers_of_node(T->lchild,count); numbers_of_node(T->rchild,count); } } int depth(btnode* T)//求二叉樹的深度,規定根節點所在層次為0層 { int leftlen,rightlen; if(TNULL) return 0; else { leftlen=depth(T->lchild)+1; rightlen=depth(T->rchild)+1; } if(leftlen>rightlen) return leftlen; return rightlen; } void leaf(btnode* T,int &count)//求葉子節點個數 { if(T) { if(T->lchildNULL&&T->rchildNULL) count=count+1; leaf(T->lchild,count); leaf(T->rchild,count); } } int main() { btnode *T=new btnode; T=NULL; int num1,node; num1=node=0; create(T); leaf(T,num1);//num1儲存葉子結點個數 numbers_of_node(T,node);//node儲存結點個數 cout<<“先序序列輸出結果為”; prior_order1(T); cout<<’\n’; cout<<“中序序列輸出結果為”; mid_order(T); cout<<’\n’; cout<<“後序序列輸出結果為”; behind_bt(T); cout<<’\n’; cout<<“葉子結點個數為”<<num1<<endl; cout<<“二叉樹結點個數為”<< node<<endl; cout<<“二叉樹深度為”<<depth(T); return 0; }

在這裡插入圖片描述 相信只要努力了肯定就有所收穫的,大家看不懂程式碼的時候就照著打幾遍,不懂也能記下來了,久而久之慢慢就掌握了。