網(wǎng)站首頁 編程語言 正文
概念
本文以一個(gè)簡(jiǎn)單的樹為例,如下圖,來記錄樹的一些概念。
樹
一種由n個(gè)節(jié)點(diǎn)組成的具有一定層次關(guān)系的有限數(shù)據(jù)集合。每個(gè)節(jié)點(diǎn)有0個(gè)或者n個(gè)子節(jié)點(diǎn),有一個(gè)根節(jié)點(diǎn)(沒有前驅(qū)只有后繼),除根節(jié)點(diǎn)外每一個(gè)節(jié)點(diǎn)都有一個(gè)前驅(qū),0個(gè)或多個(gè)后繼。
樹的葉子節(jié)點(diǎn)
只有一個(gè)前驅(qū),沒有后繼的節(jié)點(diǎn),為最外層的節(jié)點(diǎn)。葉子節(jié)點(diǎn)的度為0。
節(jié)點(diǎn)的度
節(jié)點(diǎn)擁有的子樹的數(shù)目。
分支結(jié)點(diǎn)
度不為0的結(jié)點(diǎn)。
樹的度
樹中結(jié)點(diǎn)的最大的度。
樹的高度
任意葉子節(jié)點(diǎn)距離根節(jié)點(diǎn)的最大深度。此文中樹的葉子節(jié)點(diǎn)為D、E、H,距離根節(jié)點(diǎn)的深度都為4,故高度為4。
樹的深度
即從根節(jié)點(diǎn)到葉子節(jié)點(diǎn)的行數(shù)。此文中樹的深度為4。
二叉樹
二叉樹是每個(gè)節(jié)點(diǎn)最多有兩個(gè)子樹的樹結(jié)構(gòu)。
它有五種基本形態(tài):
二叉樹可以是空集;
根可以有空的左子樹或右子樹;
或者左、右子樹皆為空。
二叉樹的特點(diǎn)
二叉樹第i層上的結(jié)點(diǎn)數(shù)目最多為2i-1(i>=1)
深度為k的二叉樹至多有2k-1個(gè)結(jié)點(diǎn)(k>=1)
包含n個(gè)結(jié)點(diǎn)的二叉樹的高度至少為(log2n)+1
滿二叉樹
高度為h,并且由2h-1個(gè)節(jié)點(diǎn)組成的二叉樹。
完全二叉樹
一棵二叉樹中,只有最下面兩層節(jié)點(diǎn)的度可以小于2,并且最下層的葉節(jié)點(diǎn)集中在靠左的若干位置上,這樣的二叉樹稱為完全二叉樹。
二叉查找樹
二叉查找樹又被稱為二叉搜索樹。設(shè)x為二叉查找樹中的一個(gè)結(jié)點(diǎn),x結(jié)點(diǎn)包含關(guān)鍵字key,結(jié)點(diǎn)x的key值計(jì)為key[x]。如果y是x的左子樹中的一個(gè)結(jié)點(diǎn),則key[y]<=key[x];如果y是x的右子樹的一個(gè)結(jié)點(diǎn),則key[y]>=key[x]。
特點(diǎn):
1.若任意結(jié)點(diǎn)的左子樹不空,則左子樹上所有結(jié)點(diǎn)的值均小于它的根結(jié)點(diǎn)的值。
2.任意結(jié)點(diǎn)的右子樹不空,則右子樹上所有結(jié)點(diǎn)的值均大于它的根結(jié)點(diǎn)的值。
3.任意結(jié)點(diǎn)的左、右子樹也分別為二叉查找樹。
4.沒有鍵值相等的結(jié)點(diǎn)。
示例
下面直接上代碼,一個(gè)簡(jiǎn)單的樹的創(chuàng)建、遍歷輸出,葉子節(jié)點(diǎn)數(shù),高度。
代碼實(shí)現(xiàn)
Tree.h
#pragma once
typedef struct MYTREE {
char data;
struct MYTREE* lChild;
struct MYTREE* rChild;
}MyTree;
class Tree
{
public:
Tree();
~Tree();
void CreateTree();
void TraverseTree(MyTree *root);
void GetLeafNode(MyTree *root,int &num);
int GetTreeDepth(MyTree *root);
void GetTreeNode(MyTree *root, int &num);
};
Tree.cpp
#include "Tree.h"
#include <iostream>
#include<algorithm>//max,min
using namespace std;
Tree::Tree()
{
}
Tree::~Tree()
{
}
void Tree::CreateTree()
{
MyTree t1 = {'A',nullptr,nullptr};
MyTree t2 = { 'B',nullptr,nullptr };
MyTree t3 = { 'C',nullptr,nullptr };
MyTree t4 = { 'D',nullptr,nullptr };
MyTree t5 = { 'E',nullptr,nullptr };
MyTree t6 = { 'F',nullptr,nullptr };
MyTree t7 = { 'G',nullptr,nullptr };
MyTree t8 = { 'H',nullptr,nullptr };
t1.lChild = &t2;
t1.rChild = &t6;
t2.rChild = &t3;
t3.lChild = &t4;
t3.rChild = &t5;
t6.rChild = &t7;
t7.lChild = &t8;
TraverseTree(&t1);
cout << endl;
int leafNum = 0;
GetLeafNode(&t1,leafNum);
cout << "leaf num: " << leafNum << endl;
int treeDepth = GetTreeDepth(&t1);
cout << "depth:" << treeDepth << endl;
int nodeNum = 0;
GetTreeNode(&t1,nodeNum);
cout << "node num; " << nodeNum << endl;
}
void Tree::TraverseTree(MyTree *root)
{
if (root == nullptr)
{
return;
}
TraverseTree(root->lChild);
cout << root->data;
TraverseTree(root->rChild);
}
void Tree::GetLeafNode(MyTree *root,int &num)
{
if (root == nullptr)
{
return ;
}
if (root->lChild == nullptr && root->rChild == nullptr)
{
num++;
}
GetLeafNode(root->lChild,num);
GetLeafNode(root->rChild,num);
}
int Tree::GetTreeDepth(MyTree * root)
{
int num = 0;
if (root == nullptr)
{
return num;
}
int lNum = GetTreeDepth(root->lChild);
int rNum = GetTreeDepth(root->rChild);
return max(lNum,rNum)+1;
}
void Tree::GetTreeNode(MyTree * root, int & num)
{
if (root == nullptr)
{
return;
}
++num;
GetTreeNode(root->lChild,num);
GetTreeNode(root->rChild,num);
}
main.cpp
#include <iostream>
#include "Tree.h"
using namespace std;
void test() {
Tree t;
t.CreateTree();
}
int main()
{
test();
return 0;
}
開發(fā)環(huán)境
vs2017控制臺(tái)輸出程序。
運(yùn)行結(jié)果
原文鏈接:https://blog.csdn.net/blqzj214817/article/details/125403432
相關(guān)推薦
- 2022-12-05 如何在React中直接使用Redux_React
- 2022-01-22 Redis——docker構(gòu)建的Redis集群
- 2022-04-17 Android編程開發(fā)從零開始編寫一個(gè)輕量級(jí)瀏覽器_Android
- 2022-05-18 C語言模擬內(nèi)存函數(shù)分析之mencpy與memmove_C 語言
- 2022-07-21 C語言詳細(xì)講解if語句與switch語句的用法_C 語言
- 2022-04-09 Spring事務(wù)管理之開啟聲明式事務(wù)
- 2021-11-25 vc控制臺(tái)程序關(guān)閉事件時(shí)的處理方式及注意點(diǎn)詳解_C 語言
- 2023-11-13 【云原生】docker設(shè)置非root用戶使用權(quán)限的方法
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支