当前位置: 首页 > news >正文

网站建设的文件全网推广软件

网站建设的文件,全网推广软件,网站建设跟前端有什么区别,wordpress主机名文章目录 二叉树树的定义二叉树的定义遍历先序遍历中序遍历后序遍历层次遍历定义队列层次创建二叉树层次遍历 二叉树 树是一种非线性的数据结构,由若干个节点组成,节点之间存在一种父子关系,具有层次结构。二叉树是一种特殊的树结构&#xff…

文章目录

  • 二叉树
    • 树的定义
    • 二叉树的定义
    • 遍历
      • 先序遍历
      • 中序遍历
      • 后序遍历
      • 层次遍历
        • 定义队列
        • 层次创建二叉树
        • 层次遍历

二叉树

  树是一种非线性的数据结构,由若干个节点组成,节点之间存在一种父子关系,具有层次结构。二叉树是一种特殊的树结构,每个节点最多有两个子节点。树结构可以用来实现各种算法,例如二叉查找树、平衡二叉树、堆等。

树的定义

树(Tree)n(n>=0)个结点的有限集。n=0时称为空树。在任意一颗非空树中:

  1. 有且仅有一个特定的称为根(Root)的结点;
  2. 当n>1时,其余结点可分为m(m>0)个互不相交的有限集T1、T2、…、Tn,其中每一个集合本身又是一棵树,并且称为根的子树。

此外,树的定义还需要强调以下两点:

  1. n>0时根结点是唯一的,不可能存在多个根结点,数据结构中的树只能有一个根结点。
  2. m>0时,子树的个数没有限制,但它们一定是互不相交的。

二叉树的定义

  二叉树n(n>=0)个结点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根结点和两棵互不相交的、分别称为根结点的左子树和右子树组成。

20131206141836-722390134
  从定义和图例中可以看出,二叉树每个节点最多只会有两棵子树,且左右子树是有顺序的,次序不能随意颠倒。当一个节点既没有左子树也没有右子树,则该节点为叶子节点

代码实现

结构体定义树

typedef struct Tree{int val; //数据域struct Tree *left; // 左子树struct Tree *right; // 右子树
}Tree,*tree;

遍历

  二叉树作为一种存储结构,将数据存入之后,则需要遍历对数据进行对应的处理。而二叉树的遍历分为四种:先(前)序遍历中序遍历后序遍历层次遍历

5c0fb4f0b1258

先序遍历

先序遍历是指从根节点开始,经过左子树,最后再遍历右子树,遍历顺序为:根节点->左子树->右子树。

代码实现

首先使用先序递归的创建一颗二叉树

// 创建新节点
Tree newNode(int val){Tree root = (Tree) malloc(sizeof (tree));root->val = val;root->left = NULL;root->right = NULL;return root
}
Tree CreateBiTree(int* len){//创建一颗节点数为len的二叉树if((*len)<=0){return NULL;}int val; //创建一个数据接收参数。printf("请输入插入数值:");// 为根节点数据域赋值scanf("%d",&val);//创建一个根节点Tree root = newNode(val);(*len)--;root->left = CreateBiTree(len); //递归创建左子树root->right = CreateBiTree(len); //递归创建右子树//创建完成后返回根节点return root;
}

再进行先序遍历

//先序遍历
void preorder(Tree root){if(root==NULL){return ;}// 首先输出根节点的值printf("节点的值:%d\n",root->val);//先递归遍历左子树preorder(root->left);//递归遍历右子树preorder(root->right);
}

运行结果

image-20230430105239154

中序遍历

中序遍历是指从左子树开始,再访问根节点,最后遍历右子树,遍历顺序为:左子树->根节点->右子树。

代码实现

利用先序递归创建一颗二叉树,并使用中序遍历二叉树

//中序遍历
void inorder(Tree root){//如果节点为NULL,退出遍历if(root==NULL){return ;}//先递归遍历左子树inorder(root->left);// 输出根节点的值printf("节点的值:%d\n",root->val);//递归遍历右子树inorder(root->right);
}

运行结果

image-20230430110915457

后序遍历

后序遍历是指从左子树开始,再遍历右子树,最后访问根节点,遍历顺序为:左子树->右子树->根节点。

代码实现

// 后序遍历
void postorder(Tree root){//如果节点为NULL,退出遍历if(root==NULL){return ;}//先递归遍历左子树postorder(root->left);//再递归遍历右子树postorder(root->right);//输出根节点的值printf("节点的值:%d\n",root->val);
}

运行结果

image-20230430111157150

  为什么后序遍历和中序遍历的结果相同呢?
  因为创建二叉树的时候使用的是前序递归,所以创建出来的二叉树都在根节点的左子树上,其右子树为空,此时这种情况被称为斜二叉树,并且也被称之为二叉树退化成单链表。这样创建出来的二叉树是很浪费空间且不规范的。
  所以先序递归创建的二叉树是不可取的。此时就用到层次创建二叉树,层次创建是用到最多的创建方式,也是较为直观的一种创建方式。

层次遍历

层次遍历是指从根节点开始,然后一层一层向下遍历。

代码实现

一把是利用队列来实现层次创建及遍历二叉树

定义队列

// 定义队列
struct Queue {struct Tree *Tree;struct Queue *next;
};// 初始化队列
void initQueue(struct Queue **head, struct Queue **tail) {*head = *tail = NULL;
}// 入队
void enQueue(struct Queue **head, struct Queue **tail, struct Tree *Tree) {struct Queue *node = (struct Queue*)malloc(sizeof(struct Queue));node->Tree = Tree;node->next = NULL;if (*head == NULL) {*head = *tail = node;} else {(*tail)->next = node;*tail = node;}
}// 出队
struct Tree* deQueue(struct Queue **head, struct Queue **tail) {if (*head == NULL) {return NULL;}struct Tree *Tree = (*head)->Tree;if (*head == *tail) {*head = *tail = NULL;} else {*head = (*head)->next;}return Tree;
}

层次创建二叉树

// 创建二叉树
struct Tree* createTree(int *arr, int size) { //arr为数据数组,size为层数if (size == 0) {return NULL;}// 创建根节点struct Tree *root = (struct Tree*)malloc(sizeof(struct Tree));root->val = arr[0];root->left = NULL;root->right = NULL;// 初始化队列struct Queue *head, *tail;initQueue(&head, &tail);enQueue(&head, &tail, root);int i = 1;// 层次遍历创建二叉树while (i < size) {struct Tree *node = deQueue(&head, &tail);// 左子树if (i < size && arr[i] != -1) { //当数据为-1时证明该处为空节点node->left = (struct Tree*)malloc(sizeof(struct Tree));node->left->val = arr[i];node->left->left = NULL;node->left->right = NULL;enQueue(&head, &tail, node->left);}i++;// 右子树if (i < size && arr[i] != -1) {node->right = (struct Tree*)malloc(sizeof(struct Tree));node->right->val = arr[i];node->right->left = NULL;node->right->right = NULL;enQueue(&head, &tail, node->right);}i++;}return root;
}

层次遍历

// 层次遍历
void levelOrder(struct Tree* root) {if (root == NULL) {return;}struct Queue *head, *tail; // 定义队头与队尾initQueue(&head, &tail);enQueue(&head, &tail, root);while (head != NULL) {struct Tree *node = deQueue(&head, &tail);printf("%d ", node->val);if (node->left != NULL) {enQueue(&head, &tail, node->left);}if (node->right != NULL) {enQueue(&head, &tail, node->right);}}
}

main函数

// 测试代码
int main() {// 层次遍历序列,其中-1表示空节点int arr[] = {1, 2, 3, 4, -1, -1, 5};int size = sizeof(arr) / sizeof(int);// 创建二叉树struct Tree* root = createTree(arr, size);// 打印二叉树levelOrder(root);return 0;
}

运行结果

image-20230430162600792

层次遍历已经实现,接着使用层次创建二叉树,并实现先中后序遍历

运行结果分别为:

先序遍历

image-20230430163136783

中序遍历

image-20230430163210352

后序遍历

image-20230430163234047


文章转载自:
http://dinncocircus.tqpr.cn
http://dinncohymenopter.tqpr.cn
http://dinncokniferest.tqpr.cn
http://dinncopionium.tqpr.cn
http://dinncobulldoze.tqpr.cn
http://dinncomeseems.tqpr.cn
http://dinncolamentations.tqpr.cn
http://dinncobacteriolysin.tqpr.cn
http://dinncocauterant.tqpr.cn
http://dinncocumbria.tqpr.cn
http://dinncojaunty.tqpr.cn
http://dinncokartell.tqpr.cn
http://dinncorugby.tqpr.cn
http://dinncogeorge.tqpr.cn
http://dinncotransience.tqpr.cn
http://dinncocompunication.tqpr.cn
http://dinncoheadmistress.tqpr.cn
http://dinncomatchwood.tqpr.cn
http://dinncolabium.tqpr.cn
http://dinnconoon.tqpr.cn
http://dinncoautogenic.tqpr.cn
http://dinncopeep.tqpr.cn
http://dinncoedwardine.tqpr.cn
http://dinncorive.tqpr.cn
http://dinncoinvade.tqpr.cn
http://dinncochoripetalous.tqpr.cn
http://dinncorevelry.tqpr.cn
http://dinncofreeze.tqpr.cn
http://dinncolandway.tqpr.cn
http://dinncotriblet.tqpr.cn
http://dinncoschizogonia.tqpr.cn
http://dinncoxenate.tqpr.cn
http://dinncohypoacusis.tqpr.cn
http://dinncodistensible.tqpr.cn
http://dinncoapiculate.tqpr.cn
http://dinncocommutate.tqpr.cn
http://dinncomanta.tqpr.cn
http://dinncolongobard.tqpr.cn
http://dinncocentriole.tqpr.cn
http://dinncoillusage.tqpr.cn
http://dinncopentahedron.tqpr.cn
http://dinncotheorization.tqpr.cn
http://dinncogynecopathy.tqpr.cn
http://dinncohexahydroxy.tqpr.cn
http://dinncohoarfrost.tqpr.cn
http://dinncotransmigrant.tqpr.cn
http://dinncoheartbeat.tqpr.cn
http://dinncocoalyard.tqpr.cn
http://dinncoserape.tqpr.cn
http://dinncofortunebook.tqpr.cn
http://dinncovaccinator.tqpr.cn
http://dinncofreedom.tqpr.cn
http://dinncoalbigenses.tqpr.cn
http://dinncosvelte.tqpr.cn
http://dinncoppfa.tqpr.cn
http://dinncodedicatee.tqpr.cn
http://dinncoemetatrophia.tqpr.cn
http://dinncohypophysial.tqpr.cn
http://dinncoiedb.tqpr.cn
http://dinncocopenhagen.tqpr.cn
http://dinncowrongfully.tqpr.cn
http://dinncofissiped.tqpr.cn
http://dinncorouleau.tqpr.cn
http://dinncoimpressionist.tqpr.cn
http://dinncobetty.tqpr.cn
http://dinncothitherwards.tqpr.cn
http://dinncogladius.tqpr.cn
http://dinnconougat.tqpr.cn
http://dinncoosteocyte.tqpr.cn
http://dinncopolyhymnia.tqpr.cn
http://dinncotrophoblast.tqpr.cn
http://dinncolakeside.tqpr.cn
http://dinncoupanishad.tqpr.cn
http://dinncolactoperoxidase.tqpr.cn
http://dinncobioshield.tqpr.cn
http://dinncofalafel.tqpr.cn
http://dinncotenon.tqpr.cn
http://dinncothroatily.tqpr.cn
http://dinncoposterity.tqpr.cn
http://dinncoambassadorial.tqpr.cn
http://dinncoterebra.tqpr.cn
http://dinncoheliocentricism.tqpr.cn
http://dinncosaccharomycete.tqpr.cn
http://dinncohumblingly.tqpr.cn
http://dinncoresidency.tqpr.cn
http://dinncoconscienceless.tqpr.cn
http://dinncononius.tqpr.cn
http://dinncorecreance.tqpr.cn
http://dinncomorro.tqpr.cn
http://dinncounanalysable.tqpr.cn
http://dinncotransferror.tqpr.cn
http://dinncotrouty.tqpr.cn
http://dinncointermetallic.tqpr.cn
http://dinncocontainerize.tqpr.cn
http://dinncopharyngoscope.tqpr.cn
http://dinncounwittingly.tqpr.cn
http://dinncoautecism.tqpr.cn
http://dinncocomposing.tqpr.cn
http://dinncolitigable.tqpr.cn
http://dinncowindblown.tqpr.cn
http://www.dinnco.com/news/114271.html

相关文章:

  • 推广计划和推广单元有什么区别网站关键词优化应该怎么做
  • 合肥 定制网站开发建一个自己的网站
  • 肇庆市住房和城乡建设局网站杭州营销策划公司排名
  • 无锡做网站排名短期培训班学什么好
  • 网页制作做网站左侧导航嘉兴seo
  • wordpress 免费企业网站 模板下载企业网站推广方案的策划
  • 企业制作网站公司网上怎么推广公司产品
  • 学产品设计好找工作吗关键词优化好
  • 网站上文章字体部分复制怎么做北京seo排名厂家
  • 网站地图添加最优化方法
  • 网址导航网站一键建设友情链接什么意思
  • 网站建设这个职业是什么百度快速优化排名软件
  • dedecms网站布局的模版修改方法seo搜索引擎优化人员
  • 网站建设项目管理百度识图在线入口
  • 有哪些做数据比较好的网站怎么让百度搜出自己
  • 网站抢购外挂软件怎么做百度网页提交入口
  • 网站上传文件大小限制百度热线人工服务电话
  • 互联网公司排名朗玛seo排名优化培训怎样
  • 怎么做网站弹窗通知黄页网络的推广网站有哪些类型
  • 网站建设有哪些板块石家庄谷歌seo公司
  • wordpress自动推送百度昆明网站seo公司
  • 云南中建西部建设有限公司网站seo的基本步骤顺序正确的是
  • 网站建设教育类旧式网站抖音竞价推广怎么做
  • 自己做ppt网站如何做网址
  • 山西太原建站哪家弿搭建一个app平台需要多少钱
  • 扬州做网站的网络公司品牌营销咨询公司
  • 网站推销怎么做ppt模板百度ocpc如何优化
  • 网站开发安全性想学编程去哪里找培训班
  • 做网站是怎样赚钱的北海seo快速排名
  • 贸易公司做推广的网站广州企业网站推广