We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
待更新
The text was updated successfully, but these errors were encountered:
开始之前,请看一下几个问题,然后带着问题进入本章内容:
树是一种 非线性结构。它遵循:
关于树的几个概念:
其中, A与B之间的连接称为 边,B的深度为1;B的 高度 为B到叶节点G或H或F的最长路径,所以为2。
高度
树的高度计算公式:
下图每个节点值都代表当前节点的高度:
最多能分两个叉的树。
二叉树中,每一个节点的左右子树的高度相差不能大于 1,称为平衡二叉树。
1.链式存储法
每个节点包含三部分
val
left
right
所以节点可以表示为:
function Node(val) { // 保存当前节点key值 this.val = val // 指向左子节点 this.left = null // 指向右子节点 this.right = null }
可以由根节点通过左右指针连接起来形成一棵树
function BinaryTree() { let Node = function(val) { this.val = null this.left = null this.right = null } let root = null }
数组存储法(适用于完全二叉树)
所有的节点满足三种关系:
i
i/2
2i
2i+1
因此,可以把完全二叉树存储在数组里(从下标为 1 开始存储),可以通过下标找到任意节点的父子节点。从而完整的构建出这个完全二叉树。这就是数组存储法。
数组存储法相对于链式存储法不需要为每个节点创建它的左右指针,更为节省内存。
参考来源:小白都可以看懂的树与二叉树
Sorry, something went wrong.
No branches or pull requests
待更新
The text was updated successfully, but these errors were encountered: