【数据结构】树tree

本文详细介绍了树的遍历策略,包括广度优先遍历(BFS)和深度优先遍历(DFS),以及二叉树的层次遍历、完全二叉树、满二叉树和搜索二叉树的特点。还探讨了B树和B+树的数据结构,以及相关术语如节点度、深度、高度和树的祖先/后代概念。
摘要由CSDN通过智能技术生成

树的遍历

广度遍历Breadth-first traversal

Breadth-first traversal is the traversal strategy used in the binary tree.Breadth first traversal, also known as level order traversal is the traversal strategy used in a binary tree. It involves visiting all the nodes at a given level.

深度遍历Depth-first traversal

level order traversal

O(n) is the time complexity of level order traversal.

树的类别

二叉树pedigree tree(binary tree)

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARGF2aWRfSHp5,size_12,color_FFFFFF,t_70,g_se,x_16

lineal tree

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARGF2aWRfSHp5,size_12,color_FFFFFF,t_70,g_se,x_16

tree用链表实现(不唯一):

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARGF2aWRfSHp5,size_20,color_FFFFFF,t_70,g_se,x_16

树的变例:先序(先访问根,先左后右),中序(先访问左边再访问根,再访问右),后序(左-》右-》根)

完全二叉树

定义:除了最后一层,其他每层结点都是最大值,最后一层的所有节点都集中在左方。

满二叉树

定义:每层结点数都是最大值,k层有2^k-1个结点。

搜索二叉树(search binary tree)

特点:In the tree, the values of all the items in its left subtree are smaller than the item in X, and the values of all the items in its right subtree are larger than the item in X.

在树中,左子树中所有项的值都小于X中的项,而右子树中所有项的值都大于X中的项。

特例:adl二叉树

B树

一颗m阶的B树满足如下条件:

1. 每个节点最多只有m个子节点。

2. 除根节点外,每个非叶子节点具有至少有 m/2(向下取整)个子节点。

3. 非叶子节点的根节点至少有两个子节点。

4. 有k颗子树的非叶节点有k-1个键,键按照递增顺序排列。

5. 叶节点都在同一层中。

特点:

1.节点排序

2.一个节点了可以存多个元素,多个元素也排序了

B+树

一颗m阶的B+树满足如下条件:

1. 每个节点最多只有m个子节点。

2. 除根节点外,每个非叶子节点具有至少有 m/2(向下取整)个子节点。

3. 非叶子节点的根节点至少有两个子节点。

4. 有k颗子树的非叶节点有k个键,键按照递增顺序排列。

5. 叶节点都在同一层中。

特点

B树升级版,拥有B树的特点

叶子节点之间有指针

非叶子节点上的元素在叶子节点上都冗余了,也就是叶子节点中存储了所有的元素,并且排好顺序

专业名词

The average depth of a binary tree is given as O(√N). In case of a binary search tree, it is O(log N).

节点(node)的集合,要么空集,要么包含root(node r)和多个非空子树。

有n个节点的树有n-1个结点的边

degree of a node :number of subtrees of the node.  For example, degree(A) = 3, degree(F) = 0.

degree of a tree : watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBARGF2aWRfSHp5,size_13,color_FFFFFF,t_70,g_se,x_16

parent : a node that has subtrees.

children : the roots of the subtrees of a parent.

 siblings : children of the same parent.

leaf ( terminal node ) : a node with degree 0 (no children).

path from n1 to nk :a (unique) sequence of nodes n1, n2, …, nk   

length of path : number of edges on the path.

depth of ni : length of the unique path from the root to ni.   Depth(root) = 0.

height of ni : length of the longest path from ni to a leaf.  Height(leaf) = 0, and height(D) = 2.

height (depth) of a tree :height(root) = depth(deepest leaf).

ancestors of a node :all the nodes along the path from the node up to the root.

descendants of a node :all the nodes in its subtrees.

  • 24
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岩塘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值