0% found this document useful (0 votes)
62 views49 pages

Chapter 06 - Binary Trees - Day 1

Binary Trees can be implemented using arrays or linked structures. They can be traversed using breadth-first or depth-first approaches. Breadth-first traversal involves dequeuing nodes from a queue and adding their children to the back of the queue. Depth-first traversal has three variants: pre-order visits the root node first, then traverses left and right subtrees; in-order visits left subtree first, then root node, then right subtree; post-order visits left and right subtrees first, then the root node. Binary search trees require that all left descendants of a node are less than or equal to the node and all right descendants are greater than or equal; they support efficient insertion and searching operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views49 pages

Chapter 06 - Binary Trees - Day 1

Binary Trees can be implemented using arrays or linked structures. They can be traversed using breadth-first or depth-first approaches. Breadth-first traversal involves dequeuing nodes from a queue and adding their children to the back of the queue. Depth-first traversal has three variants: pre-order visits the root node first, then traverses left and right subtrees; in-order visits left subtree first, then root node, then right subtree; post-order visits left and right subtrees first, then the root node. Binary search trees require that all left descendants of a node are less than or equal to the node and all right descendants are greater than or equal; they support efficient insertion and searching operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Binary Trees

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

• Preorder traversal: F, B, A, D, C, E, G, I, H (root, left, right)


• Inorder traversal: A, B, C, D, E, F, G, H, I (left, root, right)
• Postorder traversal: A, C, E, D, B, H, I, G, F (left, right, root)
• Level-order traversal (breadth-first): F, B, G, A, D, I, C, E, H
BINARY TREE IMPLEMENTATION
Array - Binary tree implementation
• Binary trees can be implemented at least in two ways
– As arrays
– As linked structures
• To implement tree as an array
– A node is declared as an object with
• Information field and
• Two reference fields
– These reference fields contain the indexes of the array cells in
which the left and the right children are stored, if any.
– It is hard to predict how many nodes will be created during
program execution
• How to reserve spaces and know the size to define an array?
Linked - Binary tree implementation
• Linked structure implementation
– A node is declared as an object with
• Information field and
• Two reference fields
Demo – Build Queue First – Queue Node
MyQueue
MyQueue
Tree Implementation
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

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy