binary tree
TRANSCRIPT
1
Chapter 5: Binary Trees• Basic tree concepts
• Binary trees
• Binary search trees
• AVL trees
• Heaps
2
Basic Tree Concepts• Trees are used for:
– Representing algebraic formulas
– Searching large and dynamic lists
3
Basic Tree Concepts• A tree consists of:
– nodes: finite set of elements– branches: directed lines connecting the nodes
• For a node: – degree: number of branches associated with the node– indegree: number of branches towards the node– outdegree: number of branches away from the node
• For a tree: – root: node with indegree 0– nodes different from the root must have indegree 1
4
Terminology• Leaf: node with outdegree 0
• Internal node: not a root or a leaf
• Parent: node with outdegree greater than 0
• Child: node with indegree greater than 0
• Siblings: nodes with the same parent
• Path: sequence of adjacent nodes
5
Terminology• Ancestor: node in the path from the root to the
node
• Descendent: node in a path from the node to a leaf
• Level: the node's distance from the root (at level 0)
• Height (Depth): the level of the leaf in the longest path from the root plus 1
• Sub-tree: connected structure below the root
6
Tree Representation
ALU
Case CPU 3.5" Disk CD-ROM
Controller
Computer
ROM
General tree
7
Tree RepresentationIndented list
Computer
Case
CPU
Controller
ALU
ROM
...
3.5" Disk
CD-ROM
8
Tree RepresentationParenthetical listing
Computer (Case CPU (Controller ALU ROM ...) 3.5" Disk CD-ROM)
9
Binary Trees• A node cannot have more than two sub-
trees:
F
B
DC
E
A
left subtree
right subtree
10
Binary Tree Properties• Height of binary trees:
Hmax = N
Hmin = log2N + 1
11
Binary Tree Properties• Height of binary trees:
Hmax = N
Hmin = log2N + 1
Nmin = H
Nmax = 2H 1
12
Binary Tree Properties• Balance:
– Balance factor: B = HL HR
– Balanced tree: balance factor is 0, -1, or 1
sub-trees are balanced
13
Binary Tree Properties• Completeness:
– Complete tree:
Nmax = 2H 1
(last level is full)
– Nearly complete tree:
Hmin = log2N 1
nodes in the last level are on the left
A
B
D E
C
F G
A
B
D E
C
14
Expression Trees• Each leaf is an operand
• The root and internal nodes are operators
• Sub-trees are sub-expressions
15
Expression Trees
+
*
+a
b c
d
a * (b + c) + d
16
Binary Tree StructureNode
leftSubTree <nodePointer>
data <dataType>
rightSubTree <nodePointer>
End Node
17
Binary Tree Traversal• Each node is processed once and only
once in a predetermined sequence.
18
Depth-First Traversal
1
2 3
2
1 3
3
1 2
PreOrder
NLR
InOrderLNR
PostOrder
LRN
19
PreOrder Traversal
A
B
C D
E
F
A B C D E F
Processing order
20
PreOrder Traversal
A
B
C D
E
F
A B C D E F
Processing order
A
B
C D
E
F
Walking order
21
PreOrder Traversal
Algorithm preOrder (val root <nodePointer>)
Traverses a binary tree in node-left-right sequence
Pre root is the entry node of a tree/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)
4 return
End preOrder
22
InOrder Traversal
A
B
C D
E
F
C B D A E F
Processing order
23
InOrder Traversal
A
B
C D
E
F
Processing order
A
B
C D
E
F
Walking order C B D A E F
24
InOrder Traversal
Algorithm inOrder (val root <nodePointer>)
Traverses a binary tree in left-node-right sequence
Pre root is the entry node of a tree/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)
4 return
End inOrder
25
InOrder Traversal
+
*
+a
b c
da * b + c + d
26
InOrder Traversal
+
*
+a
b c
d((a * (b + c)) + d)
( )
( )
( )
27
PostOrder Traversal
A
B
C D
E
F
C D B F E A
Processing order
28
PostOrder Traversal
A
B
C D
E
F
Processing order
A
B
C D
E
F
Walking order C D B F E A
29
PostOrder Traversal
Algorithm postOrder (val root <nodePointer>)
Traverses a binary tree in left-node-right sequence
Pre root is the entry node of a tree/subtree
Post each node has been processed in order
1 if (root is not null)
1 postOrder (root leftSubTree)
2 postOrder (root rightSubTree)
3 process (root)
4 return
End preOrder
30
Breadth-First Traversal
A
B
C D
E
F
A
B E
C D F
Processing order
31
Breadth-First Traversal
A
B
C D
E
F
Walking order
A
B E
C D F
Processing order
A
B
C D
E
F
32
Breadth-First TraversalAlgorithm breadthFirst (val root <nodePointer>)
1 pointer = root
2 while (pointer not null)
1 process (pointer)
2 if (pointer left not null)
1 enqueue (pointer left)
3 if (pointer right not null)
1 enqueue (pointer right)
4 if (not emptyQueue)
1 dequeue (pointer)
5 else
1 pointer = null
3 return
End breadthFirst