資料結構 樹

2021-10-09 23:07:54 字數 2388 閱讀 2466

// tnode.h
#pragma once

#include

#include

#include

using namespace std;

//************************************

// method: treenode

// fullname: treenode

// qualifier: 樹節點屬性

//************************************

class

property

//自定義新增設定屬性的方法:todo

property()

//析構函式

~property()

public

://初始化函式

void

init()

//自定義新增獲取屬性的方法:todo

//獲取ivalue的值

int getvalue()

//獲取ichildnum的值

int getchildnum()

protected

:void

setvalue()

void

setchildnum()

private

: int ivalue;

int ichildnum;

vector viarrary;};

//************************************

// method: treenode

// fullname: treenode

// qualifier: 一棵樹的節點

//************************************

struct treenode

;struct sumpara

;//************************************

// method: createtree

// fullname: createtree

// access: public

// returns: void

// qualifier: 建立一棵樹

// parameter: treenode * & tnode

//************************************

void

createtree

(treenode*

& tnode, sumpara sp)

//設定當前節點的屬性

tnode =

newtreenode

; tnode-

>pnature = prop;

prop-

>

init()

; tnode-

>vtchildren.

resize

(tnode-

>pnature-

>

getchildnum()

,null);

//處理孩子節點

for(int i =

0; i < tnode-

>pnature-

>

getchildnum()

;++i)

}//************************************

// method: visittree

// fullname: visittree

// access: public

// returns: void

// qualifier: 先序遍歷一棵樹

// parameter: treenode * tnode

//************************************

void

visittree

(treenode* tnode)

cout << tnode-

>pnature-

>

getvalue()

<< endl;

int isize = tnode-

>vtchildren.

size()

;for

(int i =

0; i < isize;

++i)

}

// main.cpp
#include "tnode.h"

void

main()

資料結構 樹

樹的概念 1.家族樹 在現實生活中,有入如下血統關係的家族可用樹形圖表示 張源有三個孩子張明 張亮和張麗 張明有兩個孩子張林和張維 張亮有三個孩子張平 張華和張群 張平有兩個孩子張晶和張磊。以上表示很像一棵倒畫的樹。其中 樹根 是張源,樹的 分支點 是張明 張亮和張平,該家族的其餘成員均是 樹葉 而...

資料結構 樹

1 定義 樹是一種非線性結構,是一種一對多的資料結構。分析樹的結構,我們用遞迴的方法,根結點下面又可以看做是子樹。2 樹的儲存結構 我們一般用孩子兄弟法儲存。也就是把乙個結點的左邊第乙個孩子放在此結點的左邊孩子,把此結點的右兄弟放在此結點的右邊孩子。這樣就產生了二叉樹。二叉樹和樹可以相互對應。3 二...

資料結構 樹

二叉樹性質回顧 滿二叉樹 完全二叉樹等 給定一棵二叉樹,要求分層遍歷該二叉樹,即從上到下按層次訪問該樹,每一層單獨輸出一行,每一層要求訪問的順序為從左到右。我們在遍歷的過程中將該層節點的孩子節點壓入乙個佇列,這樣就可以實現從上到下一層一層地遍歷該二叉樹。層序遍歷 並分層列印 如果不用分層的話只用佇列...