Beruflich Dokumente
Kultur Dokumente
Overview
Applications
DS & A
Trees
Slide # 2
Trees
Generic
Tree
G is parent of N
and child of A
M is child of F and
grandchild of A
A is an ancestor of P
P is a descendant of A
DS & A
Trees
Slide # 3
Definitions
DS & A
Trees
Slide # 4
Definitions
DS & A
Trees
Slide # 5
Definitions
DS & A
Trees
Slide # 6
B, C, D, E, F, G
are siblings
K, L, M
are siblings
B, C, H, I, P, Q, K, L, M, N
are leaves
DS & A
Trees
Slide # 7
DS & A
Trees
Slide # 8
Trees
Slide # 9
Implementation of Trees
DS & A
Trees
Slide # 10
Implementation of Trees
Struct TreeNode
{
Object element;
TreeNode firstChild;
TreeNode nextSibling;
}
DS & A
Trees
Slide # 11
DS & A
struct BinaryTreeNode
{
Object element;
BinaryTreeNode leftChild;
BinaryTreeNode rightChild;
}
Trees
Slide # 12
DS & A
Trees
Slide # 13
E.g., (a + b * c) + ((d * e + f) * g)
DS & A
Trees
Slide # 14
Evaluate expression
Recursively evaluate left and right subtrees
Apply operator at root node to results from subtrees
Traversals (recursive definitions)
Post-order: left, right, root
Pre-order: root, left, right
In-order: left, root, right
DS & A
Trees
Slide # 15
DS & A
Trees
Slide # 16
Traversals
Pre-order: + + a * b c * + * d e f g
Post-order: a b c * + d e * f + g * +
In-order: a + b * c + d * e + f * g
DS & A
Trees
Slide # 17
DS & A
Trees
Slide # 18
E.g., a b + c d e + * *
stack
top
(1)
(2)
(3)
DS & A
(4)
+
a
top
top
Trees
top
+
b
Slide # 19
E.g., a b + c d e + * *
top
(5)
(6)
+
a
top
c
d
+
a
+
d
DS & A
Trees
Slide # 20
DS & A
Trees
Slide # 21
14
Empty
A key-value pair and two binary trees
72
33
25
53
DS & A
64
43
84
99
Symmetric order:
97
Trees
smaller
than x
larger
than x
Slide # 22
E
C
N
N
A
E
S
E
C
R
DS & A
H
I
H
G
S
C
N
Trees
E
X
P
S
H
G
E
X
E
C
A
A
A S E R C H I N G X M P L
N
N
L
Slide # 23
To delete a node:
Case 1 (zero children): just remove it.
Case 2 (one child): pass the child up.
Case 3 (two children): find the next largest node using right-left*
or left-right*, swap with next largest, remove as in Case 1 or 2.
A
S
X
E
C
H
G
E
C
H
G
DS & A
E
C
N
M
S
R
H
G
N
P
M
L
Trees
N
P
Slide # 24
Tree Shape
Tree shape
51
14
14
72
33
25
53
43
72
43
97
64
84
53
33
99
51
DS & A
Trees
97
84
99
64
Slide # 25
Searching in BSTs
Contains (T, x)
{
if (T == NULL)
then return NULL
if (T.element == x)
then return T
if (x < T.element)
then return Contains (T.leftChild, x)
else return Contains (T.rightChild, x)
}
Typically assume no duplicate elements.
If duplicates, then store counts in nodes,
or each node has a list of objects.
DS & A
Trees
Slide # 26
Searching in BSTs
h = O(N)
1
4
2
2
1
4
6
8
DS & A
Trees
Slide # 27
Searching in BSTs
Complexity ?
DS & A
O(h)
Trees
Slide # 28
Searching in BSTs
Complexity ?
DS & A
O(h)
Trees
Slide # 29
Printing BSTs
PrintTree (T)
{
if (T == NULL)
then return
PrintTree (T.leftChild)
cout << T.element
PrintTree (T.rightChild)
}
Complexity?
DS & A
1 2 3 4 6 8
(n)
Trees
Slide # 30
E.g., insert 5
Old tree:
(1)
New tree:
insert(5)
(2)
(3)
(4)
DS & A
Trees
Slide # 31
DS & A
Trees
Slide # 32
(2)
remove(4):
(1)
(1)
(2)
(3)
(3)
DS & A
Trees
Slide # 33
E.g., remove(2):
(1)
(2)
Old tree:
Becomes
case 1 here
New tree:
DS & A
Trees
Slide # 34
Trees
Slide # 35
DS & A
Trees
Slide # 36
Trees
Slide # 37
Implementation of BST
Public member
functions calling
private recursive
member functions.
DS & A
Trees
Slide # 38
Implementation of BST
notice order
of tests!
Why?
DS & A
Trees
Slide # 39
recursive
version
non-recursive
version
DS & A
Trees
Slide # 40
DS & A
Trees
Slide # 41
Case 2:
Copy successor data
Delete successor
Algorithms
Trees
Slide # 42
Implementation of BST
Post-order traversal
DS & A
Trees
Slide # 43
DS & A
Trees
Slide # 44
BST Analysis
printTree
(N)
insert, remove, contains, findMin, findMax
O(h), where h = height of tree
Worst case: h = ?
Best case: h = ?
Average case: h = ?
DS & A
(N)
( lg N)
( lg N)
Trees
Slide # 45
DS & A
Trees
Slide # 46
DS & A
Trees
Slide # 47
DS & A
Trees
Slide # 48
A complete binary tree has the shortest overall path length for any
binary tree.
The longest path in a complete binary tree with n elements is
guaranteed to be no longer than ceiling(log n).
If we can keep our tree complete, were set for fast search times.
Very costly to maintain a complete binary tree.
E.g. insert 1 to the tree on the left causes every element to be
moved ... i.e. this operation is O(n)
5
3
2
7
4
6
3
DS & A
Trees
Slide # 49
DS & A
Trees
Slide # 50
AVL Trees
Which of these is a valid AVL tree?
Note: height violation not
allowed at ANY node
x
h=0
h=1
h=2
h=2
DS & A
Trees
Slide # 51
AVL Trees
DS & A
Trees
Slide # 52
DS & A
Trees
Slide # 53
AVL Insert
balanced
Rotating 7-8
restores balance
Inserting 6 violates
AVL balance condition
DS & A
Trees
Slide # 54
AVL Insert
Fix at the
violated node
root
inserted
node
DS & A
Trees
Slide # 55
Insert
DS & A
CASE 2
left
subtree
right
subtree
left
subtree
CASE 1
CASE 3
Insert
Insert
Trees
right
subtree
right child
left child
CASE 4
Insert
Slide # 56
DS & A
Trees
Slide # 57
CASE 1
Insert
DS & A
Trees
Slide # 58
Imbalance
After:
Balanced
h-1
h
h+1
inserted
Invariant:
DS & A
Remember:
X, Y, Z could be empty trees,
or single node trees,
or mulltiple node trees.
Trees
Slide # 59
Case 1 example
Imbalance
Before:
After:
Balanced
inserted
DS & A
Trees
Slide # 60
AVL Insert
DS & A
Trees
Slide # 61
CASE 4
Insert
DS & A
Trees
Slide # 62
Imbalance
After:
Balanced
Invariant:
DS & A
Trees
Slide # 63
Case 4 example
will this be true always?
Imbalance
4
2
Imbalance
Fix this
node
balanced
7
inserted
DS & A
Trees
Slide # 64
CASE 2
Insert
DS & A
Trees
Slide # 65
AVL Insert
Imbalance
After:
Imbalance
remains!
inserted
Think of Y as =
Note: X, Z can be empty trees, or single node trees, or mulltiple node trees
But Y should have at least one or more nodes in it because of insertion.
DS & A
Trees
Slide # 66
AVL Insert
After:
Imbalance
Balanced!
#2
#1
=Z
=X
=Y
inserted
Can be implemented as two
successive single rotations
DS & A
Trees
Slide # 67
Case 2 example
Balanced!
Imbalance
5
5
2
#1
6
2
#2
3
6
5
4
1
4
inserted
DS & A
Slide # 68
CASE 3
Insert
DS & A
Trees
Slide # 69
AVL Insert
imbalance
#2
#1
inserted
Trees
Slide # 70
Delete(2): ?
10
7
5
2
15
19
13
11
14
16
DS & A
Trees
17
25
18
Slide # 71
DS & A
Trees
Slide # 72
DS & A
Trees
Slide # 73
Locate insertion
site relative to
the imbalanced
node (if any)
Q) Is it guaranteed
that the deepest
node with imbalance
is the one that gets
fixed?
A) Yes, recursion
will ensure that.
Insert first,
and then fix
Case 1
Case 2
Case 4
Case 3
Rotation
Order is important
Algorithms
Slide # 75
// #1
// #2
Algorithms
Slide # 76
AvlTree class
CONSTRUCTION: with no initializer
**** Public Operations
void insert( x )
--> Insert x
void remove( x )
--> Remove x (unimplemented)
boolean contains( x ) --> Return true if x is present
Comparable findMin( ) --> Return smallest item
Comparable findMax( ) --> Return largest item
boolean isEmpty( )
--> Return true if empty; else false
void makeEmpty( )
--> Remove all items
void printTree( )
--> Print tree in sorted order
*** Errors
Throws UnderflowException as appropriate
/**
* Implements an AVL tree.
* Note that all "matching" is based on the compareTo method.
*/
DS & A
Trees
Slide # 77
root;
/**
* Construct the tree.
*/
public AvlTree( )
{
root = null;
}
DS & A
Trees
Slide # 78
//
//
//
//
// Constructors
AvlNode( AnyType theElement )
{
this( theElement, null, null );
}
AvlNode( AnyType theElement,
AvlNode<AnyType> lt,
AvlNode<AnyType> rt )
{
element = theElement;
left
= lt;
right
= rt;
height
= 0;
}
}
DS & A
Trees
Slide # 79
/**
* Make the tree logically empty.
*/
public void makeEmpty( )
{
root = null;
}
DS & A
Trees
Slide # 80
Trees
Slide # 81
Trees
Slide # 82
Trees
Slide # 83
/**
* Find an item in the tree.
* @param x the item to search for.
* @return true if x is found.
*/
public boolean contains( AnyType x )
{
return contains( x, root );
}
DS & A
Trees
Slide # 84
t )
// No match
}
DS & A
Trees
Slide # 85
/**
* Insert into the tree; duplicates are ignored.
* @param x the item to insert.
*/
public void insert( AnyType x )
{
root = insert( x, root );
}
DS & A
Trees
Slide # 86
Trees
Insert first,
and then fix
Case 1
Case 2
Slide # 87
Insert first,
and then fix
Case 4
Case 3
// Duplicate ... do nothing
update height
after rotation
DS & A
WHY?
Slide # 88
/**
* Rotate binary tree node with left child.
* For AVL trees, this is a single rotation for case 1.
* Update heights, then return new root.
*/
private AvlNode<AnyType> rotateWithLeftChild( AvlNode<AnyType> k2 )
{
Rotation
AvlNode<AnyType> k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = Math.max( height(k2.left), height(k2.right) ) + 1;
k1.height = Math.max( height( k1.left ), k2.height ) + 1;
return k1;
Order is important
}
DS & A
Trees
Slide # 89
DS & A
Trees
Slide # 90
/**
* Double rotate binary tree node: first left child
* with its right child; then node k3 with new left child.
* For AVL trees, this is a double rotation for case 2.
* Update heights, then return new root.
*/
private AvlNode<AnyType> doubleWithLeftChild( AvlNode<AnyType> k3 )
{
k3.left = rotateWithRightChild( k3.left );
return rotateWithLeftChild( k3 );
}
DS & A
Trees
Slide # 91
DS & A
Trees
k1)
Slide # 92
Trees
Slide # 93
DS & A
Trees
Slide # 94