二叉樹學習

2021-09-06 08:30:20 字數 3423 閱讀 3814

最近有了大把的時間,學習了資料結構,在大學的時候就接觸過資料結構,在現在那時只不過是為了考乙個二級證,那時的概念只是背,但是一直不理解解,還好,我打算重走這段歷史。

回歸正題:

二叉樹的概念

二叉樹是n(n>=0)個結點的有限集合,該集合或者為空集(成為空二叉樹),或者由乙個節點和兩棵不互相交的,分別稱為根節點的左子樹和右子樹的二叉樹組成。

二叉樹的特點

每個結點最多有兩棵子樹

左右子樹是有順序的,不能顛倒的

即使子樹只有乙個結點,也要區分它是左子樹還是右子樹。

特殊二叉樹

斜樹 所有的結點都只有左子樹的二叉樹叫做左斜樹。所有結點都只有右子樹的二叉樹叫做右斜樹。這樣二叉樹的結點數與樹的深度有關。

2. 滿二叉

在一棵二叉樹中,如果所有的分支結點都存在左子樹和右子樹,並且所有的葉子都在同一層上,這樣的二叉樹叫做滿二叉。在同深度的二叉樹中滿二叉樹的葉子結點個數最多,葉子樹最多。

3. 完全二叉樹

對一棵樹具有n個節點的二叉樹按層序編號,如果編號為i(1<=i<=n)的結點與同深度的滿二叉樹編號為i的

節點在二叉樹中位置完全相同,則這棵二叉樹稱為完全二叉樹。

完全二叉樹的特點:

1)葉子結點只能出現在最下兩層

2)最下層的葉子一定集中在左部連續位置

3)倒數二層,若有葉子節點,一定都在右部連續位置

4)如果結點度為1,則該節點只有左子樹,即不存在只有右子樹的情況

5)同樣結點數的二叉樹,完全二叉樹的深度最小。

二叉樹的構建

兩個類的宣告:

class

bintreenode

;

void setrightchild(bintreenode*r)

bintreenode*getrightchild()

void setleftchild(bintreenode*r)

bintreenode *getleftchild()

void setdata(int

data)

intgetdata()

};class

bintree;

~bintree()

;void setroot(bintreenode *item)

bintreenode *getroot()

bintreenode *creat_tree();

void pre_order(bintreenode *item) const

;

void in_order(bintreenode *item) const

;

void post_order(bintreenode *item) const

;

void level_order(bintreenode *item)const

;

int get_leaf_num(bintreenode *item) const

;

int get_tree_height(bintreenode *item) const

;

void swap_left_right(bintreenode *item) const

;

//bintreenode * get_near_common_father(bintreenode *root,bintreenode *r,bintreenode *l) const;

void print_rout(bintreenode *item,int sun) const

;

bool is_in_tree(bintreenode *r,bintreenode *item) const

;};

2.二叉樹的構建

bintreenode *bintree::creat_tree()

else

}

二叉樹的前序遍歷

void bintree::pre_order(bintreenode *item) const

}

二叉樹的中序遍歷

void bintree::in_order(bintreenode *item) const

}

二叉樹的後續遍歷

void bintree::post_order(bintreenode *item) const

}

得到二叉樹的節點總數

int bintree::get_leaf_num(bintreenode *item) const

if(item->getleftchild() == null && item->getrightchild() ==null)

return (get_leaf_num(item->getleftchild())+get_leaf_num(item->getrightchild()));

}

得到樹的深度

int get_tree_height(bintreenode *item) const

if(item->getleftchild()==null && item->getrightchild() ==null)

int l_tree = get_tree_height(item->getleftchild());

int r_tree = get_tree_height(item->getrightchild());

return l_tree >=r_tree ? l_tree+1 : r_tree+1

;}

互動左子樹與右子樹

void bintree::swap_left_right(bintreenode *item)

bintreenode *tmp = item->getleftchild();

item->setleftchild(item->getrightchild());

item->setrightchild(tmp);

swap_left_right(item->getleftchild());

swap_left_right(item->getrightchild());

}

節點檢視是否在根為r的二叉樹中存在item這個節點。

bool bintree::is_in_tree(bintreenode *r,bintreenode *item) const

else

if(r ==item)

else

if(!has && r->getrightchild()!=null)

return

has;

}}

如何查詢二叉樹中兩個結點最近的雙親結點?稍後討論

二叉樹 二叉樹

題目描述 如上所示,由正整數1,2,3 組成了一顆特殊二叉樹。我們已知這個二叉樹的最後乙個結點是n。現在的問題是,結點m所在的子樹中一共包括多少個結點。比如,n 12,m 3那麼上圖中的結點13,14,15以及後面的結點都是不存在的,結點m所在子樹中包括的結點有3,6,7,12,因此結點m的所在子樹...

樹 二叉樹 滿二叉樹 完全二叉樹 完滿二叉樹

目錄名稱作用根 樹的頂端結點 孩子當遠離根 root 的時候,直接連線到另外乙個結點的結點被稱之為孩子 child 雙親相應地,另外乙個結點稱為孩子 child 的雙親 parent 兄弟具有同乙個雙親 parent 的孩子 child 之間互稱為兄弟 sibling 祖先結點的祖先 ancesto...

二叉樹學習

二叉樹的遞迴思想是一大難點。自認為是容易遺忘的東西。簡單問題 求n result n n 1 n 2 1 自然迴圈可以解決問題 int i,result result n for i n 1 i 0 i 遞迴的寫法就是 int fibonacci1 int n 很明顯的特點是 1.有乙個退出函式的條...