Tree of excellent courseware notes

Source: Internet
Author: User

6.1 tree definition and basic terms
6.2 Binary Tree
6.3 traverse binary tree and clue Binary Tree
6.4 trees and forests
6.5 Heman tree and Its Application
Chapter 6 tree and binary tree
2
Page 2
6.1 tree definition and basic terms
Tree Definition
The tree is composed of n (n
0) a finite set of nodes. If n = 0, it is null.
Tree;
If n> 0

There is a specific node called root. It only has direct successor, but does not
There is a direct precursor;

Other nodes except the root are divided into M (M
0) a finite set of non-Intersecting objects
T0, T1 ,...
, Tm-1, each collection is a tree, and called the root of the subtree. Each child
The root node of the tree has only one direct precursor, but can have zero or multiple direct successors.
A
B c
E f g h I j
K l m
D
3
Page 3
A
B c
E f g h I j
K l m
D
Root
Subtree1
Subtree2
Subtree3
6.1 tree definition and basic terms
4
Page 4
Node: Data Element and Its Branches
Node degree (degree): number of Subtrees owned by the node
Degree: the maximum degree of the node in the tree.
Branch node: node with a degree of less than 0
Leaf (leaf) node: A node with a degree of 0
Child node: the root of the knot tree
Parent node: the direct precursor node of the child node
Sibling node: A child node of the same parent node is called a sibling node.
Level: the root layer is the first layer. The sub-node level is better than the parent node level.
Point level plus 1
Depth: The maximum hierarchy of nodes in the tree.
Ordered Tree: left-to-right sub-tree
Unordered tree: sub-tree unordered
Forest (forese): m (
0) set of trees that do not match each other
6.1 tree definition and basic terms
5
Page 5
6.2 Binary Tree
B
C
D
E
F
L
Binary Tree example
6.2.1
Binary Tree Definition
A binary tree is a finite set of nodes. The set is either empty or
A root node is added with two binary trees called left and right trees.
.
The left subtree and
Right subtree, even if there is only one subtree
Indicates whether the tree is left or right.
Subtree. This is the most important binary tree and tree
Difference.
6
Page 6
D
C
H
E
F
B
A
1
Layer: number of nodes 2
1-1
= 2
0
Items
2
Layer: number of nodes 2
2-1
= 2
1
Items
3
Layer: number of nodes 2
3-1
= 2
2
Items
6.2.2
Binary Tree nature
Nature 1
:
I
At most 2 layers
I-1
Nodes
Certificate: 1)
K = 1
Is true;
2)
Set
K = I-1
Is true;
3)
Then, when
K = I
In the Binary Tree
I
At most 2 layers
I-1-1
*
2 =
2
I-1
Nodes;

The original proposition is true.
6.2 Binary Tree
7
Page 7
Nature 2
: Depth is
K
At most 2 binary trees are supported.
K
-1
Nodes
Evidence: nature of use 1
, Set 1st
Layer
K
The maximum number of nodes in the layer are added,
Then: 1 + 2 + 2
2
+
............
+ 2
K-2
+ 2
K-1
= 2
K
-1

The original proposition is true.
Nature 3
: Number of leaf nodes of a binary tree
N0
Equal to 2
Point of completion N2 + 1
Certificate: set to 1
The number of nodes is
N1
, The total number of branches is
B
B = N0 + N1 + n2-1
Again
B = N1 + 2
*
N2

N1 + 2
*
N2 = N0 + N1 + n2-1

N0 = n2 + 1

The original proposition is true.
6.2 Binary Tree
8
Page 8
Full Binary Tree: a K-depth binary tree with 2k-1 nodes, as shown in (.
Full Binary Tree: If the depth is K, each node in the binary tree with N nodes can
If the depth is K, the full Binary Tree corresponds to the node of the number from 1 to n.
The sample Binary Tree is a Complete Binary Tree, as shown in (B. Figure (c) and (d) are two unfinished trees.
Binary Tree. Full binary trees are a special case of full Binary Trees.
1
2 3
4 5 6
()
7
1
2 3
4 5 6
(B)
1
2 3
4 5
(C)
6
1
2 3
4
(D)
5
6.2 Binary Tree
Full Binary Tree
With leaf nodes
Appears at Layer K
Or K-1 layer.
9
Page 9
Property 4: the depth of a Complete Binary Tree with N nodes is
Log2n
+ 1.
Symbol
X
It indicates the maximum integer not greater than X.
Proof: If the depth of the binary tree is K, the binary tree is defined based on the Nature 2 and the full binary tree.
Expected result: 2k-1-1 <n <= 2k-1 or 2k-1 <= n <2 K
Obtain the logarithm: K-1 <log2n <K

K is an integer,
K =
Log2n
+ 1.
Property 5: If the node of A Complete Binary Tree with N knots is numbered by sequence
(From top to bottom, from left to right), for any node I (1 <= I <= N), there are:
(1) If I = 1, node I has no parent and is the root of a binary tree. If I> 1,
The parent node is the node.
I/2
.
(2) If 2I> N, node I has no left child; otherwise, the left child is node 2I.
(3) If 2I + 1> N, node I has no right child; otherwise, the right child is a node.
2I + 1.
6.2 Binary Tree
10
Page 10
You can first prove (2) and (3), and then launch (1) from (2) and (3 ).
Proof: 1 for I = 1, defined by the full Binary Tree, its left child is node 2, if 2> N, that is
Node 2 does not exist. node I has no children. Node I's right child can only be Node 3.
Point 3 does not exist, that is, 3> N. At this time, node I has no right child.
2 For I> 1, there are two situations:
(1) set J (1 <= j <=
Log2n
The first node of the layer is I, which is composed
Nature 2 and definition I = 2j-1, the left and right nodes of node I must be the first and second layers of J + 1
Node, which is numbered 2j = 2*2j-1 = 2I and 2I + 1. If 2I> N, no left child exists. If
2I + 1> N, no right child.
(2) Assume that a node number on layer J is I (2j-1 <= I <= 2j-1), and 2I + 1 <n,
The left child is 2I, And the right child is 2I + 1. The Node numbered I + 1 is the node numbered I.
Right brother or cousin. If it has a left child, its number must be 2I + 2 = 2 * (I + 1 );
If it has a right child, its number must be 2I + 3 = 2 * (I + 1) + 1.
6.2 Binary Tree
11
Page 11
3 from (2) and (3) (1)
When I = 1, the node is the root, so there is no parent. When I> 1, set its parent node to compile
If I is the left child, that is, I = 2 P, then p = I/2 =
I/2
If I is the right child
Child, I = 2 p + 1, P = (I-1)/2 =
I/2
.
Pass.
6.2 Binary Tree

I/2
I I + 1
2I 2I + 1 2I + 2 2I + 3
I + 1
2I + 2 2I + 3
I
2I 2I + 1
(A) I and I + 1 nodes on the same layer (B) I and I + 1 nodes are not on the same Layer
......
......
12
Page 12
6.2.3 Binary Tree Storage Structure
1. Sequential Storage Structure
Using a set of continuous Storage
Unit Storage binary tree data
Element. Binary Tree
All nodes are numbered by sequence,
The node serial number reflects the node
.
A B C D E F G H I J K L
1 2 3 4 5 6 7 8 9 10 11 12
6.2 Binary Tree
D
C
G
E
F
B
A
H
K
I
J
L
8 9 10 11 12
4 5 6 7
2 3
1
13
Page 13
A B C D E F?
1 2 3 4 5 6 7 8 9 10 11 12
The disadvantage of sequential storage is that it may cause a great waste of storage space.
In this case, 2h-1 nodes are required for the right decision tree with a depth of H and only H nodes.
Storage space.
6.2 Binary Tree
D
C
E ø
F
B
A
Ø g ø
H
8 9 10 11 12
4 5 6 7
2 3
1
D
C
E
F
B
A
G
H
14
Page 14
Typedef struct bitnode
{Telemtype data;
Struct bitnode * lchild;
Struct bitnode * rchild;
} Bittnode, * bitree;
Binary linked list
2 chain storage structure:
6.2 Binary Tree
Data lchild rchild
Typedef struct bitnode
{Telemtype data;
Struct bitnode * lchild;
Struct bitnode * rchild;
Struct bitnode * parent;
} Bittnode, * bitree;
Triple linked list
Data lchild rchild parent
15
Page 15
6.2 Binary Tree
16
Page 16
6.2 Binary Tree
A
B
D
G
E
F
H
J
I
C
# Define max_size 10
Typedef struct bitnode
{Telemtype data;
Int lchild;
Int rchild;
} Bittnode;
Typedef bitnode sqbitree [max_size];
Data
Lchild
Rchild
1 2 3 4 5 6 7 8 9 10
A B c d e f g h I j
2 3 0 6 0 0 8 9 0 0
0 4 5 7 0 0 0 10 0
3 static chained storage structure:
The static chain structure is applicable to the known number of nodes on a binary tree, or does not support dynamic storage.
The advanced language for storage allocation.
17
Page 17
6.3 traverse binary tree and clue Binary Tree
6.3.1
Traverse Binary Trees
1
Definition: access each node in a binary tree in a certain order so that each node is
Access once and only once.

Linear table Traversal
(1
) Sequential table traversal:
For (I = 1; I <= V. Last; I ++)
Visit (v.
ELEM
[I]);
(2
) List traversal:
For (P = L-> next; P! = NULL; P = p-> next) visit (p-> data );

Binary tree traversal: Non-linear relationship. The order must be determined.

Generally, the processing order of the root node is divided:
First Order (DLR
), Medium Order (LDR
), Post-order (LRD
).
18
Page 18
-Traverse Binary Trees in ascending order (
DLR)
Operation definition:
(1)
Access the root node;
(2)
Traverse the left subtree in sequence
(3)
Traverse the right subtree in sequence
-Traverse a binary tree in the middle order (
LDR)
Operation definition:
(1)
Traverse the left subtree in the middle order;
(2)
Access the root node;
(3)
Traverse the right subtree;
-Post-order traversal of Binary Trees (
LRD)
Operation definition:
(1)
Traverse the left subtree in descending order;
(2)
Then traverse the right subtree;
(3)
Access the root node;
D
C
F
E
B
A
Preface: A B D C E F
Central order: d B A E C f
Descending order: d B E F C
6.3 traverse binary tree and clue Binary Tree
19
Page 19
2
Traversal Algorithm (in the middle order)
Recursive Algorithm:
Void inorder (bitree T)
{If (T! = NULL)
{If (t-> lchild! = NULL)
Inorder (t-> lchild );
Printf (t-> data );
If (t-> rchild! = NULL)
Inorder (t-> rchild );
}
}
Void inorder (bitree T)
{Initstack (s );
P = T;
While (p |! Satckempty (s ))
{If (p) {push (P );
P = p-> lchild;
}
Else {Pop (P );
Printf (p-> data );
P = p-> rchild );
}
}
}
Non-recursive algorithms:
6.3 traverse binary tree and clue Binary Tree
20
Page 20
3
Binary Tree Algorithm example:
Int treeequal (bitree T1, bitree T2)
// If the two trees are equal, 1 is returned; otherwise, 0 is returned (first order)
{If (! T1 &&! T2) Return (1 );
Else if (T1 & T2)
{If (T1-> DATA = t2-> data)
If (treeequal (T1-> lchild, T2-> lchild ))
Return (treeequal (T1-> rchild, T2-> rchild ));
}
Return (0 );
}
6.3 traverse binary tree and clue Binary Tree
21
Page 21
Bitree treecopy (bitree t) // Binary Tree replication (First Order)
{If (t)
{P = (bitree) malloc (sizeof (bitnode ));
P-> DATA = T-> data;
P-> lchild = treecopy (t-> lchild );
P-> rchild = treecopy (t-> rchild );
Return (P );
}
Else return (null );
}
6.3 traverse binary tree and clue Binary Tree
22
Page 22
6.3 traverse binary tree and clue Binary Tree
6.3.2
Clue Binary Tree
1
What is a clue binary tree?

Essentially, traversal performs linear processing on non-linear Binary Trees. Traversal order
The frontend and successor positions of a node in the column can only be obtained during the dynamic traversal process.

The binary linked list of N nodes contains N + 1 empty Chain Domain, which can store its front and back
Child pointer.

Node Structure: lc h ild lt a G d a t a r t a G R C H ILD
Ltag = 0. lchild points to the left subnode.
Ltag = 1, lchild points to the front Node
Rtag = 0, rchild points to the right child node
Rtag = 1, rchild points to the next node
Typedef struct bithrnode
{Telemtype data;
Struct bithrnode * lchild;
Struct bithrnode * rchild;
Unsigned ltag: 1;
Unsigned rtag: 1;
} Bitthrnode, * bithrtree;
23
Page 23
D
C
F
E
B
A
No frontend node no successor Node
0
0
0
0
0
0
1
1
1
1
1
1
1
1
A
B
D
E
C
F
Add a header Node
6.3 traverse binary tree and clue Binary Tree
Note: The precursor in this section
In the traversal Sequence
In a linear logical relationship
Node precursor and successor,
It is not its parent-child node.
24
Page 24
2
Search for the frontend and successor
Bithrnode * priornode (bithrnode * P)
{Bithrnode * pre = p-> lchild;
If (! P-> ltag) // find the rightmost node of the Left subtree
While (! Pre-> rtag) Pre = pre-> rchild;
Return (pre );
}
Bithrnode * nextnode (bithrnode * P)
{Bithrnode * Next = p-> rchild;
If (! P-> rtag) // find the leftmost node of the right subtree
While (! Next-> ltag) Next = Next-> lchild;
Return (next );
}
6.3 traverse binary tree and clue Binary Tree
25
Page 25
3
Traversal of a clue Binary Tree
For convenience, add a header node for the clue binary tree to make it similar
A list of two-way cyclic clues.
Algorithm 1: P134 algorithms 6.5
Algorithm 2: Using nextnode Functions
Void inorder_thr (bithrtree T)
{P = T-> lchild;
While (P! = T &&! P-> ltag) // find the leftmost Node
P = p-> lchild;
While (P! = T)
{Printf (p-> data); P = nextnode (p );}
}
6.3 traverse binary tree and clue Binary Tree
26
Page 26
4
Clues of Binary Trees
Void inorderthreading (bithrtree & thrt, bithrtree T)
// The ltag and rtag fields of each node in T are 0.
{Bithrnode * Pre, * P;
// Pre points to the last processed node, and P points to the current processed Node
Bithrnode * Pre, * P, * s [Max]; // the stack of the sequential structure
Int I = 0; // I is the top position of the stack.
Thrt = (bithrnode *) malloc (sizeof (bithrnode ));
Thrt-> ltag = 0;
Thrt-> rtag = 1;
Thrt-> rchild = thrt;
If (! T) thrt-> lchild = thrt;
6.3 traverse binary tree and clue Binary Tree
27
Page 27
Else {
Thrt-> lchild = T; Pre = thrt; P = T;
Do {While (p) {s [I ++] = P; P = p-> lchild ;}
// Find the leftmost node in the left subtree of the p Node
If (I> 0)
{P = s [-- I];
If (! P-> lchild) {P-> ltag = 1; p-> lchild = pre ;}
If (! Pre-> rchild) {pre-> rtag = 1; pre-> rchild = P ;}
Pre = P; // pre is the node processed in the previous Loop
P = p-> rchild; // P is the node processed in this loop
}
} While (I> 0 | P! = NULL );
Pre-> rtag = 1; pre-> rchild = thrt; thrt-> rchild = pre;
}
}
6.3 traverse binary tree and clue Binary Tree
28
Page 28
6.4 trees and forests
.........
A
B
C
D
F
G
H
I
J
E
A 1 2 3
-1
B
4 5-1 0
C 6-1-1 0
D 7 8-1
0
E-1-1-1 1
F-1-1-1 1
G-1-1-1 2
H 9
-1-1
3
I-1-1-1 3
J-1-1-1 7
0
1
2
3
4
5
6
7
8
9
Use an array (static chained
Storage) indicates
Tree,-1
It indicates null.
Disadvantage: The NULL pointer field is too large.
Many, (
K-1)
× N
+ 1
.
Example:
Level
K
= 3
Tree
6.4.1 tree storage structure
1. Child Notation: composed of data domains,
K
Child node pointer fields and parent node pointer fields.
Data Child1 child2 childk parents
29
Page 29
A
B
C
D
F
G
H
I
J
E
A
Bytes
B
C
D
Bytes
F
∧ G
∧ H
Bytes
I
Zookeeper
E
Bytes
J
Bytes
6.4 trees and forests
2 child brother Notation: pointer to the first subtree root of a data domain
And the pointer fields pointing to its sibling nodes. Essentially, a binary tree is used to represent
Tree.
Firstchild data nextsibling
30
Page 30
A
B
C
D
F
G
H
I
J
E
A
B
C
D
F
G
H
I
J
E
A
B
C
D
F
G
H
I
J
E
6.4 trees and forests
6.4.2 conversion of trees, forests, and Binary Trees
1 tree is converted into a corresponding binary tree:
1
) The leftmost branch of each node is retained, and other branches are deleted.
2
) The node under the same parent node becomes the brother of its left node.
31
Page 31
B
C
D
F
G
H
I
J
E
B
C
D
F
G
H
I
J
E
A
B
C
D
F
G
H
I
J
E
A
6.4 trees and forests
2. The forest is converted into a corresponding binary tree:
Add a virtual root node whose child node is the root of each tree.
Then, it becomes the problem of converting a tree into a binary tree.
32
Page 32
N
B
C
D
F
G
H
I
J
E
A
T
1
T
2
......
T
3
L
R
Root: a B e f c g d h j I
Hougen: e f B g c j h I d
6.4 trees and forests
6.4.3 tree and forest Traversal
1. traverse the first root and back root of the tree:
1
First root: similar to the first sequential traversal of a binary tree (NLR)
) N:
Root; L
: First child
Tree, R
: For the remaining subtree, The traversal direction is from the second subtree to the last subtree.
2
) Backend root: similar to the middle-order traversal (LRN) of a binary tree
) L
: The first subtree, R
:
The other sub-trees traverse the direction from the second sub-tree to the last sub-tree,
N:
Root.
33
Page 33
Preface: B e f c g d h j I
Descending order: e f B g c j h I d
B
C
D
F
G
H
I
J
E
A
B
C
D
F
G
H
I
J
E
2. First and last traversal of the Forest:
1
) First-order traversal is similar to the first-order traversal of a tree. Add a virtual root node.
The child node is the root of each tree. Traverse the root of the tree to obtain the first sequence of the forest.
Column (remove the root node ).
2
) The post-order traversal is similar to the post-order traversal of the tree. Add a virtual root node.
The child node is the root of each tree. Traverse the root of the tree to obtain the descending order of the forest.
Column (remove the root node ).
6.4 trees and forests
34
Page 34
6.6 Heman tree and Its Application
Path Length: the total number of branches between nodes.
Tree path length: The sum of the path lengths from the root node to each node
Length of the tree's weighted path (
WPL)
:
Sum of weighted path lengths of leaf nodes
Optimal binary tree or Heman tree: the length of the tree's weighted path
WPL
Minimum Binary Tree
E
G
H
L
L
E
H
G
E
G
H
L
7
7
7
5
2
4
4
4
2
2
5
5
WPL = 36
WPL = 46
WPL = 35
6.6.1 optimal binary tree)


N
I
I
I
L
L
W
W
W P L
W P L
1
35
Page 35
Score
Level
Proportion
0 ~ 59-60 ~ 69 70 ~ 79 80 ~ 89 90 ~ 100
E D C B
0.05 0.15 0.40 0.30 0.10
A <60
A <70
A <80
A <90
E
D
C
B
Y n
N
N
N
Y
Y
Y
70 ≤ A <80
C
B
E
D
Y n
Y
Y
N
80 ≤ A <90
60 ≤ A <70
A <60
A <80
A <70 A <90
A <60
E d
C B
N
N
N
N
Y n
Y
Y
Y
Decision tree:
6.6 Heman tree and Its Application
WPL = 1, 3.15
WPL = 1, 2.05
WPL = 1, 2.2
36
Page 36
(1) by the given n weights {w0, W1, W2 ,...
, Wn-1}, constructed with N Binary Trees
F = {t0, T1, T2 ,...
, Tn-1}, where each binary tree Ti only
The left and right subtree of a root node with a valid WI are empty.
(2) Repeat the following steps until only one tree is left in F:
① Select the binary tree with the minimum weight of the two root nodes in F as the left and right
The subtree constructs a new binary tree. The weight of the root node of the New Binary Tree is
The sum of the weights of the root nodes on the left and right trees.
② Delete the two binary trees in F.
③ Add the new binary tree to F.
6.6 Heman tree and Its Application
Heman algorithm:
37
Page 37
Construction Process of the Heman tree
38
Page 38
The main purpose of Heman encoding is to compress data. Set a packet: Cast cast
Sat at a tasa, Character Set combination is {C, A, S, T}, and the occurrence frequency (times) of each character is
W = {2, 7, 4, 5 }. If each character is encoded as a: 00 T: 10 C: 01 S: 11,
The total encoding length is (2 + 7 + 4 + 5) x 2 = 36. If not
It is expected to reduce the total encoding length.
6.3.2 Heman Encoding
Build a Heman Based on the Occurrence Frequency of each character
Tree. 0 is assigned to the left branch, 1 is assigned to the right branch, and Heman encoding is obtained.
A: 0 T: 10 C: 110 S: 111, total encoding length:
7*1 + 5*2 + (2 + 4) * 3 = 35, total ratio of the same-Length Encoding
The length must be short.
The total length is equal
The length of the weighted path wpl in the Heman tree.
Heman encoding is a non-Prefix encoding. Not decoded
Will be confused.
6
A, 7
S, 4
C, 2
11
T, 5
18
1
1
1
0
0
0
6.6 Heman tree and Its Application
39
Page 39
6
A, 7
S, 4
C, 2
11
T, 5
18
1
1
1
0
0
0
Typedef struct
{Unsigned int weight;
Unsigned int parent, lchild, rchild;
} Htnode, * huffmantree:
Typedef char ** hufmancode;
Implementation of Heman encoding:
1
Build with 2
N-1
Array of units,
Where
N
Units are used to save the initial node,
N-1
Nodes are used to represent Internal nodes.
2
Construct a Heman tree, that is, execute
N-1
Cycle, each time an internal node is generated.
The two nodes with the smallest weight value are their left and right sons.
3
Calculate the Heman encoding of each character.
6.6 Heman tree and Its Application
40
Page 40
Void hufmancode (hufumantree & HT, humancode & HC, int * w, int N)
{If (n <= 1) return;
M = 2 * n-1;
Ht = (huffmantree) malloc (m + 1) * sizeof (htnode); // No unit 0
For (P = HT + 1, I = 1; I <= N; ++ I, ++ P, ++ W) * P = {* w, 0, 0, 0 };
For (; I <= m; ++ I, ++ p) * P = {0, 0, 0, 0 };
For (I = n + 1; I <= m; ++ I)
{Select (HT, I-1, S1, S2 );
HT [S1]. Parent = I; HT [s2]. Parent = I;
HT [I]. lchild = S1; HT [I]. rchild = S2;
HT [I]. Weight = HT [S1]. Weight + HT [s2]. weight;
}
6.6 Heman tree and Its Application
41
Page 41
Hc = (huffmancode) malloc (n + 1) * sizeof (char *));
Cd = (char *) malloc (N * sizeof (char ));
CD [n-1] = '/0 ';
For (I = 1; I <= N; ++ I)
{Start = n-1;
For (C = I, F = HT [I]. parent; F! = 0; C = F, F = HT [f]. Parent)
If (HT [f]. lchild = c) CD [-- start] = '0 ';
Else CD [-- start] = '1 ';
HC [I] = (char *) malloc (n-Start) * sizeof (char *));
Strcpy (HC [I], & CD [start]);
}
Free (CD );
}
6.6 Heman tree and Its Application
Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.