MIS 215 Module 5 Binary Trees - PowerPoint PPT Presentation

About This Presentation
Title:

MIS 215 Module 5 Binary Trees

Description:

Designers. MIS215. Binary Search. Search. Techniques. Sorting Techniques. Bubblesort. Basic Algorithms ... Trees designed for searching the left subtree has ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 22
Provided by: WrightStat2
Category:

less

Transcript and Presenter's Notes

Title: MIS 215 Module 5 Binary Trees


1
MIS 215 Module 5 Binary Trees
2
Where 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
3
Todays 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

4
Binary 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
5
Binary 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
6
Traversal 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.

7
Traversal exercises
a
a
a
b
b
b
c
c
c
1
5
2
3
4
Inorder Preorder Postorder Level-order
8
Expression Trees

log
!
x
b
a
n
ab
-
or
x
a
a
c
b
(altb)or(cltd)
a-(bc)
9
A Linked Binary Tree
10
Binary 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.

11
Binary 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.

12
Binary Search Trees with same Keys
13
Tree Insertion
14
Deletion in a Binary Search Tree General Cases
Delete x
x
Case 1 deletion of a leaf x
15
Deletion 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
16
AVL 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.

17
Examples of AVL and Non-AVL Trees
/
-
/
\
-
/
-
-
-
-
\
/
-
-
-
-
-
-
AVL trees
-
-
-
-
-
Non-AVL trees
18
Insertions into an AVL tree
19
Insertions 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
20
Deletions in AVL TreesSample Cases (Part 1)
21
Deletions in AVL TreesSample Cases (Part 2)
Write a Comment
User Comments (0)
About PowerShow.com