树的遍历
广度遍历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)
lineal tree
tree用链表实现(不唯一):
树的变例:先序(先访问根,先左后右),中序(先访问左边再访问根,再访问右),后序(左-》右-》根)
完全二叉树
定义:除了最后一层,其他每层结点都是最大值,最后一层的所有节点都集中在左方。
满二叉树
定义:每层结点数都是最大值,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树
1.节点排序
2.一个节点了可以存多个元素,多个元素也排序了
B+树
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 :
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.