Data Structures(????) Course 7:Tree - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Data Structures(????) Course 7:Tree

Description:

Data Structures( ) Course 7:Tree LOGO * * * * Vocabulary Tree Subtree Branch Indegree Outdegree Degree ... – PowerPoint PPT presentation

Number of Views:198
Avg rating:3.0/5.0
Slides: 33
Provided by: ValuedGate1175
Category:

less

Transcript and Presenter's Notes

Title: Data Structures(????) Course 7:Tree


1
Data Structures(????)Course 7Tree
2
Vocabulary
  • Tree ?
  • 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 trees
  • 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
4
Basic tree concepts
Root the first node Indegree zero
Indegreebranch 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
D
C
E
G
F
Outdegreebranch is directed away from the node
5
terminology
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 gt 0)
  • Child a node with a predecessor (indegree1)
  • 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
D
C
E
G
F
6
Terminology
7
Terminology
  • Recursive definition of a tree
  • A tree is a set of nodes that either
  • Empty
  • Has a designated noderoot,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
8
Tree representation
  • Tree is implemented in computer using
    pointers,there are three representations outside
    the computer

Algorithm converttoparen (val root ltnode
pointergt,
ref output ltstringsgt) 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 ( roots first child ) 3
loop ( more siblings ) 1
converttoparen ( roots next child ) 4
end loop 5 place close parenthesis in the
output 3 end if 4 return End
converttoparen
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
9
7-2 Binary trees
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
A
B
E
F
D
C
Right subtree
Left subtree
10
Several binary trees
symmetry is not required
Null tree, with no nodes
A
A
A
B
B
(a)
(b)
(d)
(c)
A
A
A
A
C
B
B
B
B
C
E
D
C
C
(g)
(h)
(f)
(e)
Figure 7-6 A collection of binary trees
11
properties
  • ????i????? 2i??
  • ???k???????2k-1???
  • ?????????n0n21
  • ????n?????????,??????????
  • i1 ????
  • igt1????
  • 2igtn,?i????,??????2i
  • 2i1gtn,?i????,??????2i1

12
properties
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

13
Binary tree structure
The node of binary tree
leftsubtree data rightsubtree
Node leftsubtree ltpointer to nodegt
data ltdatatypegt rightsubtree
ltpointer to nodegt End node
Traditionally, the subtree pointers are simply
called left and right
14
Binary 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 childrens children . And
so forth until all nodes have been processed Each
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 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
16
Preorder traversal (NLR)
NoteThe root node is processed first, before
the left subtree, right subtree
17
Inorder traversal (LNR)
Note in the inorder traversal, the node is
processed between Its subtrees. left
subtreerootright subtree
18
Postorder traversal (LRN)
Notein the postorder traversal, the root is
processed after the subtrees. left
subtreeright subtreeroot
Note we took the same path in all three
walks Only the time of the processing changed.
19
Breadth-first traversals
Note We process all of the children of a node
before proceeding with the next level.
20
7-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 form Operandoperator
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
21
Infix traversal
Algorithm infix (val tree lttree pointergt 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-gttoken is an operand
) 1 print ( tree-gttoken ) 2 else
1 print ( open parenthesis )
2 infix ( tree-gtleft ) 3 print (
tree-gttoken ) 4 infix ( tree-gtright )
5 print ( close parenthesis ) 3 end
if 2 end 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
22
Postfix traversal
As same as posorder traversal of any binary tree
Algorithm postfix (val tree lttree pointergt 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-gtleft ) 2 postfix ( tree-gtright )
3 print ( tree-gttoken ) 2 end 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 bcx, we get
axd , continue,meet , do axy , get yd,
continue, yd, this is the result.
Result
23
Prefix traversal
As same as preorder traversal of any binary tree
Algorithm prefix (val tree lttree pointergt 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-gttoken )
2 prefix ( tree-gtleft ) 3 prefix
( tree-gtright ) 2 end 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 bcx, we
get a x d , continue, meet a x , do axy ,
get y d, continue, yd, this is the result.
Result
24
General tree
  • Each node can have an unlimited outdegree
  • Each node may have as many children as necessary.
  • Example the bill of materials (p268)

25
Changing general tree to binary tree
  • It is easier to represent binary tree than
    general tree
  • Use two relationships parent to child, sibling
    to sibling

26
7-6 Huffman code
  • 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.

E15 T12 A10 O08 R07 N06 S05
U05 I04 D04 M03 C03 G02 K02
Table 7-2 character weights for a sample of
Huffman code
27
build 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.

28
  • 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 characters 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
The result
Figure 7-24 Huffman code assignment
30
7-7 summary
  • Tree consists of nodes(elements) and
    branch(directed lines)
  • Degree the number of branches associated with a
    node
  • Indegreedirected toward the node
  • Outdegree directed away from the node
  • Rootthe first node with indegree of zero
  • All node ,except root have an indegree of one
  • Leafwith an outdegree of zero
  • Internal nodeneither the root nor the leaf
  • Parent, child , siblings
  • Patha sequences of nodes in which each node is
    adjacent to the next one
  • An ancestorany node in the path from the root of
    a given node
  • Descendentany node in all of the paths from a
    given node to a leaf
  • Levelthe distance from the root
  • Heightthe level of the leaf in the longest path
    from the root 1
  • Subtreeany connected structure below the root
  • Binary treeno node have more than two children
  • Hmin Hmax N
    Hmin H Nmax 2n 1

31
7-7 summary
  • Balance factor B HL - HR
  • Balanced tree B 0 Binary
    balanced tree B lt 1
  • Complete treethe last level is full
  • nearly complete treehas 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 firstpreorder(NLR), inorder(LNR),
    postorder(LRN),
  • Breadth-firstprocess all nodes in a level before
    processing to the next level
  • A general treeeach node can have an unlimited
    outdegree
  • Changing a general tree to a binary treeidentify
    the leftmost children connected the siblings
    from left to right delete the unused branches
  • Huffman codeuses a variable-length code to
    represent characters, shorter code assigns to
    characters that occur more frequently
  • To create Huffman codedetermine 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 parents weight is the sum of
    the two nodes repeat until nodes on all levels
    are combined into a single tree

32
Exercises
  • 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
  • -ABCD/EF
  • According to weight value W(2,5,7,9,13) build a
    Huffman tree, write Huffman code of each leaf
    node.
Write a Comment
User Comments (0)
About PowerShow.com