Title: MIS 215 Module 5 Binary Trees
1MIS 215 Module 5 Binary Trees
2Where are we?
MIS215
Basic Algorithms
Introduction
List Structures
Advanced structures
Search Techniques
Intro to Java, Course
Sorting Techniques
Java lang. basics
Hashtables
Linked Lists
Binary Search
Graphs, Trees
Stacks, Queues
Arrays
Bubblesort
Fast Sorting algos (quicksort, mergesort)
Newbie
Programmers
Developers
Professionals
Designers
3Todays buzzwords
- Binary trees
- Linked structures where a node can be connected
to at most two other nodes - Binary search trees
- Trees designed for searching the left subtree
has items smaller than the node item, the right
subtree has bigger - Balanced binary search trees
- Binary trees that adjust the height to ensure
that the subtree heights are same - Key
- A component of an object that is typically used
for quick retrieval of the object
4Binary Trees A Definition
DEFINITION A binary tree is either empty, or it
consists of a node called the root together with
the two binary trees called the left subtree and
the right subtree of the root
Some examples of binary trees are given below
one empty binary tree, one binary tree with one
node, and two with two nodes. These are different
from each other.
and
and
5Binary Tree More Examples
We never draw any part of a binary tree to look
like
The binary trees with three nodes are
a
a
a
b
b
b
c
c
c
6Traversal of binary trees
- There are four standard traversal orders
- With preorder traversal we first visit a node,
then traverse its left subtree, and then traverse
its right subtree. - With inorder traversal we first traverse the left
subtree, then visit the node, and then traverse
the right subtree. - With postorder traversal, we first traverse the
left subtree, then the right subtree, and then
vist the node. - With level-order traversal, we traverse the tree
by levels the top level first, then the next
level, and so on, visiting the nodes
left-to-right.
7Traversal exercises
a
a
a
b
b
b
c
c
c
1
5
2
3
4
Inorder Preorder Postorder Level-order
8Expression Trees
log
!
x
b
a
n
ab
-
or
x
a
a
c
b
(altb)or(cltd)
a-(bc)
9A Linked Binary Tree
10Binary Search TreesA Definition
- DEFINITION A binary search tree is a binary
tree that is either empty or in which every node
contains a key and satisfies the conditions - The key in the left child of a node(if it exists)
is less than the key in its parent nodes. - The key in the right child of the node (if it
exists) is greater than the key in its parent
nodes. - The left and right sub-trees of the root are
again binary search trees.
11Binary Search TreesSome Comments
- We can regard binary search tree as a new ADT.
- We can regard binary search tree as a
specialization of binary trees. - We may study binary search trees as a new
implementation of the OrderedList.
12Binary Search Trees with same Keys
13Tree Insertion
14Deletion in a Binary Search Tree General Cases
Delete x
x
Case 1 deletion of a leaf x
15Deletion in a Binary Search Tree
Delete
r
x
x
b
y
b
y
a
z
a
z
r
Delete
z
c
z
y
b
y
x
a
x
c
b
a
16AVL Trees A Height Balance Technique
- Definition An AVL tree is a binary search tree
in which the heights of the left and right
subtrees of the root differ by at most 1 and in
which the left and right subtrees are again AVL
trees. - With each node of an AVL tree is associated a
balance factor that is left higher, equal or
right higher according, respectively, as the left
sub-tree has height greater than, equal to, or
less than that of the right sub-tree.
17Examples of AVL and Non-AVL Trees
/
-
/
\
-
/
-
-
-
-
\
/
-
-
-
-
-
-
AVL trees
-
-
-
-
-
Non-AVL trees
18Insertions into an AVL tree
19Insertions into an AVL treeChange the Order of
the List
Rotate left
k
k
m
k
m
m
u
u
Double rotation left
m
t
m
u
m
u
u
k
k
t
v
p
t
k
v
v
p
20Deletions in AVL TreesSample Cases (Part 1)
21Deletions in AVL TreesSample Cases (Part 2)