Upload
wilfrid-doyle
View
344
Download
0
Embed Size (px)
Citation preview
Data Structures(Data Structures( 数据结数据结构构 ))
Course 7:TreeCourse 7:Tree
2西南财经大学天府学院
VocabularyVocabularyTree 树Subtree 子树Branch 分枝Indegree 入度Outdegree 出度Degree 度Leaf 叶子Internal node 内部结点Level 层Height(depth) 高度(深度)Chart format 图格式Indented list 缩进表(锯齿状表)Parenthetical list 多层括号表 ( 广义表 )Binary tree 二叉树Balance 平衡
Balance factor 平衡因子Complete binary trees 完全二叉树Traversal 遍历depth first 深度优先breadth first 广度优先Preorder traversal 先 ( 根次 ) 序遍历inorder traversal 中序遍历Postorder traversal 后序遍历Infix (post pre ) 中 ( 后 前 ) 缀表达式Operator 运算符Operand 运算分量Hufman code 哈夫曼编码Weight 权值Binary search tree 二分查找树AVL 平衡二叉树
3西南财经大学天府学院
Chapter 7 introduction to treesChapter 7 introduction to trees7-1 Basic tree concepts
ListTree
Tree: consists of a finite set of elements(nodes), and a finite set of directed lines (branches) that connect the nodes
A
G
D
F
C
E
B
Recall that a list is a collection of components in which 1. each component (except one, the first)
has exactly 1 predecessor. 2. each component (except one, the last)
has exactly 1 successor.
a tree is very similar: it has property (1) but (2) is slightly relaxed
Figure 7-1 A tree
4西南财经大学天府学院
Root: the first nodeIndegree = zero
A
G
D
F
C
E
BDegree: the number of branches (the sum of indegree and outdegree)
Outdegree:branch is directed away from the node
Indegree:branch is directed toward the node
All of the nodes (exception of the root)have only one indegree,but have zero,one and more outdegreeNote : empty tree , no node
Basic tree conceptsBasic tree concepts
5西南财经大学天府学院
terminologyterminology
A
G
D
F
C
E
B
Leaf: node with an outdegree of zeroInternal node: is not a root or a leafParent: has successor nodes (outdegree > 0)Child: a node with a predecessor (indegree=1)Siblings: nodes with the same parentAncestor: any node in the path from the root to the node Descendent: any node in the path below the parent node
Path: a sequence of nodes in which each node is adjacent to the next one, every node can be reached from the rootsuch as ADG,ABE,…
BranchAD
BranchDG
6西南财经大学天府学院
TerminologyTerminology
A
G
D
F
C
E
B
Level of a node is its distance from the rootRoot: level 0The children of root level 1Siblings at the same level
Level 0
Level 1
Level 2
Height(depth) of the tree is the level of the leaf in the longest path from the root+1 ( = 3 )The height of an empty tree is 0 ( 书上为 -1)
A
G
D
F
C
E
B
7西南财经大学天府学院
Subtree B
Subtree C
Subtree D
Subtree: divided from a tree ,any connected structure below the root,Subtrees can be subdivided into subtrees
A
G
D
F
C
E
B
TerminologyTerminology
Recursive definition of a Recursive definition of a treetreeA tree is a set of nodes that either (1) Empty(2) Has a designated node—root,from
which hierarchically descent zero or more subtrees,which are also trees
8西南财经大学天府学院
Tree representationTree representationTree is implemented in computer using pointers,there are three representations outside the computer
computer
case CPU 3.5”disk CD-ROM
ROMALUcontroller
1 Computer1-1 case1-2 CPU1-2-1 controller1-2-2 ALU….1-2-9 ROM1-3 3.5” Disk…1-9 CD-ROM
Chart format: general treeIndented list: bill-of-materials (goezinta, goes into)Parenthetical listing: 广义表To convert a tree to PN
uses algebraic expressions,Open parenthesis indicates the start of a new level, each closing parenthesis completes the current level and moves up one level in the tree, consider the tree in Figure 7-1,its parenthetical notation isA ( B ( E ) C D ( F G ) )
Algorithm converttoparen (val root <node pointer>, ref output <strings>)Convert a general tree to parenthetical notation.Pre root is a pointer to a tree nodePost output contains parenthetical notation1 place root in output2 if ( root is a parent ) 1 place an open parenthesis in the output 2 converttoparen ( root’s first child ) 3 loop ( more siblings ) 1 converttoparen ( root’s next child ) 4 end loop 5 place close parenthesis in the output3 end if4 returnEnd converttoparen
9西南财经大学天府学院
7-2 Binary trees 7-2 Binary trees
A
F
E
DC
B
Left subtree Right subtree
No node can have more than two subtrees, or a node can have zero,one, or two subtrees are designated as left subtree and right subtree
10西南财经大学天府学院
Several binary treesSeveral binary trees
(a)
(g) (h)(f)(e)
(d)(c)(b)
AA
B
A
B
A
B C
A
CB
ED
A
B
C
A
B
C
Figure 7-6 A collection of binary trees
Null tree, with no nodes
symmetry is not required
11西南财经大学天府学院
propertiesproperties
二叉树第 i 层上至多有 2i 结点深度为 k 的二叉树最多有 2k-1 个结点对任何一棵二叉树有 n0=n2+1
对一棵有 n 个结点的完全二叉树,按层编号对任一结点有i=1 为根结点i>1 双亲结点2i>n, 则 i 无左孩子,否则左孩子为 2i2i + 1>n, 则 i 无右孩子,否则右孩子为 2i+1
2/i
12西南财经大学天府学院
propertiesproperties
•Height of binary trees•Balance•Balance factor•Complete binary trees•Nearly complete tree
The distance of a node from the root determines the efficiency it can be locatedThe children of any node: only one branch pathNodes at level 2 : two braches from the rootSo the shorter the tree, the easier to locate any desired node
Balance factor(B) is the difference in height between its left and right subtrees ( HL, HR) B = HL – HR
A tree is balanced if its balance factor is 0 and its subtrees are also balanced (seldom)More generally, is –1, 0, +1
Given the nodes of the binary tree , NThe height of the tree
Hmax= N all in one direction(branch)
Hmin = +1Given a height of the binary tree,H
The number of nodes in the tree Nmin = HNmax = 2H - 1
N2log
A complete tree has the maximum number of entries for its height.the maximum number is reached when the last level is full . such as
A
B C
A
CB
ED
A
E F
If it has the minimum height for its nodes and all nodes in the last level are found on the left . such as
A
B C
A
CB
ED
A
FD E
CB
D
13西南财经大学天府学院
Binary tree structureBinary tree structure
leftsubtree data rightsubtree
The node of binary tree
Node leftsubtree <pointer to node> data <datatype> rightsubtree <pointer to node>End node
Traditionally, the subtree pointers are simply called left and right
14西南财经大学天府学院
Binary tree traversalsBinary tree traversals
Traversal: each node of the tree be processed once and only once in a predetermined sequence Approaches: depth first and breadth first
Depth first traversal: the processing proceeds along a path from the root through one child to the most distant descendent of that first child before processing a second child.Process all of the descendents of a child before going on to the next child.
breadth first traversal: the processing proceeds horizontally from the root to all of its children, then to its children’s children . And so forth until all nodes have been processedEach level is completely processed before the next level is started
15西南财经大学天府学院
Depth-first traversals
Given that a binary tree consists of root, a left subtree, and a right subtreeWe can define 6 different depth –first traversal sequences.Computer scientist have assigned three of these sequences standard names in the literature
1
2 3
1
2 3
1
2 3
Left subtree Right subtree(a) Preorder traversal(b) inorder traversal(c) postorder traversal
Figure 7-8 binary tree traversals
Traditional designation: N- root, L-leftsubtree, R-right subtree
16西南财经大学天府学院
Preorder traversal (NLR)Preorder traversal (NLR)
Note:The root node is processed first, before the left subtree, right subtree
Recursive algorithm:
Algorithm preOrder (val root <node pointer> )Traverse a binary tree in node-left-right sequencePre root is the entry node of a tree or subtreePost each node has been processed in order1 if (root is not null) 1 process (root) 2 preOrder (root->leftsubtree) 3 preOrder (root->rightsubtree)2 end if3 returnEnd preOrder
Algorithm 7-2 preorder traversal of a binary tree
example
A
F
E
DC
B
Figure 7-9 a binary tree for traversals
A
F
E
DC
B
Walking order A B C D E F
17西南财经大学天府学院
Inorder traversal (LNR)Inorder traversal (LNR)
Note: in the inorder traversal, the node is processed between Its subtrees. left subtree—root—right subtree
Recursive algorithm:
Algorithm inOrder (val root <node pointer>Traverse a binary tree in left-node-right sequencePre root is the entry node of a tree or subtreePost each node has been processed in order1 if (root is not null) 1 inOrder (root->leftsubtree) 2 process (root) 3 inOrder (root->rightsubtree)2 end if3 returnEnd preOrder
Algorithm 7-3 inorder traversal of a binary tree
example
A
F
E
DC
B
Walking order C B D A E F
18西南财经大学天府学院
Postorder traversal (LRN)Postorder traversal (LRN)
Note:in the postorder traversal, the root is processed after the subtrees. left subtree—right subtree—root
Recursive algorithm:
Algorithm postOrder (val root <node pointer>Traverse a binary tree in left-right-node sequencePre root is the entry node of a tree or subtreePost each node has been processed in order1 if (root is not null) 1 postOrder (root->leftsubtree) 2 postOrder (root->rightsubtree) 3 process (root)2 end if3 returnEnd postOrder
Algorithm 7-4 postorder traversal of a binary tree
example
A
F
E
DC
B
Walking order C D B F E A
Note: we took the same path in all three walks;Only the time of the processing changed.
19西南财经大学天府学院
Breadth-first traversalsBreadth-first traversals
Note : We process all of the children of a node before proceeding with the next level.
AlgorithmUses queue
Algorithm breadthfirst (val root <node pointer>Process tree using breadth-first traversal.Pre root is a pointer to a tree nodePost tree has been processed 1 pointer = root2 loop ( pointer not null ) 1 process ( pointer ) 2 if (pointer->left not null ) 1 enqueue ( pointer->left ) 3 end if 4 if (pointer->right not null ) 1 enqueue ( pointer->right ) 5 end if 6 if ( not emptyQueue ) 1 dequeue ( pointer ) 7 else 1 pointer = null 8 end if 3 end loop4 returnEnd breadthfirstAlgorithm 7-5 breadth-first tree traversal
example
A
F
E
DC
B
A
B E
CD F
20西南财经大学天府学院
7-4 expression trees7-4 expression trees
An expression is a sequence of tokens that follow prescribed rules. Token may be either an operand or an operator.We consider only binary arithmetic operators in the formOperand—operator—operand
The properties of an expression tree1. Each leaf is an operand2. The root and internal nodes are operators3. Subtrees are subexpressions, with the root being an operator.
+
+
*
a
b c
da*(b+c)+d
An infix expression and its expression tree
The traversal of expression tree
Three standard traversals represent the three different expression formats: infix, postfix, and prefix. The inorder traversal produces the infix expression The postorder traversal produces the postfix expression The preorder traversal produces the prefix expression
21西南财经大学天府学院
Infix traversalInfix traversal
Traverses the tree and print infix expressionAdd opening parenthesis’ ( ‘at the beginning of each expressionAdd closing parenthesis ’ ) ‘at the end of each expression
Note: root of the tree and each of its subtree represent a subexpression , so print (: when we start a tree or subtree ): when we have processed all of its children
+
+
*
a
b c
d
((a*(b+c))+d)(
(
( )
)
)
Algorithm infix (val tree <tree pointer>Print the infix expression for an expression tree.Pre tree is a pointer to an expression treePost the infix expression has been printed 1 if ( tree not null ) 1 if ( tree->token is an operand ) 1 print ( tree->token ) 2 else 1 print ( open parenthesis ) 2 infix ( tree->left ) 3 print ( tree->token ) 4 infix ( tree->right ) 5 print ( close parenthesis ) 3 end if2 end if3 returnEnd infixAlgorithm 7-6 infix expression tree traversal
Algorithm:
22西南财经大学天府学院
Postfix traversalPostfix traversal
As same as posorder traversal of any binary tree
Note : it does not require parentheses
Algorithm:Algorithm postfix (val tree <tree pointer>Print the postfix expression for an expression tree.Pre tree is a pointer to an expression treePost the postfix expression has been printed 1 if ( tree not null ) 1 postfix ( tree->left ) 2 postfix ( tree->right ) 3 print ( tree->token )2 end if3 returnEnd postfixAlgorithm 7-7 postfix expression tree traversal
Result : a b c + * d +Operation : scan from left one to right, when meet + ,do b+c=x, we get ax*d+ , continue,meet * , do a*x=y , get yd+, continue, y+d, this is the result.
Result:
23西南财经大学天府学院
Prefix traversalPrefix traversal
As same as preorder traversal of any binary tree
Note : it does not require parentheses
Algorithm:Algorithm prefix (val tree <tree pointer>Print the prefix expression for an expression tree.Pre tree is a pointer to an expression treePost the prefix expression has been printed 1 if ( tree not null ) 1 print ( tree->token ) 2 prefix ( tree->left ) 3 prefix ( tree->right )2 end if3 returnEnd prefixAlgorithm 7-8 prefix expression tree traversal
Result : + * a + b c d Operation : scan from left one to right, when meet b c ,do b+c=x, we get + * a x d , continue, meet a x , do a*x=y , get + y d, continue, y+d, this is the result.
Result:
24西南财经大学天府学院
General treeGeneral tree
Each node can have an unlimited outdegreeEach node may have as many children as necessary.Example: the bill of materials (p268)
25西南财经大学天府学院
Changing general tree to binary Changing general tree to binary treetree
It is easier to represent binary tree than general treeUse two relationships: parent to child, sibling to sibling A
B E
C
F
D HG I
(a)general tree
A
B E
C
F
D HG I
(b)identify leftmost children
A
B E
C
F
D HG I
( c ) connect siblings
A
B E
C
F
D HG I
(d) Delete unneeded branches
A
B
EC
FD
H
G
I
(e)the resulting binary tree
26西南财经大学天府学院
7-6 Huffman code7-6 Huffman code
ASCII & EBCDIC are fixed_length codes.Ignore occurs frequentHuffman code assign shorter codes to characters that occur more frequently.A popular data compression algorithmBefore we can assign bit patterns to each character, we assign each character a weight based on its frequency of use.
E=15 T=12 A=10 O=08 R=07 N=06 S=05
U=05 I=04 D=04 M=03 C=03 G=02 K=02
Table 7-2 character weights for a sample of Huffman code
27西南财经大学天府学院
build a treebuild a tree
We can build a tree based on weight values The basic steps:1. organize the entire character set into a row, ordered according to frequency from highest to lowest , each character is now a node at the leaf level of a tree .2. Find the two nodes with the smallest combined frequency weights and join them to form a third node, resulting in a simple two-level tree .the weight of the new node is the combined weights of the original two nodes. This node is eligible to be combined with other nodes. 3. Repeat step 2 until all of the nodes , on every level , are combined into a single tree.
E15
T12
A10
O08
R07
N06
S05
U05
I04
D04
M03
C03
G02
K02
E15
T12
A10
O08
R07
N06
S05
U05
I04
D04
M03
C03
G K
04
A O S U I D E R N M C G K T
10
1 1 1 1
1
1
1
1
1
0
0 0 00
0 0 0
0
00
0 1
11
28西南财经大学天府学院
Figure 7-21----7-24 shows the processNote : in the sixth row, the lowest-value node is 08(O), the second lowest value is 10(A). But there are three 10s: (A), (S-U) and (M-C-G-K)We choose whichever of the 10s is adjacent to the 8. To keeps the branch lines from crossing and allows us to preserve the legibility of the tree.If none of the higher values are adjacent to the lower value, we can rearrange the nodes for clarity To assign codes: 0 to the left branch (or 1)
1 to the right branch (or 0)A character’s code: starting at the root and following the branches that lead to that character. The code is the bit value of each branch on the path taken in sequence.The leading bits of each code are unique, that is, no code is the prefix of any other code
29西南财经大学天府学院
A O S U I D E R N M C G K T
10
1 1 1 1
1
1
1
1
1
0
0 0 00
0 0 0
0
00
0 1
11
A=000 U=0101 E=100 M=11000 K=11011
O=001 I=0110 R=1010 C=11001 T=111
S=0100 D=0111 N=1011 G=11010
Figure 7-24 Huffman code assignment
The resultThe result
30西南财经大学天府学院
7-7 summary7-7 summaryTree consists of nodes(elements) and branch(directed lines)Degree – the number of branches associated with a nodeIndegree—directed toward the nodeOutdegree — directed away from the nodeRoot—the first node with indegree of zeroAll node ,except root have an indegree of oneLeaf—with an outdegree of zeroInternal node—neither the root nor the leafParent, child , siblingsPath—a sequences of nodes in which each node is adjacent to the next oneAn ancestor—any node in the path from the root of a given nodeDescendent—any node in all of the paths from a given node to a leafLevel—the distance from the root Height—the level of the leaf in the longest path from the root +1Subtree—any connected structure below the rootBinary tree—no node have more than two childrenHmin = Hmax= N Hmin = H Nmax = 2n + 1 N2log
31西南财经大学天府学院
7-7 summary7-7 summaryBalance factor : B = HL - HR
Balanced tree B = 0 Binary balanced tree B <= 1Complete tree—the last level is full nearly complete tree—has the minimum height for its nodes and all nodes in the last level are found on the leftA binary tree traversal – visits eachnode of the tree once and only once in a predetermined sequenceDepth first—preorder(NLR), inorder(LNR), postorder(LRN),…Breadth-first—process all nodes in a level before processing to the next levelA general tree—each node can have an unlimited outdegreeChanging a general tree to a binary tree—identify the leftmost children ; connected the siblings from left to right ; delete the unused branches Huffman code—uses a variable-length code to represent characters, shorter code assigns to characters that occur more frequently To create Huffman code—determine the number of occurrences for each character; put the entire character set into a row(leaves of a tree); find the two nodes with the smallest combined frequency weights and link them to a new node to form a tree whose parent’s weight is the sum of the two nodes; repeat until nodes on all levels are combined into a single tree
32西南财经大学天府学院
ExercisesExercises
A binary tree has ten nodes. The inorder and preorder traversal of the tree are shown below. Draw the treePreorder: JCBADEFIGHInorder: ABCEDFJGIH A nearly complete binary tree has nine nodes. The breadth traversal of the tree is given below . draw the treeJCBADEFIGDraw the expression tree and find the infix and prefix expressions for the following postfix expression:*-AB+*CD/EFAccording to weight value W=(2,5,7,9,13) build a Huffman tree, write Huffman code of each leaf node.