# Trees - PowerPoint PPT Presentation

PPT – Trees PowerPoint presentation | free to download - id: 6c0111-OWFlN The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
Title:

## Trees

Description:

### Document presentation ... Postorder traversal Binary search tree Expressions in ... inorder traversal PowerPoint Right in ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 56
Provided by: parCseNs4
Category:
Tags:
Transcript and Presenter's Notes

Title: Trees

1
Chapter 5
• Trees

2
Binary trees
• A binary tree is a finite set of elements that is
either empty or is partitioned into 3 disjoint
subsets

root left subtree right subtree
3
An example of binary tree (1)
• root A
• node A, B, C, , H, I
• father of B A
• sons of B D, E
• left son of B D
• right son of B E
• depth 3
• ancestors of E A, B
• descendants of B D, E, G

level
0
A
1
C
B
2
F
E
D
3
G
H
I
4
An example of binary tree (2)
• left descendant of B D
• right descendant of B E, G
• brother B and C are brothers
• D and E are brothers
• leaf a node that has no sons e.g. D, G, H, I

right subtree of A
left subtree of A
B
C
D
E
F
G
H
I
5
Not binary trees
A
A
B
C
B
C
E
D
F
E
D
F
A
G
H
I
G
B
C
(a)
(b)
E
D
F
G
H
I
(c)
6
Strictly binary tree
• Each nonleaf node has nonempty left and right
subtrees.

A
C
B
E
D
F
G
7
Complete binary tree of depth 3
• of nodes in a complete binary tree of depth d

A
B
C
E
D
G
F
J
I
H
K
N
M
L
O
8
Implementation of a binary tree
• Each node has 3 fields.
• ??? "father" ??? 4 ? field.

left son
information
right son
A
null C null
B
null E null
null D null
9
• define NUMNODES 500
• struct nodetype
• int info
• int left
• int right
• int father
• struct nodetype nodeNUMNODES

10
Dynamic node representation
• struct nodetype
• int info
• struct nodetype left
• struct nodetype right
• struct nodetype father
• typedef struct nodetype NODEPTR

11
Creation of a new tree
• maketree(x) Create a new tree consisting of a
• single node
• NODEPTR maketree(int x)
• NODEPTR p
• p getnode()
• p-gtinfo x
• p-gtleft NULL
• p-gtright NULL
• return(p)
• / end maketree /

X
12
Creation of a new son
• setleft(p, x) create a new left son of node p
• with information field x.
• void setleft(NODEPTR p, int x)
• if (p NULL)
• printf("void insertion\n")
• else if (p-gtleft ! NULL)
• printf("invalid insertion\n")

p
p
x
13
An application of binary trees
• Finding all duplicate numbers in a number series.
• 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14,
5
• build a binary search tree
• smaller numbers stored in the left subtree.
• larger numbers stored in the right subtree.
• Duplicate numbers

14
Implementation with C
• struct nodetype
• int info
• struct nodetype left
• struct nodetype right
• typedef struct nodetype NODEPTR
• main()
• NODEPTR ptree
• NODEPTR p, q
• int number
• scanf("d", number)
• ptree maketree(number)

15
• while (scanf("d", number) ! EOF)
• p q ptree
• while (number ! p-gtinfo q ! NULL)
• p q
• if (number lt p-gtinfo)
• q p-gtleft
• else
• q p-gtright
• / end while /
• if (number p-gtinfo)
• printf("d is a duplicate\n", number)
• else if (number lt p-gtinfo)
• setleft(p, number)
• else
• setright(p, number)
• / end while /
• / end main /

16
Traversals in a binary tree (1)
• (1) preorder (depth-first order)
• root
• left subtree
• right subtree
• (2) inorder (symmetric order)
• left subtree
• root
• right subtree
• (3) postorder
• left subtree
• right subtree
• root

17
Traversals in a binary tree (2)
• preorder
• inorder
• postorder

18
Preorder traversal
• void pretrav(NODEPTR tree)
• if (tree ! NULL)
• printf("d\n", tree-gtinfo) // visit the
root
• pretrav(tree-gtleft) // traverse left
subtree
• pretrav(tree-gtright)// traverse right
subtree
• / end if /
• / end pretrav /

19
Inorder traversal
• void intrav(NODEPTR tree)
• if (tree ! NULL)
• intrav(tree-gtleft) // traverse left
subtree
• printf("d\n", tree-gtinfo) // visit the
root
• intrav(tree-gtright) // traverse right
subtree
• / end if /
• / end intrav /

20
Postorder traversal
• void posttrav(NODEPTR tree)
• if (tree ! NULL)
• posttrav(tree-gtleft) //traverse left
subtree
• posttrav(tree-gtright)//traverse right
subtree
• printf("d\n", tree-gtinfo) // visit the
root
• / end if /
• / end posttrav /

21
Binary search tree
• 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

14
15
4
3
9
18
14
7
9
16
20
17
5
• inorder traversal
• sorted 3,4,4,5,5,7,9,9,14,14,15,16,17,18,20

4
5
22
Expressions in binary trees
• prefix A B C
• ( preorder traversal )
• infix A B C
• ( inorder traversal )
• postfix A B C
• ( postorder traversal )

A

C
B
23
Implicit array for an almost- complete binary
tree (1)
• (1) Each leaf is either at level d or at level
d-1.
• (2) For any node n with a right descendant at
level d, all the left descendants of node n that
are leaves are also at level d.

A
0
2
C
B
1
3
G
F
E
D
6
5
4
J
H
I
8
9
7
0 1 2 3 4 5 6 7 8 9
A B C D E F G H I J
24
Implicit array for an almost- complete binary
tree (2)
• An almost complete binary tree can be implemented
by an array.
• sons of node p
• father of node p
• not an almost complete binary tree

25
Extension to not almost complete binary trees
A
B
C
E
D
G
F
0 1 2 3 4 5 6 7 8 9 10 11 12
A B C D E F G
26
Nonrecursive inorder traversal
• define MAXSTACK 100
• void intrav2(NODEPTR tree)
• struct stack
• int top
• NODEPTR itemMAXSTACK
• s
• NODEPTR p
• s.top -1
• p tree
• do
• /travel down left branches as far as
possible /
• / saving pointers to nodes passed /
• while (p ! NULL)
• push(s, p)
• p p-gtleft
• / end while /

27
• / check if finished /
• if (!empty(s))
• / at this point the left subtree is empty
/
• p pop(s)
• printf("d\n", p-gtinfo) / visit the root
/
• p p-gtright / traverse right subtree
/
• / end if /
• while (!empty(s) p ! NULL)
• / end intrav2 /
• The recursion stack cannot be eliminated.

28
• A node with an empty right subtree points to its
inorder successor. It can be traversed in inorder
without a stack.

setleft(p, x)
A
C
B

F
E
D
setright(p, x)

I
H
G
29
Implementation of a right in-threaded binary tree
• If the array implementation is used,
• positive pointer normal right son
• negative pointer inorder successor
• dynamic implementation
• struct nodetype
• int info
• struct nodetype left // pointer to left son
• struct nodetype right // pointer to right son
• // right is NULL or
• typedef struct nodetype NODEPTR

30
Implementation with C (1)
• void intrav3(NODEPTR tree) // No stack is used
• NODEPTR p, q
• p tree
• do
• q NULL
• while (p ! NULL)
• / traverse left branch /
• q p
• p p-gtleft
• / end while /

31
Implementation with C (2)
• if (q ! NULL)
• printf("d\n", q-gtinfo)
• p q-gtright
• while (q-gtrthread p ! NULL)
• printf("d\n", p-gtinfo)
• q p
• p p-gtright
• / end while /
• / end if /
• while (q ! NULL)
• / end intrav3 /

32
Heterogeneous binary trees
''
• The binary tree represents 3 4(6-7)/5 3.

3
''
'/'
3
5
''
4
'-'
7
6
33
Evaluation of an expression represented by a
binary tree (1)
• define OPERATOR 0
• define OPERAND 1
• struct nodetype
• short int utype / OPERATOR or OPERAND /
• union
• char chinfo
• float numinfo
• info
• struct nodetype left
• struct nodetype right
• typedef struct nodetype NODEPTR
• float evalbintree(NODEPTR tree)
• float opnd1, opnd2
• char symb

34
Evaluation of an expression represented by a
binary tree(2)
• if (tree-gtutype OPERAND) / expression is a
single /
• return (tree-gtnuminfo) / operand /
• / tree-gtutype OPERATOR /
• / evaluate the left subtree /
• opnd1 evalbintree(tree-gtleft)
• / evaluate the right subtree /
• opnd2 evalbintree(tree-gtright)
• symb tree-gtchinfo / extract the
operator /
• / apply the operator and return the result /
• return(oper(symb, opnd1, opnd2))
• / end evalbintree /

35
The Huffman code (1)
• Suppose we have a set of symbols A, B, C, D
• 1) Each symbol is encoded by 3 bits (inefficient)
• Message A B A C C D A would be encoded by 21 bits

symbol code A 0 1 0
B 1 0 0 C 0 0 0
D 1 1 1
010 100 010 000 000 111 010
36
The Huffman code (2)
• 2) Each symbol is encoded by 2 bits
• Message A B A C C D A would be encoded by 14 bits

symbol code A 00
B 01 C
10 D 11
00 01 00 10 10 11 00
37
The Huffman code (3)
• 3) Huffman codes (variable-length codes)
• symbol code
• A 0
• B 110
• C 10
• D 111
• Message A B A C C D A would be encoded by 13
bits
• 0 110 0 10 10 111 0
• A frequently used symbol is encoded by a short
bit string.

38
Huffman tree
39
Construction of a Huffman tree
symbol
E I A D C G B F H 25 15 15 12 7 6 6
4 1 25 15 15 12 7 6 6 5 25 15 15 12 11 7
6 25 15 15 13 12 11 25 23 15 15 13 28 25 23
15 38 28 25 53 38 91
frequency
40
Sym Freq Code Sym Freq Code Sym Freq
Code
A 15 111 D 12 011 G 6
1100 B 6 0101 E 25 10 H
1 01000 C 7 1101 F 4 01001
I 15 00
41
The result of Huffman coding
decode
• A Huffman tree is a strictly binary tree. The
Huffman algorithm is an optimal encoding scheme.

111 01000 10 111 011 A H E A D
encode
42
Representing lists as binary trees (1)
• n of elements

(balanced)
insertion or deletion (kth element)
O(1) (inserting an element following a
given element)
O(n-k)
O(log2n)
finding the kth element
O(1)
O(k)
O(log2n)
43
Representing lists as binary trees (2)
• nonleaf node of leaves in the left subtree
• leaf node contents of a list element

A
F
null
B
C
D
E
4
k3
Finding the kth element k3
1
2
k1
1
1
F
E
A
B
C
D
44
Representing lists as binary trees (3)
• A complete binary tree of depth d has 2d1-1
nodes or 2d leaf nodes.
• If an almost complete binary tree is used to
represent a list, the kth element can be found by
accessing O(log2n) nodes.

45
Deleting elements in the list (1)
4
3
tree
1
1
2
2
F
E
F
E
1
1
1
D
X
A
B
C
D
A
B
X
p
(a)
(b)
46
Deleting elements in the list (2)
• Time O(log2n)

2
1
2
F
E
1
A
B
(d)
X
(c)
47
Trees
• root of the tree A
• B, C, D are brothers.
• degree of a node of sons
• degree of A 3
• degree of B 2
• degree of C 0
• degree of D 1

Fig.1
48
Ordered trees
• ordered tree the subtrees of each node form an
ordered set
• Fig.1 and Fig.2 are different ordered trees.
• In Fig.1,
• oldest son of A B
• youngest son of A D

Fig.1
Fig.2
49
Representation of trees
oldest son
information
next brother
A
null
B
C
D
null
null
null
E
F
null
G
null
null
null
50
Ordered tree and binary tree
• An ordered tree can be represented as a binary
tree.

Binary tree
ordered tree
51
Forest
• an ordered set of ordered trees

52
Representing a forest by a binary tree
• corresponding binary tree
• preorder traversal
• A B C D E F G H I J M K L
• inorder traversal
• B D E F C A H G M J K L I
• postorder traversal
• F E D C B H M L K J I G A

53

Preorder A B C D E Inorder A B C
D E PostorderA B C D E
A
B

E
C
D
(a)

why same?

A

Preorder A B C D E Inorder A B C D
E PostorderB A D C E
B

C
E
D
(b)
54
An example game trees
• tic-tac-toe ????
• evaluation function
• ?????????(??) ? ?????????(??)

X O
X
O
?? X ?? O
X O
X X
O
X O
X
X O
X O
X
X O
X O
X X
O
X O X
X
O
2 3-1
2 3-1
2
2
1 3-2
55
The minimax method