Lecture 3 Trees
Lecture 3 Trees
Definition
A tree is a finite nonempty set of elements.
It is an abstract model of a hierarchical structure.
consists of nodes with a parent-child relation.
Applications:
Organization charts
File systems
Computers”R”Us
Programming environments
Leaf nodes
Binary Search Trees
Key property
Value at node
Smaller values in left subtree
Larger values in right subtree
Example
X
X>Y
X<Z
Y Z
Binary Search
Binary search can perform operation find(k) on a dictionary
implemented by means of an array-based sequence, sorted by key
similar to the high-low game
at each step, the number of candidate items is halved
terminates after O(log n) steps
Example: find(7)
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
lm h
Binary Search Trees 6
Binary Search Trees
Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10
25
Binary Not a binary
search trees search tree
Binary Tree Implementation
class BinaryNode
{
// Friendly data; accessible by other package
routines
Object element; // The data in the node
BinaryNode left; // Left child
BinaryNode right; // Right child
}
Iterative Search of Binary
Tree
Node *Find( Node *n, int key) {
while (n != NULL) {
if (n->data == key) // Found it
return n;
if (n->data > key) // In left subtree
n = n->left;
else // In right subtree
n = n->right;
}
return null;
}
Node * n = Find( root, 5);
Recursive Search of Binary
Tree
Node *Find( Node *n, int key) {
if (n == NULL) // Not found
return( n );
else if (n->data == key) // Found it
return( n );
else if (n->data > key) // In left subtree
return Find( n->left, key );
else // In right subtree
return Find( n->right, key );
}
Node * n = Find( root, 5);
Example Binary Searches
Find ( root, 2 )
root
10 5
10 > 2, left 5 > 2, left
5 30 5 > 2, left 2 45 2 = 2, found
2 = 2, found
2 25 45 30
10
25
Example Binary Searches
Find (root, 25 )
10 5
10 < 25, right 5 < 25, right
5 30 30 > 25, left 2 45 45 > 25, left
25 = 25, found 30 > 25, left
2 25 45 30
10 < 25, right
10 25 = 25, found
25
Types of Binary Trees
Degenerate – only one child
Complete – always two children
Balanced – “mostly” two children
more formal definitions exist, above are intuitive ideas
Degenerate Balanced
binary tree binary tree
Binary Search Properties
Time of search
Proportional to height of tree
Balanced binary tree
O( log(n) ) time
Degenerate tree
O( n ) time
Like searching linked list / unsorted array
Binary Search Tree
Construction
How to build & maintain binary trees?
Insertion
Deletion
Maintain key property (invariant)
Smaller values in left subtree
Larger values in right subtree
Binary Search Tree –
Insertion
Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left subtree for Y
4. If X > Y, insert new leaf X as new right subtree for
Y
Observations
O( log(n) ) operation for balanced tree
Insertions may unbalance tree
Example Insertion
Insert ( 20 )
10 10 < 20, right
30 > 20, left
5 30
25 > 20, left
2 25 45 Insert 20 on left
20
Binary Search Tree –
Deletion
Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node
a) Replace with largest value Y on left subtree
OR smallest value Z on right subtree
b) Delete replacement value (Y or Z) from subtree
Observation
O( log(n) ) operation for balanced tree
Deletions may unbalance tree
Example Deletion (Leaf)
Delete ( 25 )
10 10
10 < 25, right
5 30 30 > 25, left 5 30
25 = 25, delete
2 25 45 2 45
Example Deletion (Internal
Node)
Delete ( 10 )
10 5 5
5 30 5 30 2 30
2 25 45 2 25 45 2 25 45
5 30 5 30 5 30
2 25 45 2 25 45 2 45
1 4 7 9
3 5
Preorder Traversal
A traversal visits the nodes of a tree Algorithm preOrder(v)
in a systematic manner
visit(v)
In a preorder traversal, a node is
for each child w of
visited before its descendants
v
Application: print a structured
preorder (w)
document
1
Become Rich
2 5 9
1. Motivations 2. Methods 3. Success Stories
3 4 6 7 8
1.1 1.2 Help
Enjoy 2.1 Get a 2.2 Start a 2.3 Acquired
Poor
Life CS PhD Web Site by Google
Friends
Postorder Traversal
8
3 7
todo.txt
homeworks/ programs/
1K
1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K
Continued
6
v
3 8
z
4
AVL Trees 35
Self adjusting Trees
Ordinary binary search trees have no balance
conditions
what you get from insertion order is it
Balanced trees like AVL trees enforce a balance
condition when nodes change
tree is always balanced after an insert or delete
Self-adjusting trees get reorganized over time as
nodes are accessed
Tree adjusts after insert, delete, or find
36
AVL Tree Definition
AVL Trees 37
Insertion in an AVL Tree
Insertion is as in a binary search tree
Always done by expanding an external node.
Example: 44 44
c=z
17 78 17 78
a=y
32 50 88 32 50 88
48 62 48 62
b=x
54
w
AVL Trees 38
Trinode Restructuring
let (a,b,c) be an inorder listing of x, y, z
perform the rotations needed to make b the topmost node of the
three
(other two cases
a=z case 2: double rotation
are symmetrical)
a=z (a right rotation about c,
c=y then a left rotation about a)
b=y T0
T0 b=x
c=x
T3
b=y b=x
T1
T1 T2
T2 T3 a=z c=x a=z c=y
AVL Trees 39
Insertion Example,
continued 44
5
2
z 64
17 78 7
3
2y 1
1
32 1 50 4 88
1
2 x
48
1
3 62
5
54 T3
unbalanced... T2
T0
4
T1 44 4
2 3 x
17
2 y 62
z6
1 2 2
32
1
1 50 3 5
78 7
1 1
...balanced 48 54 88
T2
T0 T1 T3
AVL Trees 40
Restructuring
(as Single Rotations)
Single Rotations:
T0 T3
T1 T3 T0 T1 T2
T2
T3 T3
T0 T2 T2 T1 T0
T1
AVL Trees 41
Restructuring
(as Double Rotations)
double rotations:
T0 T2
T2 T3 T0 T1 T3
T1
T0 T2
T3 T2 T3 T1 T0
T1
AVL Trees 42
Removal in an AVL Tree
Removal begins as in a binary search tree, which means the node
removed will become an empty external node. Its parent, w, may
cause an imbalance.
Example:
44 44
17 62 17 62
32 50 78 50 78
48 54 88 48 54 88
AVL Trees 43
Rebalancing after a
Removal
Let z be the first unbalanced node encountered while travelling
up the tree from w. Also, let y be the child of z with the larger
height, and let x be the child of y with the larger height.
We perform restructure(x) to restore balance at z.
As this restructuring may upset the balance of another node
higher in the tree, we must continue checking for balance until
the root of T is reached
62
a=z 44
44 78
w 17 62 b=y
17 50 88
50 78 c=x
48 54
48 54 88
AVL Trees 44
Running Times for
AVL
Trees
a single restructure is O(1)
using a linked-structure binary tree
find is O(log n)
height of tree is O(log n), no restructures needed
insert is O(log n)
initial find is O(log n)
Restructuring up the tree, maintaining heights is O(log n)
remove is O(log n)
initial find is O(log n)
Restructuring up the tree, maintaining heights is O(log n)
AVL Trees 45
AVL Trees 46
AVL Trees 47
AVL Trees 48
AVL Trees 49
AVL Trees 50
AVL Trees 51
AVL Trees 52
AVL Trees 53
AVL Trees 54
Problems
AVL Trees 55
Balanced Search Trees
Kinds of balanced binary search trees
height balanced vs. weight balanced
“Tree rotations” used to maintain balance on insert/delete
Non-binary search trees
2/3 trees
each internal node has 2 or 3 children
all leaves at same depth (height balanced)
B-trees
Generalization of 2/3 trees
Each internal node has between k/2 and k children
Each node has an array of pointers to children
Widely used in databases
Other (Non-Search) Trees
Parse trees
Convert from textual representation to tree
representation
Textual program to tree
Used extensively in compilers
Tree representation of data
E.g. HTML data can be represented as a tree
called DOM (Document Object Model) tree
XML
Like HTML, but used to represent data
Tree structured
Parse Trees
Expressions, programs, etc can be
represented by tree structures
E.g. Arithmetic Expression Tree
A-(C/5 * 2) + (D*5 % 4)
+
- %
A * * 4
/ 2 D 5
C 5
XML
Data Representation
E.g.
<dependency>
<object>sample1.o</object>
<depends>sample1.cpp</depends>
<depends>sample1.h</depends>
<rule>g++ -c sample1.cpp</rule>
</dependency>
Tree representation
dependency