Sunteți pe pagina 1din 22

9/12/2017

Trees

Nature View of a Tree


leaves

branches
root

1
9/12/2017

Computer Scientist’s View

root

leaves

branches
nodes

What is a Tree
• A tree is a finite
nonempty set of
elements. Computers”R”Us
• It is an abstract model
of a hierarchical
structure. Sales Manufacturing R&D
• Consists of nodes with
a parent-child
relation. US International Laptops Desktops
• Applications:
– Organization charts
Europe Asia Canada
– File systems
– Programming
environments

2
9/12/2017

Tree Terminology
• Root: node without parent (A) Subtree: tree consisting of a
• Siblings: nodes share the same parent node and its descendants
• Internal node: node with at least one child
(A, B, C, F)
• External node (leaf ): node without children
(E, I, J, K, G, H, D) A
• Ancestors of a node: parent, grandparent,
grand-grandparent, etc.
• Descendant of a node: child, grandchild,
grand-grandchild, etc. B C D
• Depth of a node: number of ancestors
• Height of a tree: maximum depth of any
node (3)
• Degree of a node: the number of its E F G H
children
• Degree of a tree: the maximum degree of
its node.
I J K

5
subtree

Tree Properties
Property Value
A Number of nodes
Height
C Root Node
B
Leaves
Interior nodes
Ancestors of H
D E F Descendants of B
Siblings of E
Right subtree of A
Degree of this tree
G

H I

3
9/12/2017

Tree Properties
Property Value
A Number of nodes : 9
Height : 4
C Root Node : A
B
Leaves : 5
Interior nodes : 4
Ancestors of H : G, E,B, A
D E F Descendants of B : E, G, H, I
Siblings of E : D, F
Right subtree of A : C
Degree of this tree : 3
G

H I

Intuitive Representation of Tree Node

List Representation
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
The root comes first, followed by a list of links to sub-trees

How many link fields are needed in


such a representation?

Data Link 1 Link 2 … Link n

4
9/12/2017

Trees

• Every tree node:


– object – useful information
– children – pointers to its children

Data

Data Φ Φ Data Φ Data Φ Φ

Data Φ Φ Data Φ Φ Data Φ Φ

A Tree Representation

• A node is represented by an
object storing ∅
– Element
– Parent node B
– Sequence of children nodes
∅ ∅

A D F
B

A D F

∅ ∅
C E
C E
10

5
9/12/2017

Left Child, Right Sibling Representation

Data
Left Right
Child Sibling A

B C D

E F G H I

J K L

11

TREE ADT: Operations


• Operations
– Traversal
– Insertion
– Deletion
– Search
– Copy
– ….

12

6
9/12/2017

Tree Traversal
• Two main methods:
– Preorder
– Postorder
• Recursive definition
• Preorder:
– visit the root
– traverse in preorder the children (subtrees)
• Postorder
– traverse in postorder the children (subtrees)
– visit the root

13

Preorder Traversal
• A traversal visits the nodes of a tree in a systematic manner
• In a preorder traversal, a node is visited before its
descendants
• Application: print a structured document

1
Become Rich

2 5 9
1. Motivations 2. Methods 3. Success Stories
3 4 6 7 8
1.1 Enjoy 1.2 Help 2.1 Get a 2.2 Start a 2.3 Acquired
Life Poor Friends CS PhD Web Site by Google
14

7
9/12/2017

Postorder Traversal
• In a postorder traversal, a node is visited after its
descendants
• Application: compute space used by files in a directory and
its subdirectories
9
cs16/
8
3 7
todo.txt
homeworks/ programs/
1K

1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K

15

Binary Trees

16

8
9/12/2017

Binary Tree
• A binary tree is a tree with the Applications:
following properties: arithmetic expressions
– Each internal node has at most decision processes
two children (degree of two)
searching
– The children of a node are an
ordered pair

• We call the children of an A


internal node left child and
right child
B C
• Alternative recursive definition:
a binary tree is either
– a tree consisting of a single node, D E F G
OR
– a tree whose root has an ordered
pair of children, each of which is a
H I
binary tree
17

Examples of the Binary Tree

A 1 A
A

B B 2
B C

C
3 D E F G
D

4 H I
E 5
18

9
9/12/2017

Difference Between A Tree and A Binary Tree

• The subtrees of a binary tree are ordered; those


of a tree are not ordered.

A A

B B

• Are different when viewed as binary trees.


• Are the same when viewed as trees.

19

Arithmetic Expression Tree


• Binary tree associated with an arithmetic expression
– internal nodes: operators
– external nodes: operands
• Example: arithmetic expression tree for the expression
(2 × (a − 1) + (3 × b))

× ×

2 − 3 b

a 1
20

10
9/12/2017

Decision Tree
• Binary tree associated with a decision process
– internal nodes: questions with yes/no answer
– external nodes: decisions
• Example: dining decision

Want a fast meal?

Yes No

How about coffee? On expense account?

Yes No Yes No
Starbucks Spike’s Al Forno Café Paragon

21

Binary Tree Traversal


• Traversal
– Each node in a tree is processed exactly once in a
systematic manner
• Three main ways of tree traversal
– Preorder
– Inorder
– Postorder

22

11
9/12/2017

Binary Tree Traversal…


• The easiest way to define each
order is by using recursion A

• Preorder traversal (Rlr)


– Process the root node B D
– Traverse the left subtree in
preorder
C E G
– Traverse the right subtree in
preorder
• Preorder traversal: F

ABCDEFG

23

Assignment: Preorder Traversal

Preorder: FBADCEGIH

24

12
9/12/2017

Assignment: Preorder Traversal

Preorder: PFBHGSRYTWZ

25

Binary Tree Traversal…


• Inorder traversal (lRr)
A
– Traverse the left subtree in
Inorder
– Process the root node B D

– Traverse the right subtree in


Inorder C E G
• Inorder traversal: CBAEFDG
F

26

13
9/12/2017

Assignment: Inorder Traversal

Inorder: ABCDEFGHI

27

Assignment: Inorder Traversal

Inorder: BFGHPRSTWYZ

28

14
9/12/2017

Binary Tree Traversal…


• Postorder traversal (lrR)
A
– Traverse the left subtree in
postorder
– Traverse the right subtree in B D
postorder
– Process the root node C E G
• Postorder traversal:
CBFEGDA F

29

Assignment: Postorder Traversal

Postorder: ACEDBHIGF

30

15
9/12/2017

Assignment: Postorder Traversal

Postorder: BGHFRWTZYSP

31

Print Arithmetic Expressions


• Specialization of an inorder traversal
– print operand or operator when visiting node
– print “(“ before traversing left subtree
– print “)“ after traversing right subtree

× ×

2 − 3 b ((2 × (a − 1)) + (3 × b))

a 1

32

16
9/12/2017

Tutorial: Algorithm to Print Arithmetic


Expression using Binary Tree

33

Linked storage representation for


binary tree
• DATA
– Information
associated with
LPTR DATA RPTR node
• LPTR
– Address of location
of left subtree
• RPTR
– Address of location
of right subtree

34

17
9/12/2017

Linked storage representation for


binary tree: option 2
• A node is represented by
an object storing ∅
– Data
– Parent
– Left child B
– Right child

∅ ∅

B A D

A D ∅ ∅ ∅ ∅

C E
C E
35

Binary tree traversal: algorithms


• Recursive
• Iterative

36

18
9/12/2017

Preorder traversal: iterative algorithm


Algorithm PREORDER(T)
Input: A binary tree whose root node address is given by a pointer variable T
Output: Preorder traversal of a tree
[Initialize]
if T=NULL then
return false {Empty Tree}
else
TOP -1
push(S, T)
[Process each stacked branch address]
Repeat while TOP>=0
[Get stored address and branch left]
P pop(S)
Repeat while P != NULL
write DATA(P)
if RPTR(P) != NULL then
push(S,RPTR(P)) [Store address of nonempty right subtree]
P LPTR(P) [branch left]
[Finished]
return
37

Preorder traversal: recursive algorithm

Algorithm RPREORDER(T)
Input: A binary tree whose root node address is given by a pointer variable T
Output: Preorder traversal of a tree
[Process the root node]
if T != NULL then
write DATA(T)
else
return false {Empty Tree}
[Process the left subtree]
if LPTR(T) != NULL then
RPREORDER(LPTR(T))
[Process the right subtree]
if RPTR(T) != NULL then
RPREORDER(RPTR(T))
[Finished]
return

38

19
9/12/2017

Postorder traversal: iterative algorithm


Algorithm POSTORDER(T)
Input: A binary tree whose root node address is given by a pointer variable T
Output: Postorder traversal of a tree
[Initialize]
if T=NULL then
return false {Empty Tree}
else
TOP -1
PT
[Traverse in postorder]
Repeat while true
[Descend left]
Repeat while P != NULL
push(S,P)
P LPTR(P)
[Process a node whose left and right subtree have been traversed]
Repeat while S[TOP] <0
P POP(S)
write DATA(P)
if TOP = -1 then
return
[Branch right and then mark node from which we branched]
P RPTR(S[TOP])
S[TOP]  -S[TOP]
[Finished]
return 39

Postorder traversal: recursive algorithm


Algorithm RPOSTORDER(T)
Input: A binary tree whose root node address is given by a pointer variable T
Output: Postorder traversal of a tree
[Initialize]
if T=NULL then
return false {Empty Tree}
[Traverse in postorder the left subtree]
if LPTR(T) != NULL then
RPOSTORDER(LPTR(P))
[Traverse in postorder the left subtree]
if RPTR(T) != NULL then
RPOSTORDER(RPTR(P))
[Process the root node]
write DATA(P)
[Finished]
return

40

20
9/12/2017

Inorder traversal: iterative algorithm


Algorithm INORDER(T)
Input: A binary tree whose root node address is given by a pointer variable T
Output: Inorder traversal of a tree

41

Inorder traversal: iterative algorithm


Algorithm INORDER(T)
Input: A binary tree whose root node address is given by a pointer variable T
Output: Inorder traversal of a tree
[Initialize]
if T=NULL then
return false {Empty Tree}
else
TOP -1
PT
[Traverse in Inorder]
Repeat while true
[Descend left]
Repeat while P != NULL
push(S,P)
P LPTR(P)
[Process a node whose left subtree have been traversed]
write DATA(S[TOP])
P POP(S)
P RPTR(P)
if TOP = -1 then
return
[Finished]
return 42

21
9/12/2017

Inorder traversal: recursive algorithm

Algorithm RINORDER(T)
Input: A binary tree whose root node address is given by a pointer variable T
Output: Inorder traversal of a tree

43

22

S-ar putea să vă placă și