编程语言/二叉树:修订间差异
来自康健生活
| (未显示同一用户的27个中间版本) | |||
| 第12行: | 第12行: | ||
* 树的节点没有左右之分,而二叉树的节点有左右之分 | * 树的节点没有左右之分,而二叉树的节点有左右之分 | ||
== 二叉树分类 == | |||
=== 满二叉树 === | |||
一棵深度为 k 且有 <math>2^k -1</math> 个节点的二叉树称为满二叉树。 | 一棵深度为 k 且有 <math>2^k -1</math> 个节点的二叉树称为满二叉树。 | ||
| 第19行: | 第19行: | ||
digraph binaryTree{ | digraph binaryTree{ | ||
node[shape=circle,color=red,fontcolor=blue,fontsize=10]; | node[shape=circle,color=red,fontcolor=blue,fontsize=10]; | ||
root[color=blue,fontcolor=black,fontsize= | root[color=blue,fontcolor=black,fontsize=10]; | ||
root->a | root->a; | ||
root->b; | root->b; | ||
a->c; | a->c; | ||
| 第29行: | 第29行: | ||
</graphviz> | </graphviz> | ||
=== 完全二叉树 === | |||
完全二叉树是指最后一层左边是满的,右边可能满也可能不满,然后其余层都是满的二叉树称为完全二叉树, 满二叉树 <math>\in</math> 完全二叉树。 | 完全二叉树是指最后一层左边是满的,右边可能满也可能不满,然后其余层都是满的二叉树称为完全二叉树, 满二叉树 <math>\in</math> 完全二叉树。 | ||
| 第35行: | 第35行: | ||
digraph binaryTree{ | digraph binaryTree{ | ||
node[shape=circle,color=red,fontcolor=blue,fontsize=10]; | node[shape=circle,color=red,fontcolor=blue,fontsize=10]; | ||
root[color=blue,fontcolor=black,fontsize= | root[color=blue,fontcolor=black,fontsize=10]; | ||
root->a | root->a; | ||
root->b; | root->b; | ||
a->c; | a->c; | ||
a->d; | a->d; | ||
b->e[style=dotted]; | |||
b->f[style=dotted]; | |||
} | } | ||
</graphviz> | </graphviz> | ||
=== 平衡树 === | |||
=== 二叉搜索树 === | |||
* 若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值; | |||
* 若任意节点的右子树不空,则右子树上所有节点的值均大于它的根节点的值; | |||
* 任意节点的左、右子树也需要满足左边小右边大的性质 | |||
<graphviz renderer='neato'> | |||
digraph binaryTree{ | |||
node[shape=circle,color=red,fontcolor=blue,fontsize=10]; | |||
12[color=blue,fontcolor=black,fontsize=10]; | |||
12->4; | |||
12->18; | |||
4->1; | |||
4->8; | |||
18->16; | |||
18->20; | |||
} | |||
</graphviz> | |||
{{Unknown|accord}} | |||
以下不满足搜索二叉树示例: | |||
<graphviz renderer='neato'> | |||
digraph binaryTree{ | |||
node[shape=circle,color=red,fontcolor=blue,fontsize=10]; | |||
12[color=blue,fontcolor=black,fontsize=10]; | |||
12->4; | |||
12->18; | |||
4->1; | |||
4->8; | |||
21[style=filled]; | |||
18->21[style=dotted]; | |||
18->20; | |||
} | |||
</graphviz> | |||
===== 链式存储结构(三叉链表) ===== | |||
<graphviz renderer='neato'> | |||
digraph binaryTree{ | |||
node[shape=circle,color=red,fontcolor=blue,fontsize=10]; | |||
data[color=blue,fontcolor=black,fontsize=10,label="node"]; | |||
node[shape=record]; | |||
struct1 [label="<f0> parent|<f1> leftChild|<f2> rightChild|<f3> node"]; | |||
node[shape=none,color=red,fontcolor=blue,fontsize=10]; | |||
parent[color=blue,fontcolor=black,fontsize=10]; | |||
node[shape=none,color=red,fontcolor=blue,fontsize=10]; | |||
leftChild[color=blue,fontcolor=black,fontsize=10]; | |||
node[shape=none,color=red,fontcolor=blue,fontsize=10]; | |||
rightChild[color=blue,fontcolor=black,fontsize=10]; | |||
data->parent; | |||
data->leftChild; | |||
data->rightChild; | |||
} | |||
</graphviz> | |||
==== 查找 ==== | |||
==== 遍历 ==== | |||
===== 中序遍历(inorder) ===== | |||
===== 前序遍历(preorder) ===== | |||
===== 后序遍历(postorder) ===== | |||
== 树的存储 == | |||
=== 数组 === | |||
=== 链表 === | |||
== 解题 == | |||
=== BFS === | |||
=== DFS === | |||
=== 其他 === | |||
参考[https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof/ 力扣] 第 105 题,根据中序划分二叉树子树,然后递归求解 | |||
<graphviz renderer='neato'> | |||
digraph binaryTree{ | |||
node[shape=circle,color=red,fontcolor=blue,fontsize=10]; | |||
root[color=blue,fontcolor=black,fontsize=10]; | |||
root->a[color=yellow]; | |||
root->b[color=green]; | |||
a->c[color=yellow]; | |||
a->d[color=yellow]; | |||
b->e[color=green]; | |||
b->f[color=green]; | |||
} | |||
</graphviz> | |||
<syntaxhighlight lang="javascript"> | |||
/** | |||
* Definition for a binary tree node. | |||
* function TreeNode(val) { | |||
* this.val = val; | |||
* this.left = this.right = null; | |||
* } | |||
*/ | |||
/** | |||
* @param {number[]} preorder | |||
* @param {number[]} inorder | |||
* @return {TreeNode} | |||
*/ | |||
var buildTree = function(preorder, inorder) { | |||
if (!preorder.length || !inorder.length) { | |||
return null; | |||
} | |||
const rootValue = preorder[0]; | |||
const node = new TreeNode(rootValue); | |||
let i = 0; | |||
for (; i < inorder.length; ++i) { | |||
if (inorder[i] === rootValue) { | |||
break; | |||
} | |||
} | |||
node.left = buildTree(preorder.slice(1, i + 1), inorder.slice(0, i)); | |||
node.right = buildTree(preorder.slice(i + 1), inorder.slice(i + 1)); | |||
return node; | |||
};</syntaxhighlight> | |||
{{Unknown|心谭}} | |||