Tree Data Structure 5 / FIG.

Traversal summary

(1) Tree:
(order / disorder) tree, corresponding to the first order traversal of a binary tree forest preorder traversal
(order / disorder) tree, the order corresponding to the binary tree traversal forest preorder

In order not forest (or forest in order to subsequent equivalent),
the forest (in) / equivalent of the first sequence / order of the tree before
the tree is not preorder

Preorder tree and subsequent ⇒ not construct the tree
to a tree structure, it must know preorder

1, the preamble sequence +
2, in sequence after the sequence +
3, + sequence in order
to know more than 3 in case any one can construct the tree


(2) Figure:
breadth chart (and tree) search ⇔ binary tree traversal traverse the level
diagram of the depth-first traversal of a binary tree preorder traversal ⇔


basic concept

Number of nodes in the tree can be 0 (can be null tree)

(1-1) node:
contains not only data elements, and comprising a branch point subtree (pointers to sub-tree)

Leaf (terminal / external) Node: A
degree of node 0

Non-terminal (branch) nodes:
of not referred to a non-terminal node (internal node) of the node 0
including the root node

An intermediate node (non-leaf node non-root)

(1-2) node / tree of:

The number of subtrees branching or the number of nodes has degree is referred to as node;

Each node of the tree is a maximum.

(1-3) level, the height (depth), Forest:
① levels: from the root, root for the first layer, the second layer is the root of the child, and so on.

The maximum level tree nodes: the height (depth) ② tree

③ forest: a collection of several trees disjoint tree


Memory tree

Order - parent storage structure (one-dimensional array)

Represents an array subscript node tree, the contents of the array element represents a node of the parent node


(2) chain
① children storage

② children brothers storage


Binary Tree

① each node at most two subtrees

② there are left and right sub-tree of order

③N₀=N₂+1

④n nodes, can be constructed of different binary C²ⁿₙ / (n + 1) Species



1. Full Binary Tree

All branch node has a left child and right child nodes, and all the leaf nodes are concentrated in the most binary layer


2. The complete binary tree

① except the last one may be dissatisfied with the other layers of the layer has reached the maximum node

② If less than the last layer, the layer node order from left to right


Test sites

1: Depth complete binary tree of n nodes
(log₂n rounded lower) +1 or [log₂ the (n + 1) rounding]

Proof Tianqin 136

2: n is a complete binary tree leaf node depth
① nodes of degree 1 exist in the (rounded lower log₂2n) +1
② absence nodes of degree 1 (log₂2n-1 under rounding) +1


3: determining the depth of a binary search tree of n nodes is equal to the depth ⇒ complete binary tree


3. The node number (1 ~ n)
parent - child

node a number i⇒
① parents number (i / 2 rounded lower)
② left child No. 2i (if any)


From bottom to a first internal node: n / 2
proved ① / absence of a single limb ② subtracting leaves

application

1: complete binary tree may be used to sort the stack configured


3. binary storage structure

(1) sequential storage

(2) a chain storage

① binary list
first child node in the linked list node points to the two chain domain and the second node are child nodes

n-n + 1 has an empty node pointer
Method a:
Total pointer field - node pointer field; and the node pointer fields have the n-1
⇒2n- (n-1) = n + 1
as n- father node has a node (corresponding with the n-1 node pointer field pointing to these)

Method two:
the number of null pointer: 2N₀ + N₁ = N₀ + N₁ + N₂ + 1 = n + 1

② trigeminal list
trigeminal list with respect to binary list, which is used to point to the third parent pointer.

When n nodes, there are n + 2 null pointer.
Because only the root no parent, so compared to the binary addition of a chain-null pointer, then the total null pointer is n + 2.

Arbitrary non-recursive binary tree traversing algorithm without using the stack structure, the list select the trigeminal (parent node may point)


4. binary tree traversal

Binary tree traversal results may not be unique

(1) preorder

(2) preorder

(3) after preorder

(4) traverse the level
of the binary tree traversal level by means of a circular queue

5. threaded binary

The purpose of introducing a binary tree of clues:
speed up the Find node predecessor or successor speeds
without using recursion can be traversed

The only result can not traverse

6. Special binary tree

(1) a binary tree preorder and postorder opposite
⇔ height equal to a number of nodes in each node ⇔

(2) only the binary tree height h is 0 degree and the node 2, which is at least 2h-1 nodes
except the first node h layers, each of 2, only one (2 subnodes ), a total of 2 (h-1) th, plus a total of 2h-1 a root node.
It can also be substituted for special circumstances (root only)


7. ordered tree

Sequential relationship between the child node of any node in the tree, the tree is called ordered tree

It is understood to traverse the level number
as in the first layer A, a second layer BCD, a third layer EF

Binary tree traversal orderly relations seeking its transformation: test center
method: ① to draw up an ordered tree (hierarchy traversal No. ABCDEF)
② reconverted to binary tree

Method Two: The rule tree traversal corresponds to the selected binary tree


⇔ ⇔ binary tree forest

The default method list using child Brothers (easy conversion)

⇔ binary tree
(1) binary tree →

principle: ① adjacent sibling connection
② delete parents with a child's first non-connection
③ brother has become the right subtree, child becomes left subtree

Because the root is not necessarily the right brother, so binary tree root after the conversion is not necessarily the right child

(2) binary tree →
principle: ① right child become brothers and connection
② parents and the (non-first child) connection
③ delete nodes and connections brothers

⇔ binary tree forest
(1) → binary tree forest

① will all have the right child of the root node of a binary tree right child link down
② then converted into binary trees and more trees

(2) Forest binary →
① converting binary tree forest
② the second binary tree as the right subtree of the root of the binary tree, the tree as a third right subtree of the second tree ...

If a non-null binary converted forest, the shape of the binary tree is: a root node may have left subtree and right subtree (Discussion binary tree forest → → binary whether confusion)


The Huffman tree (optimal binary) and Huffman coding

Huffman binary tree default, can also be more fork

Also known as optimal binary Huffman binary tree:
All the n binary tree leaf node with the right configuration, the minimum weighted path length of a binary tree.

External path length (value = WPL): external path length and the path length from the root is called the binary tree to each external node (leaf node) (E) of
the Huffman tree (the leaf node access) outside the shortest path length binary

Weighted path length tree WPL: Weighted path length tree path length weighted means of all tree nodes and leaf

(1) path length weighted length path:

Path Length:
the number of branches on the path (a branch path consisting of the node to another node)

Weighted Path Length nodes:
nodes having weights multiplied node weights from point to point path length between the roots, is weighted path length of the node


(2) test sites

1: the weight of all the Huffman tree on the leaves, branches only those weights used for construction algorithm, in fact, no weights

2: Huffman tree is not complete binary tree, strictly speaking, is not entirely m-tree

3: number of nodes is even not Huffman tree



Published 46 original articles · won praise 15 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_41850194/article/details/100732122