453 lines
28 KiB
HTML
453 lines
28 KiB
HTML
|
|
<!doctype html>
|
|||
|
|
<html lang="zh-Hans">
|
|||
|
|
<head>
|
|||
|
|
<meta charset="utf-8">
|
|||
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|||
|
|
<title>DS-12 树、森林与二叉树的转换及遍历 · 孩子兄弟表示法与遍历序列互推 · 408 知识卡片</title>
|
|||
|
|
<meta name="description" content="孩子兄弟表示法:左指针挂长子,右指针挂兄弟。树与森林如何折成唯一的一棵二叉树,遍历序列怎么互推,无左/无右孩子的结点各数什么——408 高频考点,可拖动、可跑一遍的讲解页。">
|
|||
|
|
<link rel="canonical" href="https://example.com/ds12/">
|
|||
|
|
<meta property="og:type" content="website">
|
|||
|
|
<meta property="og:url" content="https://example.com/ds12/">
|
|||
|
|
<meta property="og:title" content="DS-12 树、森林与二叉树的转换及遍历 · 408 知识卡片">
|
|||
|
|
<meta property="og:description" content="左孩子、右兄弟;先根变先序、后根变中序。一棵树折成二叉树的全过程,可以拖着看。">
|
|||
|
|
<meta property="og:image" content="https://example.com/ds12/og.png">
|
|||
|
|
<meta property="og:image:width" content="1200">
|
|||
|
|
<meta property="og:image:height" content="630">
|
|||
|
|
<meta name="twitter:card" content="summary_large_image">
|
|||
|
|
<link rel="icon" href="favicon.svg" type="image/svg+xml">
|
|||
|
|
<link rel="apple-touch-icon" href="apple-touch-icon.png">
|
|||
|
|
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#e9e5db">
|
|||
|
|
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#191712">
|
|||
|
|
<meta name="color-scheme" content="light">
|
|||
|
|
<link rel="stylesheet" href="styles.css">
|
|||
|
|
<script src="../../assets/back.js"></script>
|
|||
|
|
<script>document.documentElement.className += ' js';</script>
|
|||
|
|
</head>
|
|||
|
|
<body>
|
|||
|
|
<a class="skip" href="#main">跳到正文</a>
|
|||
|
|
|
|||
|
|
<header class="site-head" id="siteHead">
|
|||
|
|
<div class="site-head__in">
|
|||
|
|
<a class="brand" href="#top">
|
|||
|
|
<span class="brand__code">DS-12</span>
|
|||
|
|
<span class="brand__name">408 知识卡片</span>
|
|||
|
|
</a>
|
|||
|
|
<nav class="nav" aria-label="章节导航">
|
|||
|
|
<ul>
|
|||
|
|
<li><a href="#rep">表示</a></li>
|
|||
|
|
<li><a href="#convert">转换</a></li>
|
|||
|
|
<li><a href="#traverse">遍历</a></li>
|
|||
|
|
<li><a href="#count">计数</a></li>
|
|||
|
|
<li><a href="#quiz">问答</a></li>
|
|||
|
|
</ul>
|
|||
|
|
</nav>
|
|||
|
|
</div>
|
|||
|
|
</header>
|
|||
|
|
|
|||
|
|
<main id="main">
|
|||
|
|
|
|||
|
|
<!-- ═══════════ HERO · 转换仪器 ═══════════ -->
|
|||
|
|
<section class="hero" id="top">
|
|||
|
|
<div class="hero__grid-underlay" aria-hidden="true"></div>
|
|||
|
|
|
|||
|
|
<div class="hero__head">
|
|||
|
|
<p class="eyebrow">408 · 数据结构 · 知识卡片 <span class="eyebrow__sep">/</span> DS-12</p>
|
|||
|
|
<h1 class="hero__title">一棵树,<br>折成一棵二叉树</h1>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<dl class="titleblock" aria-label="本页图例规格">
|
|||
|
|
<div class="titleblock__row"><dt>图号</dt><dd>DS-12 / 00</dd></div>
|
|||
|
|
<div class="titleblock__row"><dt>运行示例</dt><dd>n = 10 · 叶子 6</dd></div>
|
|||
|
|
<div class="titleblock__row"><dt>度序列</dt><dd>3 · 3 · 2 · 1</dd></div>
|
|||
|
|
<div class="titleblock__row"><dt>转换</dt><dd>左孩子 · 右兄弟</dd></div>
|
|||
|
|
<div class="titleblock__row"><dt>绘制</dt><dd>本页实时 SVG</dd></div>
|
|||
|
|
</dl>
|
|||
|
|
|
|||
|
|
<div class="hero__side">
|
|||
|
|
<p class="hero__deck">孩子兄弟表示法:左指针挂长子,右指针挂兄弟。任何树、任何森林,都唯一对应一棵二叉树。</p>
|
|||
|
|
<p class="hero__meta">常考三件事:按“左孩子右兄弟”画出转换后的二叉树 · 树/森林遍历序列与二叉树遍历序列互推 · 数无左/无右孩子的结点个数。</p>
|
|||
|
|
<div class="hero__legend" aria-hidden="true">
|
|||
|
|
<span class="lg lg--child">左 · firstChild 长子</span>
|
|||
|
|
<span class="lg lg--sib">右 · nextSibling 兄弟</span>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<figure class="hero__stage" id="heroStage">
|
|||
|
|
<div class="stage" data-diagram="hero"></div>
|
|||
|
|
<figcaption class="fig-cap">
|
|||
|
|
<span class="fig-cap__no">图 0</span>
|
|||
|
|
<span class="fig-cap__txt">左孩子右兄弟。拖动下面的进度条,看这棵树怎么把自己重新挂一遍。</span>
|
|||
|
|
</figcaption>
|
|||
|
|
|
|||
|
|
<div class="scrub" id="heroScrub">
|
|||
|
|
<div class="scrub__ends">
|
|||
|
|
<span class="scrub__end">树</span>
|
|||
|
|
<span class="scrub__hint" id="scrubHint">拖动 · 转换进度</span>
|
|||
|
|
<span class="scrub__end">二叉树</span>
|
|||
|
|
</div>
|
|||
|
|
<div class="scrub__track" id="scrubTrack" role="slider" tabindex="0"
|
|||
|
|
aria-label="树到二叉树的转换进度" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" aria-valuetext="0%">
|
|||
|
|
<div class="scrub__ticks" aria-hidden="true"></div>
|
|||
|
|
<div class="scrub__fill" id="scrubFill"></div>
|
|||
|
|
<div class="scrub__knob" id="scrubKnob" aria-hidden="true"></div>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</figure>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<!-- ═══════════ 口诀 ═══════════ -->
|
|||
|
|
<section class="creed" id="creed" aria-labelledby="creedTitle">
|
|||
|
|
<p class="eyebrow eyebrow--onink">口诀 · 背下来就能拿分</p>
|
|||
|
|
<h2 class="creed__line" id="creedTitle">
|
|||
|
|
<span class="creed__seg">左孩子、右兄弟;</span>
|
|||
|
|
<span class="creed__seg">先根变先序、后根变中序;</span>
|
|||
|
|
<span class="creed__seg">根无右,叶子无左。</span>
|
|||
|
|
</h2>
|
|||
|
|
<p class="creed__foot">先根落“先”、后根落“中”——这是本卡片全部推理的起点。</p>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<!-- ═══════════ 01 表示 ═══════════ -->
|
|||
|
|
<section class="sec sec--split" id="rep" aria-labelledby="repTitle">
|
|||
|
|
<div class="sec__head">
|
|||
|
|
<p class="eyebrow"><span class="sec__no">01</span> 表示 · 孩子兄弟表示法</p>
|
|||
|
|
<h2 class="sec__title" id="repTitle">每个结点只带两根指针</h2>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="sec__body">
|
|||
|
|
<div class="prose">
|
|||
|
|
<p>孩子兄弟表示法为每个结点设两个指针:<b>firstChild</b> 记下它的长子(第一个孩子),<b>nextSibling</b> 记下它的下一个兄弟。任何一棵树、任何一片森林,都唯一对应一棵二叉树——因为每个结点在这两个方向上都只有一条出边。</p>
|
|||
|
|
<p>这也正是二叉链表:所谓“二叉链表即孩子兄弟法”,存储结构是同一套东西(DS-08)。</p>
|
|||
|
|
<div class="formal" role="note" aria-label="形式化定义">
|
|||
|
|
<code>lchild(v) = firstChild(v)</code>
|
|||
|
|
<code>rchild(v) = nextSibling(v)</code>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<figure class="fig" id="anatomyFig">
|
|||
|
|
<div class="stage stage--anatomy" data-diagram="anatomy"></div>
|
|||
|
|
<figcaption class="fig-cap">
|
|||
|
|
<span class="fig-cap__no">图 1</span>
|
|||
|
|
<span class="fig-cap__txt">悬停或点住任一结点:朱砂色是它的长子边,靛蓝色是它的兄弟边。</span>
|
|||
|
|
</figcaption>
|
|||
|
|
</figure>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<!-- ═══════════ 02 转换 ═══════════ -->
|
|||
|
|
<section class="sec sec--wide" id="convert" aria-labelledby="convertTitle">
|
|||
|
|
<div class="sec__head">
|
|||
|
|
<p class="eyebrow"><span class="sec__no">02</span> 转换 · 左孩子右兄弟</p>
|
|||
|
|
<h2 class="sec__title" id="convertTitle">森林怎么折</h2>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="sec__body sec__body--convert">
|
|||
|
|
<ol class="rules">
|
|||
|
|
<li><span class="rules__n">1</span><span class="rules__t">结点的<b>左指针</b> → 第一个孩子。</span></li>
|
|||
|
|
<li><span class="rules__n">2</span><span class="rules__t">结点的<b>右指针</b> → 下一个兄弟。</span></li>
|
|||
|
|
<li><span class="rules__n">3</span><span class="rules__t">一棵树转出的二叉树,<b>根没有右孩子</b>。</span></li>
|
|||
|
|
<li><span class="rules__n">4</span><span class="rules__t">森林把各树根当兄弟:第一棵树根做二叉树的根,<b>右链</b>依次挂上其余树根;逆转换沿右链拆回各棵树。</span></li>
|
|||
|
|
</ol>
|
|||
|
|
|
|||
|
|
<figure class="fig fig--big" id="forestFig">
|
|||
|
|
<div class="stage" data-diagram="forest"></div>
|
|||
|
|
<figcaption class="fig-cap">
|
|||
|
|
<span class="fig-cap__no">图 2</span>
|
|||
|
|
<span class="fig-cap__txt" id="forestNote">一片森林:A 是第一棵树的根,D、G 各自成树。</span>
|
|||
|
|
</figcaption>
|
|||
|
|
|
|||
|
|
<div class="stepper" id="forestStepper" role="tablist" aria-label="森林转换步骤">
|
|||
|
|
<button type="button" class="step is-on" role="tab" aria-selected="true" data-step="0"><span class="step__n">①</span><span class="step__t">原森林</span></button>
|
|||
|
|
<button type="button" class="step" role="tab" aria-selected="false" data-step="1"><span class="step__n">②</span><span class="step__t">各树根当兄弟</span></button>
|
|||
|
|
<button type="button" class="step" role="tab" aria-selected="false" data-step="2"><span class="step__n">③</span><span class="step__t">长子挂左</span></button>
|
|||
|
|
<button type="button" class="step" role="tab" aria-selected="false" data-step="3"><span class="step__n">④</span><span class="step__t">折完</span></button>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<p class="readout" id="forestReadout">
|
|||
|
|
<span class="readout__k">右链结点数</span>
|
|||
|
|
<span class="readout__v" data-count="3">3</span>
|
|||
|
|
<span class="readout__k">= 树的棵数</span>
|
|||
|
|
</p>
|
|||
|
|
</figure>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<!-- ═══════════ 03 遍历 ═══════════ -->
|
|||
|
|
<section class="sec sec--split" id="traverse" aria-labelledby="traverseTitle">
|
|||
|
|
<div class="sec__head">
|
|||
|
|
<p class="eyebrow"><span class="sec__no">03</span> 遍历 · 序列互推</p>
|
|||
|
|
<h2 class="sec__title" id="traverseTitle">序列没变,只是名字变了</h2>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="sec__body">
|
|||
|
|
<div class="prose">
|
|||
|
|
<p>这是本卡片的核心考点。转换不打乱任何一个结点的先后:树的先根序列,逐字等于二叉树的先序序列;树的后根序列,逐字等于二叉树的中序序列。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<table class="tbl" id="mapTable">
|
|||
|
|
<caption class="sr-only">树与森林的遍历和转换后二叉树的遍历对应关系</caption>
|
|||
|
|
<thead>
|
|||
|
|
<tr><th scope="col">树 / 森林</th><th scope="col">转换后的二叉树</th></tr>
|
|||
|
|
</thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr><td>树 · 先根遍历</td><td><b>先序</b></td></tr>
|
|||
|
|
<tr><td>树 · 后根遍历</td><td><b>中序</b></td></tr>
|
|||
|
|
<tr><td>森林 · 先序</td><td><b>先序</b></td></tr>
|
|||
|
|
<tr><td>森林 · 中序(每棵树做后根)</td><td><b>中序</b></td></tr>
|
|||
|
|
<tr class="tbl__exception"><td>层序遍历</td><td>无对应</td></tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
|
|||
|
|
<figure class="fig fig--big" id="traceFig">
|
|||
|
|
<div class="trace-bar" id="traceBar" role="group" aria-label="选择遍历方式">
|
|||
|
|
<button type="button" class="tg is-on" data-mode="pre">跑一遍 · 先根</button>
|
|||
|
|
<button type="button" class="tg" data-mode="post">跑一遍 · 后根</button>
|
|||
|
|
<button type="button" class="tg" data-mode="level">跑一遍 · 层序</button>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="stage" data-diagram="trace"></div>
|
|||
|
|
|
|||
|
|
<figcaption class="fig-cap">
|
|||
|
|
<span class="fig-cap__no">图 3</span>
|
|||
|
|
<span class="fig-cap__txt" id="traceNote">同一棵树,两套算法。点上面任意一条跑一遍。</span>
|
|||
|
|
</figcaption>
|
|||
|
|
|
|||
|
|
<div class="seqs" id="seqs" aria-live="polite">
|
|||
|
|
<div class="seq">
|
|||
|
|
<span class="seq__k">树 · <b id="seqK1">先根</b></span>
|
|||
|
|
<span class="seq__v" id="seqV1"></span>
|
|||
|
|
</div>
|
|||
|
|
<div class="seq">
|
|||
|
|
<span class="seq__k">二叉树 · <b id="seqK2">先序</b></span>
|
|||
|
|
<span class="seq__v" id="seqV2"></span>
|
|||
|
|
</div>
|
|||
|
|
<p class="seq__verdict" id="seqVerdict"></p>
|
|||
|
|
</div>
|
|||
|
|
</figure>
|
|||
|
|
|
|||
|
|
<p class="prose prose--note">层序是唯一的例外:树的层序与转换后二叉树的层序<b>没有</b>对应关系。上图切到“层序”,两行序号会当场对不上——这就是常考的那个坑。</p>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<!-- ═══════════ 04 计数 ═══════════ -->
|
|||
|
|
<section class="sec sec--split sec--count" id="count" aria-labelledby="countTitle">
|
|||
|
|
<div class="sec__head">
|
|||
|
|
<p class="eyebrow"><span class="sec__no">04</span> 计数 · 结构特征</p>
|
|||
|
|
<h2 class="sec__title" id="countTitle">无左数叶子,<br>无右数“最后一个孩子 + 根”</h2>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="sec__body">
|
|||
|
|
<div class="prose">
|
|||
|
|
<p>转换之后,看一眼二叉树就能读出原树的结构:没有左孩子,说明它在原树里是叶子;没有右孩子,说明它没有下一个兄弟——要么是父亲的最后一个孩子,要么它自己就是根。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="tally" id="tally">
|
|||
|
|
<div class="tally__row"><span class="tally__k">结点数 n</span><span class="tally__v" data-count="10">10</span><span class="tally__u">不变</span></div>
|
|||
|
|
<div class="tally__row"><span class="tally__k">边数</span><span class="tally__v" data-count="9">9</span><span class="tally__u">n − 1</span></div>
|
|||
|
|
<div class="tally__row is-key"><span class="tally__k">无左孩子的结点数</span><span class="tally__v" data-count="6">6</span><span class="tally__u">= 叶子数</span></div>
|
|||
|
|
<div class="tally__row is-key"><span class="tally__k">无右孩子的结点数</span><span class="tally__v" data-count="5">5</span><span class="tally__u">= 度≠0 的结点数 + 1</span></div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<figure class="fig fig--big" id="countFig">
|
|||
|
|
<div class="mark-bar" id="markBar" role="group" aria-label="在图上圈出对应结点">
|
|||
|
|
<button type="button" class="tg is-on" data-mark="left">圈:无左孩子</button>
|
|||
|
|
<button type="button" class="tg is-on" data-mark="right">框:无右孩子</button>
|
|||
|
|
</div>
|
|||
|
|
<div class="stage" data-diagram="count"></div>
|
|||
|
|
<figcaption class="fig-cap">
|
|||
|
|
<span class="fig-cap__no">图 4</span>
|
|||
|
|
<span class="fig-cap__txt">同一棵二叉树。圈=没有左孩子(6 个),方框=没有右孩子(5 个)——两个集合不重合,J 同时在两边。</span>
|
|||
|
|
</figcaption>
|
|||
|
|
</figure>
|
|||
|
|
|
|||
|
|
<div class="formal formal--wide" role="note" aria-label="形式化与复杂度">
|
|||
|
|
<code>lchild(v) = firstChild(v)</code>
|
|||
|
|
<code>rchild(v) = nextSibling(v)</code>
|
|||
|
|
<code>先根(T) = r, 先根(T₁)…先根(Tₖ) ⇔ 先序</code>
|
|||
|
|
<code>后根(T) = 后根(T₁)…后根(Tₖ), r ⇔ 中序</code>
|
|||
|
|
<code>deg(v) = 长子沿右链的结点数</code>
|
|||
|
|
<code>遍历 O(n) · 递归栈 O(h)</code>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<!-- ═══════════ 05 易错点 ═══════════ -->
|
|||
|
|
<section class="sec sec--quiet" id="pitfalls" aria-labelledby="pitTitle">
|
|||
|
|
<div class="sec__head">
|
|||
|
|
<p class="eyebrow"><span class="sec__no">05</span> 易错点 · 一个字都不能错</p>
|
|||
|
|
<h2 class="sec__title sec__title--sm" id="pitTitle">四条最容易丢分的地方</h2>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<ol class="pit">
|
|||
|
|
<li class="pit__item">
|
|||
|
|
<span class="pit__mark" aria-hidden="true">✕</span>
|
|||
|
|
<p><b>树后根 = 二叉树中序,不是后序!</b>先根落“先”、后根落“中”。看到“后根”两个字就写“后序”,是最常见的一次性丢分。</p>
|
|||
|
|
</li>
|
|||
|
|
<li class="pit__item">
|
|||
|
|
<span class="pit__mark" aria-hidden="true">✕</span>
|
|||
|
|
<p><b>“根无右孩子”只对一棵树成立。</b>森林转换后,右链上的结点数 = 树的棵数,根当然有右兄弟。</p>
|
|||
|
|
</li>
|
|||
|
|
<li class="pit__item">
|
|||
|
|
<span class="pit__mark" aria-hidden="true">✕</span>
|
|||
|
|
<p><b>无左孩子数 ≠ 无右孩子数。</b>前者数的是叶子(没有长子),后者数的是“每个父亲的最后一个孩子 + 根”(没有兄弟)。两者落在不同的结点集合上,只有在 2×(度≠0 的结点数) + 1 = n 时才会碰巧相等。</p>
|
|||
|
|
</li>
|
|||
|
|
<li class="pit__item pit__item--fix">
|
|||
|
|
<span class="pit__mark" aria-hidden="true">✓</span>
|
|||
|
|
<p><b>唯一性:要分清是哪一对序列。</b>二叉树的<b>先序 + 后序</b>不能唯一还原(分不清一个结点该归左子树还是右子树);而树的<b>先根 + 后根</b> ⇔ 二叉树的<b>先序 + 中序</b>,是可以唯一还原的——对应关系本身就给出了还原办法。口诀只背到“先根落先、后根落中”,这里就推得出来。</p>
|
|||
|
|
</li>
|
|||
|
|
</ol>
|
|||
|
|
<p class="footnote">※ 第 4 条按对应关系核算过:有序树、结点互异时,(先根, 后根) 与 (先序, 中序) 是同一组信息,而后者唯一确定二叉树,转换又是一一对应。若你的讲义写的是“先根+后根不唯一”,请对齐到“二叉树先序+后序不唯一”。</p>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<!-- ═══════════ 06 关联 ═══════════ -->
|
|||
|
|
<section class="sec sec--index" id="map" aria-labelledby="mapTitle">
|
|||
|
|
<div class="sec__head">
|
|||
|
|
<p class="eyebrow"><span class="sec__no">06</span> 关联 · 一张卡片不是孤岛</p>
|
|||
|
|
<h2 class="sec__title sec__title--sm" id="mapTitle">和它接在一起的六张卡</h2>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<ul class="idx">
|
|||
|
|
<li class="idx__row"><span class="idx__code">DS-05</span><span class="idx__name">队列</span><span class="idx__why">层序遍历借助队列,与本卡片的“层序无对应”一起考。</span></li>
|
|||
|
|
<li class="idx__row"><span class="idx__code">DS-08</span><span class="idx__name">存储结构</span><span class="idx__why">二叉链表即孩子兄弟表示法,本卡片的指针定义直接用它。</span></li>
|
|||
|
|
<li class="idx__row"><span class="idx__code">DS-09</span><span class="idx__name">遍历算法</span><span class="idx__why">先序/中序/后序的递归与非递归实现可直接套用。</span></li>
|
|||
|
|
<li class="idx__row"><span class="idx__code">DS-10</span><span class="idx__name">二叉树还原</span><span class="idx__why">先序+中序唯一还原二叉树;树要先转成二叉树再还原。</span></li>
|
|||
|
|
<li class="idx__row"><span class="idx__code">DS-11</span><span class="idx__name">线索化</span><span class="idx__why">对转换后的二叉树做线索化,线索指向的正是原树的遍历次序。</span></li>
|
|||
|
|
<li class="idx__row"><span class="idx__code">DS-13</span><span class="idx__name">哈夫曼树</span><span class="idx__why">最优二叉树的构造与结点计数,和本卡片的度/叶子计数同一套数法。</span></li>
|
|||
|
|
</ul>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<!-- ═══════════ 07 拓展 ═══════════ -->
|
|||
|
|
<section class="sec sec--split" id="beyond" aria-labelledby="beyondTitle">
|
|||
|
|
<div class="sec__head">
|
|||
|
|
<p class="eyebrow"><span class="sec__no">07</span> 拓展 · 工程里它在哪</p>
|
|||
|
|
<h2 class="sec__title sec__title--sm" id="beyondTitle">firstChild / nextSibling<br>不是为了考试发明的</h2>
|
|||
|
|
</div>
|
|||
|
|
<div class="sec__body">
|
|||
|
|
<div class="prose">
|
|||
|
|
<p>first-child / next-sibling 是编译器 AST 和文件系统目录树的经典实现:每个结点只需固定两条指针,就装下了任意叉数的树。LISP 的 CAR / CDR 本质就是这套转换。而 DOM 的 firstChild / nextSibling 遍历,正是它在工程里的原型。</p>
|
|||
|
|
</div>
|
|||
|
|
<pre class="code" tabindex="0"><code><span class="code__c">// DOM 的孩子兄弟遍历 —— 与本卡片的表示法同构</span>
|
|||
|
|
for (let c = node.firstChild; c; c = c.nextSibling) {
|
|||
|
|
visit(c);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
<span class="code__c">// 逆转换:沿右链拆回各棵树</span>
|
|||
|
|
for (let r = root; r; r = r.right) {
|
|||
|
|
restoreTree(r); <span class="code__c">// r.left 还原成 r 的孩子们</span>
|
|||
|
|
}</code></pre>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<!-- ═══════════ 08 引导提问 ═══════════ -->
|
|||
|
|
<section class="sec sec--narrow" id="think" aria-labelledby="thinkTitle">
|
|||
|
|
<div class="sec__head">
|
|||
|
|
<p class="eyebrow"><span class="sec__no">08</span> 引导提问 · 先自己想</p>
|
|||
|
|
<h2 class="sec__title sec__title--sm" id="thinkTitle">三个问题,答案在折叠里</h2>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="qa">
|
|||
|
|
<details class="acc">
|
|||
|
|
<summary><span class="acc__n">Q1</span>为什么树的后根遍历恰好对应二叉树的中序遍历?用递归定义对推一遍。</summary>
|
|||
|
|
<div class="acc__body">
|
|||
|
|
<p>看转换结果:根 r 的<b>右子树一定为空</b>(根没有兄弟),<b>左子树</b>正是“r 的孩子们”折出来的那一棵。于是</p>
|
|||
|
|
<p class="acc__formula">中序 = 中序(左) + r + 中序(右) = 中序(孩子们) + r</p>
|
|||
|
|
<p>而“孩子们”是一片森林,森林的中序按定义就是各棵树的后根依次相接:后根(T₁)…后根(Tₖ)。合起来正是后根(T) = 后根(T₁)…后根(Tₖ), r。先根同理:先根 = r + 各树先根,正是先序。所以“先根落先、后根落中”是推出来的,不必背。</p>
|
|||
|
|
</div>
|
|||
|
|
</details>
|
|||
|
|
|
|||
|
|
<details class="acc">
|
|||
|
|
<summary><span class="acc__n">Q2</span>10 个结点、2 个度为 3 的树,转成二叉树后“无右孩子数”和“叶子数”各是多少?为何不等?</summary>
|
|||
|
|
<div class="acc__body">
|
|||
|
|
<p>两个计数用的是两条不同的公式:</p>
|
|||
|
|
<p class="acc__formula">叶子数 = 无左孩子数 = n − (度≠0 的结点数)<br>无右孩子数 = (度≠0 的结点数) + 1</p>
|
|||
|
|
<p>题干只给了“度为 3 的结点 2 个”,要算“无右孩子数”还差<b>度≠0 的结点总数</b>(度为 2、度为 1 的各有几个)。以本页图 4 那棵树为例:10 个结点,度为 3 的 2 个、度为 2 的 1 个、度为 1 的 1 个,于是叶子 6 个、无右孩子 5 个。</p>
|
|||
|
|
<p>为什么不等:前者数的是“没有长子”的结点(原树的叶子),后者数的是“没有下一个兄弟”的结点(每个父亲的最后一个孩子,再加上根)。它们落在不同的结点集合上,本来就没有理由相等。</p>
|
|||
|
|
</div>
|
|||
|
|
</details>
|
|||
|
|
|
|||
|
|
<details class="acc">
|
|||
|
|
<summary><span class="acc__n">Q3</span>只给先根+后根序列时,什么情况下树能唯一确定?</summary>
|
|||
|
|
<div class="acc__body">
|
|||
|
|
<p>先看清对应关系再回答:树的先根+后根 = 二叉树的先序+中序,而<b>先序+中序唯一确定一棵二叉树</b>,转换又是一一对应。所以有序树、结点互异时,<b>先根+后根本来就能唯一确定这棵树</b>。</p>
|
|||
|
|
<p>真正不能唯一确定的是<b>二叉树的先序+后序</b>:只知道第一个结点是根、后面是一堆结点,分不清谁属于左子树、谁属于右子树。只有当每个结点的度都 ≤ 1(链状,二叉树上表现为没有同时含左右孩子的结点)时才唯一。</p>
|
|||
|
|
<p class="acc__hint">考试里出现“先根+后根不能唯一确定树”的说法时,多半是从“二叉树先序+后序”串过来的。记住这张对应表,你就有底气判它。</p>
|
|||
|
|
</div>
|
|||
|
|
</details>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
|
|||
|
|
<!-- ═══════════ 09 知识问答 ═══════════ -->
|
|||
|
|
<section class="sec sec--quiz" id="quiz" aria-labelledby="quizTitle">
|
|||
|
|
<div class="sec__head">
|
|||
|
|
<p class="eyebrow"><span class="sec__no">09</span> 知识问答 · 做完三题才算完成</p>
|
|||
|
|
<h2 class="sec__title" id="quizTitle">三题,一次判卷</h2>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<form class="quiz" id="quizForm" novalidate>
|
|||
|
|
<fieldset class="q" data-q="1" data-answer="B">
|
|||
|
|
<legend class="q__legend"><span class="q__n">1</span><span class="q__lv">基础</span>将一棵树按“左孩子右兄弟”转换为二叉树后,该二叉树的根结点( )。</legend>
|
|||
|
|
<div class="q__opts">
|
|||
|
|
<label class="opt"><input type="radio" name="q1" value="A"><span class="opt__k">A</span><span class="opt__t">一定没有左孩子</span></label>
|
|||
|
|
<label class="opt"><input type="radio" name="q1" value="B"><span class="opt__k">B</span><span class="opt__t">一定没有右孩子</span></label>
|
|||
|
|
<label class="opt"><input type="radio" name="q1" value="C"><span class="opt__k">C</span><span class="opt__t">一定没有孩子</span></label>
|
|||
|
|
<label class="opt"><input type="radio" name="q1" value="D"><span class="opt__k">D</span><span class="opt__t">左右孩子都可能有</span></label>
|
|||
|
|
</div>
|
|||
|
|
<p class="q__expl"></p>
|
|||
|
|
</fieldset>
|
|||
|
|
|
|||
|
|
<fieldset class="q" data-q="2" data-answer="B">
|
|||
|
|
<legend class="q__legend"><span class="q__n">2</span><span class="q__lv">进阶</span>已知树 T 先根遍历序列为 ABDECF,后根遍历序列为 DEBFCA,则 T 对应二叉树的中序遍历序列为( )。</legend>
|
|||
|
|
<div class="q__opts">
|
|||
|
|
<label class="opt"><input type="radio" name="q2" value="A"><span class="opt__k">A</span><span class="opt__t">ABDECF</span></label>
|
|||
|
|
<label class="opt"><input type="radio" name="q2" value="B"><span class="opt__k">B</span><span class="opt__t">DEBFCA</span></label>
|
|||
|
|
<label class="opt"><input type="radio" name="q2" value="C"><span class="opt__k">C</span><span class="opt__t">DBEFCA</span></label>
|
|||
|
|
<label class="opt"><input type="radio" name="q2" value="D"><span class="opt__k">D</span><span class="opt__t">DEBCFA</span></label>
|
|||
|
|
</div>
|
|||
|
|
<p class="q__expl"></p>
|
|||
|
|
</fieldset>
|
|||
|
|
|
|||
|
|
<fieldset class="q" data-q="3" data-answer="B">
|
|||
|
|
<legend class="q__legend"><span class="q__n">3</span><span class="q__lv">综合</span>一棵树 T 共 10 个结点,其中度为 3 的结点 2 个、度为 2 的结点 1 个、度为 1 的结点 1 个,其余为叶子。将 T 转换为二叉树后,该二叉树中没有右孩子的结点个数是( )。</legend>
|
|||
|
|
<div class="q__opts">
|
|||
|
|
<label class="opt"><input type="radio" name="q3" value="A"><span class="opt__k">A</span><span class="opt__t">4</span></label>
|
|||
|
|
<label class="opt"><input type="radio" name="q3" value="B"><span class="opt__k">B</span><span class="opt__t">5</span></label>
|
|||
|
|
<label class="opt"><input type="radio" name="q3" value="C"><span class="opt__k">C</span><span class="opt__t">6</span></label>
|
|||
|
|
<label class="opt"><input type="radio" name="q3" value="D"><span class="opt__k">D</span><span class="opt__t">7</span></label>
|
|||
|
|
</div>
|
|||
|
|
<p class="q__expl"></p>
|
|||
|
|
</fieldset>
|
|||
|
|
|
|||
|
|
<div class="quiz__bar">
|
|||
|
|
<button type="submit" class="btn btn--primary" id="gradeBtn"><span class="btn__label">判卷</span></button>
|
|||
|
|
<button type="button" class="btn btn--quiet" id="resetBtn">重做</button>
|
|||
|
|
<p class="quiz__note" id="quizNote">也可以直接回复答案(如 <code>1B 2A 3C</code>)判卷。</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div class="quiz__result" id="quizResult" role="status" aria-live="polite" hidden></div>
|
|||
|
|
</form>
|
|||
|
|
<noscript>
|
|||
|
|
<p class="footnote">※ 当前浏览器未运行 JavaScript:图与判卷不可用,三题的解析已直接列在题下,答案为 1B 2B 3B。</p>
|
|||
|
|
</noscript>
|
|||
|
|
</section>
|
|||
|
|
</main>
|
|||
|
|
|
|||
|
|
<footer class="site-foot">
|
|||
|
|
<div class="site-foot__in">
|
|||
|
|
<div class="site-foot__col">
|
|||
|
|
<p class="site-foot__mark">左孩子、右兄弟</p>
|
|||
|
|
<p class="site-foot__small">408 知识卡片 · DS-12 树、森林与二叉树的转换及遍历。内容以最新 408 大纲与指定教材为准。</p>
|
|||
|
|
</div>
|
|||
|
|
<div class="site-foot__col">
|
|||
|
|
<ul class="site-foot__links">
|
|||
|
|
<li><a href="#top">回到顶部</a></li>
|
|||
|
|
<li><a href="#think">引导提问</a></li>
|
|||
|
|
<li><a href="#quiz">知识问答</a></li>
|
|||
|
|
</ul>
|
|||
|
|
<p class="site-foot__small site-foot__colophon">图 0–4 均为本页实时绘制 · 无图片素材</p>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
</footer>
|
|||
|
|
|
|||
|
|
<script src="app.js"></script>
|
|||
|
|
</body>
|
|||
|
|
</html>
|