Chapter 06 - Binary Trees - Day 1
Chapter 06 - Binary Trees - Day 1
Chapter 6
Objectives
• Trees, Binary Trees, and Binary Search Trees
• Tree Traversal
• Implementing Binary Trees
• Binary Search Tree
• Insertion
• Deletion
• Balancing a Tree
– The DSW Algorithm (*)
– AVL Trees
• Heaps
• Polish Notation and Expression Trees (*)
• Reading at home
DAY 1
TREES
DEFINITIONS
What is a tree?
• A tree is an abstract model of a hierarchical
structure
• A tree consists of nodes with parent-child relation
• Inspiration: family trees
• Applications:
– Organization charts
– File systems
– Programming environments
• Every node except one (a root) has a unique parent
Exact (Formal) definition
• Empty structure is an empty tree
• Non-empty tree consists of a root and its children, where these
children are also trees
Given a tree
Tree Terminologies
• Root: unique node without a parent
• Internal node: node with at least one child (A, B, C, F)
• Leaf: node without children (E, I, J, K, G, H, D)
• Ancestors: parent, grandparent, great-grand-parent, …
• Descendants: child, grandchild, great-grandchild, etc.
• Level: a root is at level 1 (sometimes 0).
– A father is level i then its children are at level i+1
• Height: maximum level in a tree
– Empty tree is a legitimate tree of height 0 (by definition)
– A single node is a tree of height 1
• Degree (order): number of its children
• Each node has to be reachable from the root through a unique sequence of
arcs, called path.
• The number of arcs in a path is called the length of the path.
Tree examples
Tree examples
Linked List to Tree Conversion
• Linked List can be converted into orderly tree
– All elements are stored according to some order
– However, from searching point of view, it is not better than a linked list (if
it doesn’t consist some additional constraints regarding arrangement of
nodes)
Definition, Types, Traversal
BINARY TREE
Binary Trees
• Binary tree: each node has at most two children
– Empty tree is a binary tree
– Each child may be empty or designated as either left
child or right child
Types of Binary Trees
• Full binary tree (sometimes proper binary tree or 2-
tree): every node other than the leaves has two children
• Complete binary tree: all non-terminal nodes have both
children, and all leaves are at the same level
Tree Traversal
• Tree traversal:
– Process of visiting each node in the tree exactly one
time
• Breadth-first traversal:
– Visiting each node starting from lowest (or highest)
level and moving down (or up) level by level
– Visiting nodes on each level from left to right (or from
right to left)
Breadth-first Tree traversal example
Bread-first traversal:
A, B, C, D, E, F, G, H, I
Tutorial: visit first or recursive call first
Tree traversal
• Depth-first traversal:
– Proceeds as far as possible to the left (or right)
– Then backs up until the first crossroad
– Goes one step to the right (or left)
– Again, as far as possible to the left (or right)
• There are three tasks to be done:
– V: Visiting a node
– L: Traversing the left subtree
– R: Traversing the right subtree
Ways of depth-first traversal
• Three tasks can be ordered in 3!=6 ways
• There are 6 possible ordered depth-first traversals
• It can be reduced to 3 traversals
– The move is always from left to right and
– Attention is focused on the first column.
• The 3 traversals are given these standard names
– VLR: Preorder traversal (or NLR)
– LVR: Inorder traversal (or LNR)
– LRV: Postorder (or LRN)
Inorder tree traversal
Tree Traversal Example
Output:
Breadth-first Algorithm
1. Put the root node into the queue
2. Repeat following steps
(till no more node in the queue to process)
1. Dequeue the node from the queue
2. Put its children (if there’s) into the queue
3. Display value of the node
Output:
Breadth-first Algorithm
1. Put the root node into the queue
2. Repeat following steps
(till no more node in the queue to process)
1. Dequeue the node from the queue
2. Put its children (if there’s) into the queue
3. Display value of the node
B G
Output:
F
F
Breadth-first Algorithm
1. Put the root node into the queue
2. Repeat following steps
(till no more node in the queue to process)
1. Dequeue the node from the queue
2. Put its children (if there’s) into the queue
3. Display value of the node
B G
Output:
F
Breadth-first Algorithm
1. Put the root node into the queue
2. Repeat following steps
(till no more node in the queue to process)
1. Dequeue the node from the queue
2. Put its children (if there’s) into the queue
3. Display value of the node
G A D
Output:
F
B
Breadth-first Algorithm
1. Put the root node into the queue
2. Repeat following steps
(till no more node in the queue to process)
1. Dequeue the node from the queue
2. Put its children (if there’s) into the queue
3. Display value of the node
G A D
Output:
FB
B
So on and so forth
Breadth-first traversal
Depth first
• There are three cases:
– Pre-order: VLR (Visit – Left – Right), means at every node try
to visit first (display the node first) then go its left subtree
then its right subtree
– In-order: LVR (Left – Visit – Right), means at every node try
to go to the left subtree first, if at a node cannot go any
further left then visit (display the node), then try to go to
the right subtree.
– Post-order: LRV (Left – Right – Visit), means at every node
try to go all the way to left first, when at a node, cannot go
left any further, then try to go right, if at a node cannot go
left or right any further, then display the node.
Depth first
Test program
Setting up the tree
Traverse the tree
Output
BINARY SEARCH TREES
Binary Search Tree Definition
• Binary Search Tree is a node based tree data structure
which has following properties
– Left subtree of a node contains only nodes with keys less than
the node’s key
– Right subtree of a node contains only nodes with keys greater
than the node’s key
– Both the left and right subtrees must also be binary search
trees
• From above definition, it’s naturally follows that
– Each node has a distinct key (no equal)
– An inorder traversal of a binary search tree visits the keys in
increasing order
Binary search tree example
IMPLEMENTING BINARY SEARCH
TREE
Insertion on Binary Search Tree
4 20
15
null 15 15
19
17
15
15
15 4 20
4 20
4 20 17
17
19
Insertion on a Binary Search Tree
Searching on Binary Search Trees
FINISHED DAY 1
TREES