search
HomeCommon ProblemIs a binary linked list the storage structure of a binary tree?

Binary linked list is the storage structure of binary tree. Binary linked list is a binary linked list implementation of a tree (child sibling representation), using a binary linked list as the storage structure of the tree. The two link fields of a node in the linked list point to the first child node and the second child node of the node respectively.

Is a binary linked list the storage structure of a binary tree?

Structural description (Recommended learning: web front-end video tutorial)

typedef struct CSNode{
ElemType data;
struct CSNode *firstchild , *netsibling;
} CSNode,* CSTree;

Because of the binary tree The storage structure is relatively simple and easy to process, so sometimes it is necessary to convert a complex tree into a simple binary tree before processing.

Function definition of binary linked list

bitree.h

//二叉链表定义
#include <iostream>
using namespace std;
typedef char TElemType;
struct BiTNode{
TElemType data;
BiTNode *lchild,*rchild;
};
typedef BiTNode *BiTree;
void initBiTree(BiTree &T);
void createBiTree(BiTree &T);
void preOrderTraverse(BiTree T,void (*visit)(TElemType)); //递归前序遍历
void preOrderTraverse1(BiTree T,void (*visit)(TElemType)); //非递归前序遍历
void inOrderTraverse(BiTree T,void (*visit)(TElemType)); //递归中序遍历
void postOrderTraverse(BiTree T,void (*visit)(TElemType)); //递归后序遍历
void levelOrderTraverse(BiTree T,void (*visit)(TElemType)); //层序遍历
bitree.cpp
#include "bitree.h"
void initBiTree(BiTree &T){ //构造空二叉树T
T=NULL;
}
void createBiTree(BiTree &T){
//按先序次序输入二叉树中结点的值(&#39;#&#39;表示空格),构造二叉链表表示的二叉树T。
TElemType ch;
cin>>ch;
if(ch==&#39;#&#39;) // 空
T=NULL;
else{
T=new BiTNode;
if(!T)
exit(1);
T->data=ch; // 生成根结点
createBiTree(T->lchild); // 构造左子树
createBiTree(T->rchild); // 构造右子树
}
}
void preOrderTraverse(BiTree T,void (*visit)(TElemType)){
// 先序递归遍历T,对每个结点调用函数Visit一次且仅一次
if(T){ // T不空
visit(T->data); // 先访问根结点
preOrderTraverse(T->lchild,visit); // 再先序遍历左子树
preOrderTraverse(T->rchild,visit); // 最后先序遍历右子树
}
}
void preOrderTraverse1(BiTree T,void (*visit)(TElemType)){
//前序遍历二叉树T的非递归算法(利用栈),对每个数据元素调用函数Visit
BiTree s[100];
int top=0; //top为栈顶指针
while((T!=NULL)||(top>0)){
while(T!=NULL){
visit(T->data);
s[top++]=T;
T=T->lchild;
}
T=s[--top];
T=T->rchild;
}
}
void inOrderTraverse(BiTree T,void (*visit)(TElemType)){
//中序递归遍历T,对每个结点调用函数Visit一次且仅一次
if(T){
inOrderTraverse(T->lchild,visit); // 先中序遍历左子树
visit(T->data); // 再访问根结点
inOrderTraverse(T->rchild,visit); // 最后中序遍历右子树
}
}
void postOrderTraverse(BiTree T,void (*visit)(TElemType)){
//后序递归遍历T,对每个结点调用函数Visit一次且仅一次
if(T){
inOrderTraverse(T->lchild,visit); // 后序遍历左子树
inOrderTraverse(T->rchild,visit); // 再后序遍历右子树
visit(T->data); // 最后访问根结点
}
}
void levelOrderTraverse(BiTree T,void (*visit)(TElemType)){
//层序遍历T(利用队列),对每个结点调用函数Visit一次且仅一次
BiTree q[100],p;
int f,r; // f,r类似于头尾指针
q[0]=T;
f=0;
r=1;
while(f<r){
p=q[f++]; //出队
visit(p->data);
if(p->lchild!=NULL)
q[r++]=p->lchild; //入队
if(p->rchild!=NULL)
q[r++]=p->rchild; //入队
}
}

The above is the detailed content of Is a binary linked list the storage structure of a binary tree?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool