Trees - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Trees

Description:

A binary search tree(BST) is a binary tree that may be empty. ... Why use a BST? We looked at hash tables as a representation of dictionary style data. ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 24
Provided by: anniegro
Category:
Tags: bst | trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
  • CS341
  • Western Washington University

2
Why Trees?
We need a representation for hierarchical
data. Like what? ancestor descendent superior
subordinate whole part modular
organization Definition A tree t is a finite
nonempty set of elements. One element is called
the root, and all the remaining elements are
partitioned into trees called subtrees.
3
Tree Terms
Elements are represented as nodesusually
drawn as a circle. Edges, lines, are drawn
connecting a node to its subtree. This edge
implies a relationship between nodes. A node can
be a parent node or a child node. Other terms
used to describe the relationship between nodes
are sibling, ancestor/descendent,
grandchild/grandparent A leaf is an element with
no children The term level is used to denote the
tier of an element within a tree. The root is at
level 1 its children are at level 2and so
on. The degree of an element is the number of
kids it has. The degree of a tree is the max of
its elements degrees.
4
There are many types of trees Binary
Trees Binary Search Trees AVL Trees Red-Black
Trees B-Trees
5
Binary Trees
A binary tree t is a finite, but possibly
empty, collection of elements. When the tree is
not empty, it contains a root, and all the
remaining elements are partitioned into sub
binary trees, which are called the left and right
subtrees of t. How does a binary tree differ
from a tree? We can use a binary tree to
represent arithmetic expressionsor propositional
calculus expressions.
6
Binary Tree Terms
A full binary tree is a binary tree in which all
the leaves are on the same level and every
nonleaf node has 2 children. It has height h and
contains exactly 2h - 1 elements. A complete
binary tree is either full or full through the
next-to-last level, with the leaves on the
last level as far to the left as possible. A
full binary tree is a special case of a
complete binary tree. A complete binary tree
that contains n elements has a height that is
ceiling(log2(n1)).
7
Properties of Binary Trees
Property 1 The drawing of every binary tree with
n elements, n gt 0, has exactly n 1
edges. Property 2 A binary tree of height h, h
gt0, has at most 2h 1 elements in it. The
height, depth, of a tree is the number of levels
in it. Property 3 The height of a binary tree
that contains n, n gt0 elements is at most n and
at least ceiling (log2(n1)).
8
Property 4 Let i, 1 lt i lt n, be the number
assigned to an element and its children in a
complete binary tree. The following statements
are true 1. If i 1, then this element is the
root of the binary tree. If i gt 1, then the
parent of this element has been assigned the
number floor(i/2). 2. If 2i gt n, then this
element has no left child. Otherwise, its left
child has been assigned the number 2i. 3. If 2i
1 gt n, then this element has no right
child. Otherwise, its right child has been
assigned the number 2i 1.
9
Binary Tree Operations
Create a tree Insert an element Remove an
element Determine height Determine number of
elements Copy Output a binary trees
contents Compare the contents of 2 binary
trees Destroy the tree Traversal Others?
10
Traversal
Preorder Node Left child Right child Inorder Left
child Node Right child Postorder Left
child Right child Node Level Order elements are
visited by level from top to bottom
11
What is the complexity of a binary tree
traversal? Does it matter which traversal is
executed? Does it depend on implementation? What
are possible implementations? SurpriseLinear
and Linked!
12
Binary Search Trees
Definition A binary search tree(BST) is a binary
tree that may be empty. A nonempty binary search
tree satisfies the following properties 1.
Every element has a key(or value) and no tow
elements have the same key therefore, all keys
are distinct. 2. The keys, if any, in the left
subtree of the root are smaller than the key in
the root. 3. The keys, if any, in the right
subtree of the root are larger than the key in
the root. 4. The left and right subtrees of the
root are also binary search trees.
13
Why use a BST?
  • We looked at hash tables as a representation of
    dictionary style data. And we observed that a
    hash table can provide O(1) performance for the
    dictionary operations of insertion, deletion, and
    searching.
  • But, that was in the best case. Recall that in
    the worst case these operations performed in
    O(n).
  • A limitation of the hash table representation is
    that it does not reflect an ordering of the keys.
    This prevents efficient implementations of the
    following operations
  • Output all keys in ascending order.
  • Find the kth element in ascending order.
  • Delete the kth element.

14
BST Performance
What is the performance of the following
operations using a BST representation? Searching?
Insertion? Deletion? Traversal? Now
determine the performance of these operations in
the worst case! Is there anything we can do to
avoid the worst case scenario?
15
Balanced Trees
Recall the best, average, and worst case
performance of searching a BST. O(1) O(logn) O(n
) Why would we ever have a linear order of
magnitude with a tree structure? Balancing the
tree can improve the worst case performance. An
AVL tree is a balanced treenamed
after Adelson-Velskii and Landis
16
AVL Trees
A Recursive Definition An empty binary tree is an
AVL tree. If T is a nonempty binary tree with TL
and TR as its left and right subtrees, then T is
an AVL tree iff 1) TL and TR are AVL trees 2)
hL - hR lt 1 where hL and hR represent
the heights of TL and TR respectively. An AVL
search tree is a binary search tree that is also
an AVL tree.
17
AVL Tree Properties
We can use AVL search trees to represent
a dictionary that performs each dictionary
operation in logarithmic time. We must establish
the following properties 1) The height of an
AVL tree with n elements is O(logn). 2) For
every value of n, n gt 0, there exists an AVL
tree. 3) An n-element AVL search tree can be
searched in O(height) O(logn). 4) A new
element can be inserted into an n-element AVL
search tree so that the result is an n1 element
AVL tree and such an insertion can be done in
O(logn) time. 5) An element can be deleted from
an n-element AVL search tree so that the result
is an n-1 element AVL tree and such a deletion
can be done in O(logn) time
18
Representation
We can inherit representation and
implementation from our BST data type. Butwe
need to represent the balance factor between the
left and right subtrees. Possible values include
-1, 0, and 1 How does the balance factor affect
binary tree operations? Searching? Traversal? Ins
erting? Deleting?
19
Insertion Imbalance
Observations about unbalanced trees
resulting from insertion 1. In the unbalanced
tree, the balance factors are limited to -2, -1,
0, 1, or 2 2. A node with balance factor 2 had
a balance factor 1 before the insertion. A node
with a balance factor of -2, had a balance
factor of -1 before the insertion. 3. The
balance factors of only those nodes on the path
from the root to the newly inserted node
can change as a result of the insertion. 4. Let
A denote the nearest ancestor of the
newly inserted node whose balance factor is
either -2 or 2. The balance factor of all nodes
on the path from A to the newly inserted node
was 0 prior to the insertion.
20
Classifying Imbalances
Let A denote the node with a balance factor of -2
or 2 LL The new node is in the left subtree of
the left subtree of A LR The new node is in the
right subtree of the left subtree of A RR The
new node is in the right subtree of the
right subtree of A RL The new node is in the
left subtree of the right subtree of A
21
Imbalances
LL
LR
A
A
A
A
RR
RL
22
LL and RR are considered single rotations LR and
RL are considered double rotations To transform
an LR imbalance, perform a RR rotation followed
by an LL rotation To transform an RL imbalance,
perform a LL rotation followed by an RR rotation.
23
Deletion Imbalances
Observations about unbalanced trees
resulting from deletion Let q denote the parent
of the node that was physically deleted. 1. If
the new balance factor of q is 0, its height has
decreased by 1, and we need to change the balance
factor of its parent(if any) and possibly those
of its other ancestors. 2. If the new balance
factor of q is either -1 or 1, its height is the
same as before the deletion and the balance
factors of its ancestors are unchanged. 3. If
the new balance factor of q is either -2 or 2,
the tree is unbalanced at q.
Write a Comment
User Comments (0)
About PowerShow.com