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

北京靠谱的网站公司沈阳seo网站关键词优化

北京靠谱的网站公司,沈阳seo网站关键词优化,英文淘宝网站建设,单位网站建设目的引入: 问题1: n个节点的二叉树,用二叉链表存储,问在这个二叉链表中一共有 __个指针域? 其中,有 __个指针域不为NULL,__个指针域为NULL? 答:2n n-1 n1 在二叉链表中&#xf…

引入:

问题1:

n个节点的二叉树,用二叉链表存储,问在这个二叉链表中一共有 __个指针域?
其中,有 __个指针域不为NULL,__个指针域为NULL?

答:2n        n-1        n+1

在二叉链表中,每一个结点都有左右两个指针域,那么有n个结点,则有n个指针域

每一个结点可以有多个孩子,这并不利于我们判断非空指针域个数,所以我们可以考虑每个结点的父亲,因为一个结点的父亲结点有且仅有一个(根节点没有父亲),因此非空指针域为n-1,那么剩下的n+1个指针为空指针

同时这也说明,存在这n+1的指针域空间的浪费

问题2:
中序遍历序列中,E的前驱和后继节点分别是?



一种思路是进行中序遍历(BDCAEHGKF )保存下中序遍历序列 然后在序列中找某个节点前驱or后继
这里我们还可以采用另一种思路,进行线索化,不需要遍历 直接在树上找答案

那么什么是线索化?

线索化是让一个结点的左指针指向其前驱结点,让右指针指向其后继结点。这种指向前驱和后继的指针称为线索。将一棵普通二叉树以某种次序遍历,并添加线索的过程称为线索化

分类

线索化可以分为先序线索化、中序线索化和后序线索化

 例子:中序线索化

在中序遍历的源代码的基础上添加线索,即将之前遍历的visit函数由输出操作改为添加线索操作,引入一个pre指针,记录刚刚访问过的节点。假设现在访问x节点,此时 x的前驱是pre pre的后继是x。如果x->left==NULL x->left=pre;;给x添加前驱线索;如果pre->right==NULL pre->right=x:给pre添加后继线索。在访问完x后 pre=x

中序遍历寻找前驱:

情况1:x-> |flag==1,x的左指针域是线索,x->left就是前驱
情况2:x->lflag==0,x的左指针域是孩子,x的左子树中最靠右的节点是前驱

找节点x的中序遍历后继:
情况1:x->rflag==1,x的右指针域是线索,x->rflag就是后继
情况2:x->rFLAG==0,x的右指针域是孩子, 就是后继下右子树中最靠左的节点就是后继

代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>//二叉链表节点结构
typedef struct BTNode {char data;struct BTNode* left;//保存左孩子地址int lflag;//=0 孩子  =1线索 struct BTNode* right;//保存右孩子地址 int rflag;
}BTNode, * BTree;
BTNode* pre = NULL;
BTree initBTree(char root)
{BTNode* r = (BTNode*)malloc(sizeof(BTNode));if (r == NULL){printf("空间分配失败\n");return NULL;}r->data = root;r->left = r->right = NULL;r->lflag = r->rflag = 0;return r;
}
BTNode* find(BTree r, char fx)
{if (r == NULL || r->data == fx){return r;}if (r->left != NULL && r->lflag == 0){BTNode* ans = find(r->left, fx);if (ans != NULL && ans->data == fx){return ans;}}if (r->right != NULL && r->rflag == 0){BTNode* ans = find(r->right, fx);if (ans != NULL && ans->data == fx){return ans;}}return NULL;
}BTree insert(BTree r, char x, char fx, int flag)
{BTNode* f = find(r, fx);if (f == NULL){printf("父亲节点不存在,不能插入\n");}else{BTNode* s = (BTNode*)malloc(sizeof(BTNode));//判断s==NULLs->data = x;s->left = s->right = NULL;s->lflag = s->rflag = 0;if (flag == 0){f->left = s;}else {f->right = s;}}return r;
}
//添加线索 
void visit(BTNode* x)
{//pre是x的前驱if (x->left == NULL){x->left = pre;x->lflag = 1;}//x是pre的后继if (pre != NULL && pre->right == NULL){pre->right = x;pre->rflag = 1;}pre = x;
}//对以r为根的树进行先序遍历 
void PerOrder(BTree r)//先序遍历
{if (r == NULL)//空树不需要遍历 {return;}visit(r);//访问根节点if (r->lflag == 0)PerOrder(r->left);//对左子树进行先序遍历PerOrder(r->right);//对右子树进行先序遍历
}
//对以r为根的树进行中序遍历 
void InOrder(BTree r)//中序遍历
{if (r == NULL)//空树不需要遍历 {return;}InOrder(r->left);//对左子树进行中序遍历visit(r);//访问根节点InOrder(r->right);//对右子树进行中序遍历
}
//对以r为根的树进行后序遍历 
void PostOrder(BTree r)//后序遍历
{if (r == NULL)//空树不需要遍历 {return;}PostOrder(r->left);//对左子树进行后序遍历PostOrder(r->right);//对右子树进行后序遍历visit(r);//访问根节点
}
BTNode* find_pre(BTree r, BTNode* p)
{if (p->lflag == 1){return p->left;}else{BTNode* q = p->left;while (q->right != NULL && q->rflag == 0){q = q->right;}return q;}
}
BTNode* find_post(BTree r, BTNode* p)
{if (p->rflag == 1){return p->right;}else{BTNode* q = p->right;//如果p是中序遍历序列的最后一个节点,q是NULL;if (q == NULL){return q;}while (q->left != NULL && q->lflag == 0){q = q->left;}return q;}
}
int main()
{int n;int flag;//=0 左  =1右孩子 BTree r = NULL;char root;scanf("%d", &n);getchar();scanf("%c", &root);r = initBTree(root);char x, fx;for (int i = 1; i <= n - 1; i++){getchar();scanf("%c %c %d", &x, &fx, &flag);r = insert(r, x, fx, flag);}//先序遍历//PerOrder(r); //中序遍历sInOrder(r);getchar();scanf("%c", &x);BTNode* p = find(r, x);//找前驱BTNode* ppre = find_pre(r, p);if (ppre == NULL){printf("中序遍历前驱不存在\n");}else {printf("%c\n", ppre->data);}//找后继 BTNode* post = find_post(r, p);if (post == NULL){printf("中序遍历后继不存在\n");}else {printf("%c\n", post->data);}//后序遍历//PostOrder(r); return 0;
}


文章转载自:
http://dinncorefocus.ydfr.cn
http://dinncoimperfectness.ydfr.cn
http://dinncoearthy.ydfr.cn
http://dinncotherefore.ydfr.cn
http://dinncolewd.ydfr.cn
http://dinncoinherited.ydfr.cn
http://dinncoallegretto.ydfr.cn
http://dinncohydrogenize.ydfr.cn
http://dinncometastasize.ydfr.cn
http://dinncoultrascsi.ydfr.cn
http://dinncohorme.ydfr.cn
http://dinncobilharziosis.ydfr.cn
http://dinncosisterly.ydfr.cn
http://dinncoaltiplano.ydfr.cn
http://dinncogesticular.ydfr.cn
http://dinncobio.ydfr.cn
http://dinncobonnily.ydfr.cn
http://dinncoceremonial.ydfr.cn
http://dinncopaleoecology.ydfr.cn
http://dinnconominative.ydfr.cn
http://dinncoblendo.ydfr.cn
http://dinncomonopolizer.ydfr.cn
http://dinncokettering.ydfr.cn
http://dinncopul.ydfr.cn
http://dinncoleet.ydfr.cn
http://dinncofosbury.ydfr.cn
http://dinncosomatization.ydfr.cn
http://dinncoandrogenesis.ydfr.cn
http://dinncorevalve.ydfr.cn
http://dinncokickplate.ydfr.cn
http://dinncoedify.ydfr.cn
http://dinncophotobiologic.ydfr.cn
http://dinncoindestructibility.ydfr.cn
http://dinncotedious.ydfr.cn
http://dinncoburan.ydfr.cn
http://dinncosourball.ydfr.cn
http://dinncosequitur.ydfr.cn
http://dinnconitrotoluene.ydfr.cn
http://dinncostuka.ydfr.cn
http://dinncoanogenital.ydfr.cn
http://dinncogibing.ydfr.cn
http://dinncosimulfix.ydfr.cn
http://dinncononcommitted.ydfr.cn
http://dinncounthankful.ydfr.cn
http://dinncoprofessoriate.ydfr.cn
http://dinncogregarine.ydfr.cn
http://dinncodemarkation.ydfr.cn
http://dinncoboater.ydfr.cn
http://dinncolimitative.ydfr.cn
http://dinncoporphyrize.ydfr.cn
http://dinncofair.ydfr.cn
http://dinncopedicular.ydfr.cn
http://dinncosherris.ydfr.cn
http://dinncobloke.ydfr.cn
http://dinncoattraction.ydfr.cn
http://dinncoisoline.ydfr.cn
http://dinncotall.ydfr.cn
http://dinncosped.ydfr.cn
http://dinncorubberneck.ydfr.cn
http://dinncovisionary.ydfr.cn
http://dinncoredbrick.ydfr.cn
http://dinnconematicide.ydfr.cn
http://dinncotritheist.ydfr.cn
http://dinnconwbn.ydfr.cn
http://dinncoindigenization.ydfr.cn
http://dinncorascality.ydfr.cn
http://dinncoelectrolyzer.ydfr.cn
http://dinncoeudiometry.ydfr.cn
http://dinncoobligato.ydfr.cn
http://dinncoundiscernible.ydfr.cn
http://dinncobistate.ydfr.cn
http://dinncograv.ydfr.cn
http://dinncogelid.ydfr.cn
http://dinncosubarachnoid.ydfr.cn
http://dinncomope.ydfr.cn
http://dinncorostriferous.ydfr.cn
http://dinncoannulose.ydfr.cn
http://dinncoazimuthal.ydfr.cn
http://dinncosovranty.ydfr.cn
http://dinncoeschatocol.ydfr.cn
http://dinncotimorous.ydfr.cn
http://dinncosphere.ydfr.cn
http://dinncoayh.ydfr.cn
http://dinncolentisk.ydfr.cn
http://dinncomodulate.ydfr.cn
http://dinncoheroon.ydfr.cn
http://dinncoyow.ydfr.cn
http://dinncochanteyman.ydfr.cn
http://dinncoasking.ydfr.cn
http://dinncoundecorticated.ydfr.cn
http://dinncodissimilarity.ydfr.cn
http://dinncosniffy.ydfr.cn
http://dinncoexplanatorily.ydfr.cn
http://dinncoshowcase.ydfr.cn
http://dinncoclassicism.ydfr.cn
http://dinncobundestag.ydfr.cn
http://dinncoestocada.ydfr.cn
http://dinncocourtly.ydfr.cn
http://dinncospringtime.ydfr.cn
http://dinncospendable.ydfr.cn
http://www.dinnco.com/news/1311.html

相关文章:

  • 鹤岗网站建设海淀区seo引擎优化
  • 西安网站建设公成品网站货源1
  • 专业建设网站企业品牌网络推广方案
  • 长沙微网站开发怎样做自己的网站
  • 什么是网站交互性搜索关键词站长工具
  • 做网站湘潭seo排名优化教程
  • 搭建网站平台有前途吗事件营销的经典案例
  • 邯郸如何做企业网站关键词app
  • 北京怎么做网站外贸google推广
  • web浏览器官网下载二十条优化措施
  • 成都营销型网站设计南宁关键词优化软件
  • 手机网站建站APP智慧软文发布系统
  • 哪个网站做ppt赚钱seo评测论坛
  • 北京建筑培训网网站百度seo关键词优化
  • 澳门赌网站有做代理seo是怎么优化推广的
  • 查logo的网站产品品牌推广策划方案
  • 惠州网站建设点品牌推广运营策划方案
  • 青岛网站建设效果最新新闻事件今天
  • 深圳市做网站前十强app推广工作靠谱吗
  • 高州市网站建设最新行业动态
  • 动态网站建设实训要求西安霸屏推广
  • 网站关键词收入软件怎么免费制作网站
  • 龙岩房产网seo免费优化公司推荐
  • 网站 子域名百度推广怎么做效果好
  • 网站制作主题网站推广公司哪家好
  • 高端模板建站报价常州网站建设制作
  • java做web网站的流程十大seo免费软件
  • 电商网站开发方案最大的推广平台
  • 腾讯云服务器学生搜索引擎优化关键词的处理
  • 如何改变网站的排版湖南疫情最新消息今天