- 119 Views
- Uploaded on
- Presentation posted in: General

Data Structures( 数据结构 ) Course 7:Tree

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Data Structures(数据结构)Course 7:Tree

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 平衡二叉树

- Tree 树
- Subtree 子树
- Branch 分枝
- Indegree 入度
- Outdegree 出度
- Degree 度
- Leaf 叶子
- Internal node 内部结点
- Level 层
- Height(depth) 高度（深度）
- Chart format 图格式
- Indented list 缩进表（锯齿状表）
- Parenthetical list多层括号表(广义表)
- Binary tree 二叉树
- Balance 平衡

A

B

C

D

E

F

G

- 7-1 Basic tree concepts
- List
- Tree

- Recall that a list is a collection of components in which
- each component (except one, the first) has exactly 1 predecessor.
- each component (except one, the last) has exactly 1 successor.

a tree is very similar: it has property (1) but (2) is slightly relaxed

Tree: consists of a finite set of elements(nodes), and a finite set of directed lines (branches) that connect the nodes

Figure 7-1 A tree

Root: the first node

Indegree = zero

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 outdegree

Note : empty tree , no node

A

Degree: the number of branches (the sum of indegree and outdegree)

B

C

D

E

F

G

Outdegree:branch is directed away from the node

Path: a sequence of nodes in which each node is adjacent to the next one, every node can be reached from the root

such as ADG,ABE,…

- Leaf: node with an outdegree of zero
- Internal node: is not a root or a leaf
- Parent: has successor nodes (outdegree > 0)
- Child: a node with a predecessor (indegree=1)
- Siblings: nodes with the same parent
- Ancestor: any node in the path from the root to the node
- Descendent: any node in the path below the parent node

Branch

AD

A

B

Branch

DG

C

D

E

F

G

Level of a node is its distance from the root

Root: level 0

The children of root level 1

Siblings at the same level

Level 0

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

Level 1

B

C

D

A

E

Level 2

F

G

B

C

D

E

F

G

A

B

C

D

E

F

G

Subtree C

Subtree B

Subtree D

- Recursive definition of a tree
- A tree is a set of nodes that either
- Empty
- Has a designated node—root,from which hierarchically descent zero or more subtrees,which are also trees

Subtree: divided from a tree ,any connected structure below the root,

Subtrees can be subdivided into subtrees

computer

case

CPU

3.5”disk

CD-ROM

controller

ALU

ROM

Algorithm converttoparen (val root <node pointer>,

ref output <strings>)

Convert a general tree to parenthetical notation.

Pre root is a pointer to a tree node

Post output contains parenthetical notation

1 place root in output

2 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 output

3 end if

4 return

End converttoparen

- Tree is implemented in computer using pointers,there are three representations outside the computer

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 is

A ( B ( E ) C D ( F G ) )

- Computer
- 1-1 case
- 1-2 CPU
- 1-2-1 controller
- 1-2-2 ALU
- ….
- 1-2-9 ROM
- 1-3 3.5” Disk
- …
- 1-9 CD-ROM

- Chart format:
- general tree
- Indented list:
- bill-of-materials
- (goezinta, goes into)
- Parenthetical listing:
- 广义表
- To convert a tree to PN

No node can have more than two subtrees, or a node can have zero,one, or two subtrees are designated as left subtreeand right subtree

A

B

E

C

D

F

Right subtree

Left subtree

symmetry is not required

Null tree, with no nodes

A

A

A

B

B

(a)

(b)

(c)

(d)

A

A

A

A

B

C

B

B

B

C

D

E

C

C

(e)

(f)

(g)

(h)

Figure 7-6 A collection of binary trees

- 二叉树第i层上至多有 2i结点
- 深度为k的二叉树最多有2k-1个结点
- 对任何一棵二叉树有n0=n2+1
- 对一棵有n个结点的完全二叉树，按层编号对任一结点有
i=1 为根结点

i>1双亲结点

2i>n,则i无左孩子，否则左孩子为2i

2i＋1>n,则i无右孩子，否则右孩子为2i+1

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

If it has the minimum height for its nodes and all nodes in the last level are found on the left . such as

Given the nodes of the binary tree , N

The height of the tree

Hmax= N all in one direction(branch)

Hmin = +1

Givena heightof the binary tree,H

The number of nodes in the tree

Nmin = H

Nmax = 2H - 1

A

A

A

A

A

A

B

C

B

C

B

B

C

C

B

C

D

D

E

D

D

E

E

F

E

F

The distance of a node from the root determines the efficiency it can be located

The children of any node: only one branch path

Nodes at level 2 : two braches from the root

So 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

- Height of binary trees
- Balance
- Balance factor
- Complete binary trees
- Nearly complete tree

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

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 processed

Each level is completely processed before the next level is started

1

1

1

Left subtree

2

Right subtree

3

2

3

2

3

(c) postorder traversal

(a) Preorder traversal

(b) inorder traversal

Figure 7-8 binary tree traversals

Given that a binary tree consists of root, a left subtree, and a right subtree

We can define 6 different depth –first traversal sequences.

Computer scientist have assigned three of these sequences standard names in the literature

Traditional designation: N- root, L-leftsubtree, R-right subtree

AlgorithmpreOrder (val root <node pointer>）

Traverse a binary tree in node-left-right sequence

Preroot is the entry node of a tree or subtree

Post each node has been processed in order

1 if (root is not null)

1 process (root)

2 preOrder (root->leftsubtree)

3 preOrder (root->rightsubtree)

2 end if

3 return

End preOrder

Algorithm 7-2 preorder traversal of a binary tree

A

A

Recursive

algorithm:

B

B

E

E

example

C

C

D

D

F

F

Walking order

A B C D E F

Figure 7-9 a binary tree for traversals

Note:The root node is processed first,

before the left subtree, right subtree

AlgorithminOrder (val root <node pointer>

Traverse a binary tree in left-node-right sequence

Preroot is the entry node of a tree or subtree

Post each node has been processed in order

1 if (root is not null)

1 inOrder (root->leftsubtree)

2 process (root)

3 inOrder (root->rightsubtree)

2 end if

3 return

End preOrder

Algorithm 7-3 inorder traversal of a binary tree

A

Recursive

algorithm:

B

E

example

C

D

F

Walking order

C B D A E F

Note: in the inorder traversal, the node is processed between

Its subtrees. left subtree—root—right subtree

AlgorithmpostOrder (val root <node pointer>

Traverse a binary tree in left-right-node sequence

Preroot is the entry node of a tree or subtree

Post each node has been processed in order

1 if (root is not null)

1 postOrder (root->leftsubtree)

2 postOrder (root->rightsubtree)

3process (root)

2 end if

3 return

End postOrder

Algorithm 7-4 postorder traversal of a binary tree

A

Recursive

algorithm:

B

E

example

C

D

F

Walking order

C D B F E A

Note:in the postorder traversal, the root is processed after

the subtrees. left subtree—right subtree—root

Note: we took the same path in all three walks;

Only the time of the processing changed.

Algorithm breadthfirst (val root <node pointer>

Process tree using breadth-first traversal.

Preroot is a pointer to a tree node

Post tree has been processed

1 pointer = root

2 loop ( pointer not null )

1 process ( pointer )

2 if (pointer->left not null )

1 enqueue ( pointer->left )

3end if

4 if (pointer->right not null )

1 enqueue ( pointer->right )

5end if

6 if ( not emptyQueue )

1 dequeue ( pointer )

7 else

1 pointer = null

8 end if

3 end loop

4 return

End breadthfirst

Algorithm 7-5 breadth-first tree traversal

A

example

Algorithm

Uses queue

B

E

C

D

F

A

B E

CD F

Note : We process all of the children of a node before proceeding with the next level.

+

a*(b+c)+d

*

d

+

a

b

c

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 form

Operand—operator—operand

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

- The properties of an expression tree
- Each leaf is an operand
- The root and internal nodes are operators
- Subtrees are subexpressions, with the root being an operator.

An infix expression and its expression tree

The traversal of

expression tree

((a*(b+c))+d)

+

*

d

+

a

b

c

(

)

(

)

(

)

Algorithm infix (val tree <tree pointer>

Print the infix expression for an expression tree.

Pre tree is a pointer to an expression tree

Post 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 )

3end if

2end if

3 return

End infix

Algorithm 7-6 infix expression tree traversal

Traverses the tree and print infix expression

Add opening parenthesis’ ( ‘at the beginning of each expression

Add 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

Algorithm:

As same as posorder traversal of any binary tree

Algorithm postfix (val tree <tree pointer>

Print the postfix expression for an expression tree.

Pre tree is a pointer to an expression tree

Post the postfix expression has been printed

1 if ( tree not null )

1 postfix ( tree->left )

2 postfix ( tree->right )

3 print ( tree->token )

2end if

3 return

End postfix

Algorithm 7-7 postfix expression tree traversal

Note : it does not require parentheses

Algorithm:

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:

As same as preorder traversal of any binary tree

Algorithm prefix (val tree <tree pointer>

Print the prefix expression for an expression tree.

Pre tree is a pointer to an expression tree

Post the prefix expression has been printed

1 if ( tree not null )

1 print ( tree->token )

2 prefix ( tree->left )

3 prefix ( tree->right )

2end if

3 return

End prefix

Algorithm 7-8 prefix expression tree traversal

Note : it does not require parentheses

Algorithm:

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:

- Each node can have an unlimited outdegree
- Each node may have as many children as necessary.
- Example: the bill of materials (p268)

A

B

E

C

F

A

A

A

D

G

B

B

B

E

E

E

F

F

F

(b)identify leftmost children

(a)general tree

H

C

C

C

D

D

D

G

G

G

H

H

H

I

I

I

I

A

(e)the resulting binary tree

B

E

F

C

D

G

H

I

（c）connect siblings

(d) Delete unneeded branches

- It is easier to represent binary tree than general tree
- Use two relationships: parent to child, sibling to sibling

- ASCII & EBCDIC are fixed_length codes.
- Ignore occurs frequent
- Huffman code assign shorter codes to characters that occur more frequently.
- A popular data compression algorithm
- Before we can assign bit patterns to each character, we assign each character a weight based on its frequency of use.

Table 7-2 character weights for a sample of Huffman code

E

T

A

O

R

N

S

U

I

D

M

C

G

K

04

15

12

10

08

07

06

05

05

04

04

03

03

02

02

E

T

A

O

R

N

S

U

I

D

M

C

G

K

15

12

10

08

07

06

05

05

04

04

03

03

1

0

1

0

1

0

0

0

0

1

1

1

1

0

0

1

1

0

0

1

0

0

1

0

1

1

N

A

O

S

U

I

D

E

R

M

C

G

K

T

- 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.

- Figure 7-21----7-24 shows the process
- Note : 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

1

0

1

0

1

0

0

0

0

1

1

1

1

0

0

1

1

0

0

1

0

0

1

0

1

1

N

A

O

S

U

I

D

E

R

M

C

G

K

T

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

- Tree consists of nodes(elements) and branch(directed lines)
- Degree – the number of branches associated with a node
- Indegree—directed toward the node
- Outdegree — directed away from the node
- Root—the first node with indegree of zero
- All node ,except root have an indegree of one
- Leaf—with an outdegree of zero
- Internal node—neither the root nor the leaf
- Parent, child , siblings
- Path—a sequences of nodes in which each node is adjacent to the next one
- An ancestor—any node in the path from the root of a given node
- Descendent—any node in all of the paths from a given node to a leaf
- Level—the distance from the root
- Height—the level of the leaf in the longest path from the root +1
- Subtree—any connected structure below the root
- Binary tree—no node have more than two children
- Hmin = Hmax= N Hmin = H Nmax = 2n + 1

- Balance factor : B = HL - HR
- Balanced tree B = 0 Binary balanced tree B <= 1
- Complete 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 left
- A binary tree traversal – visits eachnode of the tree once and only once in a predetermined sequence
- Depth first—preorder(NLR), inorder(LNR), postorder(LRN),…
- Breadth-first—process all nodes in a level before processing to the next level
- A general tree—each node can have an unlimited outdegree
- Changing 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

- A binary tree has ten nodes. The inorder and preorder traversal of the tree are shown below. Draw the tree
Preorder: JCBADEFIGH

Inorder: ABCEDFJGIH

- A nearly complete binary tree has nine nodes. The breadth traversal of the tree is given below . draw the tree
JCBADEFIG

- Draw the expression tree and find the infix and prefix expressions for the following postfix expression:
*-AB+*CD/EF

- According to weight value W=(2,5,7,9,13) build a Huffman tree, write Huffman code of each leaf node.