当前位置:网站首页>Tree and binary tree (learning notes)
Tree and binary tree (learning notes)
2022-07-27 00:08:00 【Muxi Krystal】
Trees and binary trees
Trees are n A finite set of nodes .
root —— Root node ( There is no precursor )
leaf —— Terminal node ( There is no successor )
The forest —— namely m A collection of disjoint trees
Ordered trees , Disordered trees
The degree of node —— Number of subtrees attached to nodes
The degree of a tree —— The maximum of all node degrees
Can prove that , All trees can be transformed into a unique corresponding binary tree
Basic characteristics of binary tree :
1. The degree of node ≤2
2. Ordered trees
Properties of binary trees :
1. On the second of the binary tree i There is at most 2^(i-1) Nodes .
2. Depth is k At most, the binary tree of 2^k-1 Nodes .
Full binary tree , Perfect binary tree
A full binary tree is a special case of a complete binary tree .
3. have n The depth of a complete binary tree of nodes must be [log2(n)]+1.
Binary list :
typedef struct BiNode{
TElemType data;
struct BiNode *lchild,*rchild;
}BiNode,*BiTree;
Traversing the binary tree :
The first sequence traversal —— First root, then left, then right
In the sequence traversal —— First left, then root, then right
After the sequence traversal —— First left, then right, then root
Preorder traversal implementation :
Status PreOrderTraverse(BiTree T){
if(T==NULL) return OK;
else{
cout<<T->data;
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
Middle order traversal implementation :
Status PreOrderTraverse(BiTree T){
if(T==NULL) return OK;
else{
PreOrderTraverse(T->lchild);
cout<<T->data;
PreOrderTraverse(T->rchild);
}
}
Post order traversal implementation :
Status PreOrderTraverse(BiTree T){
if(T==NULL) return OK;
else{
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
cout<<T->data;
}
}
Analysis of traversal algorithm :
Time efficiency :O(n)
Space efficiency :O(n)
The establishment of binary tree :
void CreateBiTree(BiTree &T){
cin>>ch;
if(ch=='#') T=NULL;
else{
T=new BiNode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
Calculate the total number of binary tree nodes :
int NodeCount(BiTree T){
if(T=NULL) return 0;
else return NodeCount(T->lchild)+NodeCount(T->rchild);
}
边栏推荐
- 【2016】【论文笔记】差频可调谐THz技术——
- In simple terms, cchart daily lesson - happy high school lesson 57 new starting point, the old tree and new bud of colorful interface library
- 2022.7.26-----leetcode.1206
- Hcip day 2_ HCIA review comprehensive experiment
- Design of vision protector based on 51 single chip microcomputer
- 08 design of intelligent agricultural environmental monitoring system based on ZigBee
- 使用AW9523B芯片驱动16路LED时,LED出现误点亮的问题
- The place where the dream begins ---- first knowing C language (2)
- Method of realizing program startup and self startup through registry
- In simple terms, cchart's daily lesson - Lesson 59 of happy high school 4 comes to the same end by different ways, and the C code style of the colorful interface library
猜你喜欢

Chapter 2 develop user traffic interceptors

Dajiang Zhitu and CC have produced multiple copies of data. How to combine them into one and load them in the new earth map

【C语言】经典的递归问题

Recent answers - column

Tencent cloud lightweight application server purchase method steps!

Pytorch学习记录(二):张量

In simple terms, cchart's daily lesson - Lesson 59 of happy high school 4 comes to the same end by different ways, and the C code style of the colorful interface library

The place where the dream begins ---- first knowing C language (2)

4-4 object lifecycle

MVC three-tier architecture
随机推荐
数据库:MySQL基础+CRUD基本操作
Azure synapse analytics Performance Optimization Guide (4) -- optimize performance using result set caching
Galaxy securities online account opening commission, is online account opening safe for customer managers
Dynamic SQL
New features of ES6
Skiasharp's WPF self drawn bouncing ball (case version)
[2016] [paper notes] differential frequency tunable THz technology——
Section 6: introduction to cmake grammar
Geek challenge 2019 (review the loopholes)
第1章 需求分析与ssm环境准备
Thousands of tiles' tilt model browsing speeds up, saying goodbye to the embarrassment of jumping out one by one
第1章 开发第一个restful应用
动态sql
带你熟悉云网络的“电话簿”:DNS
Part II - C language improvement_ 7. Structure
Part II - C language improvement_ 6. Multidimensional array
The NFT market pattern has not changed. Can okaleido set off a new round of waves?
第1章 拦截器入门及使用技巧
Part II - C language improvement_ 11. Pretreatment
Pytorch learning record (II): tensor