Chapter 5: Binary Trees: - Basic Tree Concepts - Binary Trees - Binary Search Trees - AVL Trees - Heaps
Chapter 5: Binary Trees: - Basic Tree Concepts - Binary Trees - Binary Search Trees - AVL Trees - Heaps
1
Basic Tree Concepts
• Trees are used for:
– Representing algebraic formulas
– Searching large and dynamic lists
2
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
3
1
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
4
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
5
Tree Representation
6
Tree Representation
Indented list
Computer
Case
CPU
Controller
ALU
ROM
...
3.5" Disk
CD-ROM
7
Tree Representation
Parenthetical listing
Computer (Case CPU (Controller ALU ROM ...) 3.5" Disk CD-
ROM)
8
Binary Trees
• A node cannot have more than two sub-
trees:
A
B E
C D F
left right
subtree subtree
9
Binary Tree Properties
• Height of binary trees:
Hmax = N
Hmin = log2N + 1
10
Binary Tree Properties
• Height of binary trees:
Hmax = N
Hmin = log2N + 1
Nmin = H
Nmax = 2H − 1
11
Binary Tree Properties
• Balance:
– Balance factor: B = HL − HR
– Balanced tree: balance factor is 0, -1, or 1
sub-trees are balanced
12
Binary Tree Properties
• Completeness:
– Complete tree: A
Nmax = 2H − 1 B C
13
Expression Trees
• Each leaf is an operand
• The root and internal nodes are
operators
• Sub-trees are sub-expressions
14
Expression Trees
* d
+
a
b c
a * (b + c) + d
15
Binary Tree Structure
Node
leftSubTree <nodePointer>
data <dataType>
rightSubTree <nodePointer>
End Node
16
Binary Tree Traversal
• Each node is processed once and only
once in a predetermined sequence.
17
Depth-First Traversal
1 2 3
2 3 1 3 1 2
18
PreOrder Traversal
B E
C D F
A B C D E F
Processing
order 19
PreOrder Traversal
A A
B E B E
C D F C D F
Walking
A B C D E F order
Processing
order 20
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
21
InOrder Traversal
B E
C D F
C B D A E F
Processing
order 22
InOrder Traversal
A A
B E B E
C D F C D F
Walking
C B D A E F order
Processing
order 23
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
24
InOrder Traversal
* d
a*b+c+d
a +
b c
25
InOrder Traversal
( + )
( * ) d
((a * (b + c)) + d)
( + )
a
b c
26
PostOrder Traversal
B E
C D F
C D B F E A
Processing
order 27
PostOrder Traversal
A A
B E B E
C D F C D F
Walking
C D B F E A order
Processing
order 28
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
29
Breadth-First Traversal
B E
C D F
A
B E
C D F
Processing
30
order
Breadth-First Traversal
A A
B E B E
C D F C D F
A Walking
B E order
C D F
Processing
31
order
Breadth-First Traversal
Algorithm 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
32
End breadthFirst