Professional Documents
Culture Documents
Outline
Binary trees Binary search trees AVL trees B trees Applications
9/2/2013
Trees
Tree ADT
A tree is a collection (may be empty) of nodes, containing:
a distinguished node called the root r, zero or more non-empty subtrees T1, T2, , Tk, A directed edge from the root r to the root of each subtree. root
T1
9/2/2013
T2
Tk
4
Terminologies
parent
root
children
subtrees
siblings
grandchildren
9/2/2013
Terminologies
path ancestor
length of path
depth length of path from the root descendant
9/2/2013
Terminologies
height
9/2/2013
Tree: Implementation
class TreeNode { Object element; TreeNode firstChild; TreeNode nextSibling; nextSibling=null }
nextSibling=null nextsibling firstChild=null firstChild=null nextSibling=null firstChild=null firstChild=null
9/2/2013 Data Structure: Trees 8
Binary Trees
Binary Trees
A tree in which no node can have more than 2 children.
9/2/2013
10
BinaryNode (Comparable theElement, BinaryNode lt, BinaryNode rt) { element = theElement; left = lt; right = rt; }
// Friendly data; accessible by other package routines Comparable element; // The data in the node BinaryNode left; // Left child BinaryNode right; // Right child }
9/2/2013 Data Structure: Trees 12
9/2/2013
Example
8 3
2 1 5
9/2/2013
11
4 6 7
15
12
}
} } } } }
16
FIND
Find 6
8 < 3
> 2 1 5 4 9 12
11
>
6 7
17
9/2/2013
Method find
private BinaryNode find ( Comparable x, BinaryNode t ) { if( t == null ) return null; if( x.compareTo( t.element ) < 0 ) return find( x, t.left ); else if( x.compareTo( t.element ) > 0 ) return find( x, t.right ); else return t; // Match }
9/2/2013
18
INSERT
Insert 10
8 3
10 >
11
< 4 6 5 7
19
2 1
12 10
9/2/2013
Method insert
private BinaryNode insert ( Comparable x, BinaryNode t ) { if( t == null ) t = new BinaryNode( x, null, null ); else if( x.compareTo( t.element ) < 0 ) t.left = insert( x, t.left ); else if( x.compareTo( t.element ) > 0 ) t.right = insert( x, t.right ); else ; // Duplicate; do nothing return t; }
9/2/2013 Data Structure: Trees 20
FindMax, FindMin
8 3
2 min 1 5
9/2/2013
11
4 6 7
21
12 max
REMOVE
Remove 7
11 3
2 1 4 7 12
13
14
5 6
9 10
Data Structure: Trees 23
9/2/2013
REMOVE
Remove 7
11 3
2 1 8 7 12
13
14
9 10
24
9/2/2013
REMOVE
Remove 7
11 3
2 1 4 7 12
13
14
5 6
Data Structure: Trees 25
9/2/2013
Method Remove
private BinaryNode remove(Comparable x,BinaryNode t) { if(t == null) return t; // Item not found;do nothing if( x.compareTo(t.element) < 0 ) t.left = remove(x, t.left); else if ( x.compareTo(t.element) > 0 ) t.right = remove(x, t.right); else if (t.left!=null && t.right!=null) // 2 child { t.element = findMin(t.right).element; t.right = remove(t.element, t.right); } else t = (t.left != null) ? t.left : t.right; return t; }
9/2/2013 Data Structure: Trees 26
AVL Trees
AVL Trees
An AVL tree is a binary search tree with a balance condition. Balance condition For every node in the tree, the height of the left & right subtrees can differ by at most 1.
9/2/2013
28
AVL Trees
11 3
2 1 5 7 8 12
11 13
14 1 4
Data Structure: Trees
3
2 7 12
13
14
5 6
AVL tree
9/2/2013
29
Xh+1
Yh
Zh
9/2/2013
30
k1
Yh
Zh+1
9/2/2013
31
k1
k2
Zh
Zh
Xh
9/2/2013
32
k3
Double Rotation
Zh
k
Xh
k3 k3
k2 Zh k1 k1 Xh k2 Yh
Yh+1 k1
Zh k3
Xh
k2
Xh Yh
Yh Yh
Yh
Zh
Yh
9/2/2013
33
Double Rotation
k3 k1 k2
Zh
Xh
k2
Xh
k1
Yh
k3
Yh
Zh
Yh
Yh
9/2/2013
34
If N is the number of nodes in an AVL tree, the height of the tree, h(N), is approximately 1.44 log(N+2)-.328.
9/2/2013 Data Structure: Trees 35
Class AvlNode
Class AvlNode { AvlNode (Comparable theElement) { this(theElement, null, null); } AvlNode ( Comparable theElement, AvlNode lt,rt ) { element = theElement; left=lt; right=rt; } private static int height (AvlNode t) { return t==null ? -1 : t.height; } ... Comparable element; AvlNode left; AvlNode right; int height; }
9/2/2013 Data Structure: Trees 36
Method insertion
private AvlNode insert(Comparable x,AvlNode t) { if (t == null) // insert in an empty tree t = new AvlNode( x, null, null ); // go down the left subtree else if (x.compareTo( t.element ) < 0) { t.left = insert( x, t.left ); // Does insertion violate balance condition? if(height(t.left)-height(t.right)==2) if(x.compareTo(t.left.element)<0) t = rotateWithLeftChild( t ); else t = doubleWithLeftChild( t ); }
9/2/2013 Data Structure: Trees 37
Method: insertion
// go down the right subtree else if(x.compareTo( t.element ) > 0) { t.right = insert( x, t.right ); // Does insertion violate balance condition? if(height(t.right)-height(t.left)==2) if(x.compareTo(t.right.element)>0) t = rotateWithRightChild( t ); else t = doubleWithRightChild( t ); } else ; // Duplicate; do nothing t.height=max(height(t.left),height(t.right))+1; return t; }
9/2/2013 Data Structure: Trees 38
Method rotateWithLeftChild
private static AvlNode rotateWithLeftChild(AvlNode k2) { AvlNode k1 = k2.left; k2.left = k1.right; k1.right = k2; k2.height = max(height(k2.left), height(k2.right))+1; k1.height = max(height(k1.left), k2.height)+1; return k1; } k2
k1 k1 k2
9/2/2013
39
Method rotateWithRightChild
private static AvlNode rotateWithRightChild( AvlNode k1 ) { AvlNode k2 = k1.right; k1.right = k2.left; k2.left = k1; k1.height = max(height(k1.left), height(k1.right))+1; k2.height = max(height(k2.left), k2.height)+1; return k1; } k1
k2 k2
k1
9/2/2013
40
Method doubleWithLeftChild
private static AvlNode doubleWithLeftChild (AvlNode k3) { k3.left = rotateWithRightChild(k3.left); return(rotateWithLeftChild(K3); }
k3 k1 k2 k1
k3 k2 k1
k2 k3
9/2/2013
41
Method doubleWithRightChild
private static AvlNode doubleWithRightChild (AvlNode k3) { k3.right = rotateWithLeftChild(k3.right); return(rotateWithRightChild(k3); }
k3 k1 k2
k3 k2 k1 k3
k2 k1
9/2/2013
42
Running Time
T(h)=O(h),
9/2/2013
46
T(0) = c h is the height of the tree. T(h) = T(h-1) + f(h) f(h) = O(h). T(h) = O(h2)
9/2/2013 Data Structure: Trees 47
Tree Traversal
Inorder Traversal
+
(a (b * (c / d))) + (e f)
a * -
e /
b c
9/2/2013 Data Structure: Trees
d
50
Method inorder
public static void inorder (BinaryNode t) { if ( t!=null ) { inorder(t.left); System.out.println(t.element); inorder(t.right); } }
9/2/2013
51
Preorder Traversal
+
+ a*b/cdef
a * -
e /
b c
9/2/2013 Data Structure: Trees
d
52
Method preorder
public static void preorder (BinaryNode t) { if ( t!=null ) { System.out.println(t.element); inorder(t.left); inorder(t.right); } }
9/2/2013
53
Postorder Traversal
+
abcd/*ef+
a * -
e /
b c
9/2/2013 Data Structure: Trees
d
54
Method postorder
public static void postorder (BinaryNode t) { if ( t!=null ) { inorder(t.left); inorder(t.right); System.out.println(t.element); } }
9/2/2013
55
B Trees
B trees
N-ary tree Increase the breadth of trees to decrease the height Used for indexing of large amount of data (stored in disk)
9/2/2013
57
Example
12
4 8 60 54 56 57 59 60 69 61 62 66 67 70 71 76 77 79 80 81 82 83 85 86 90 93 95 97 98 99
58
52
78
83 91
19 26 37 46 0 5 1 6 8 2 7 9 11 13 20 27 38 49 12 14 21 28 44 50 17 22 31 45 19 26 35
9/2/2013
Properties of B Trees
For an M-ary B tree: The root has up to M children. Non-leaf nodes store up to M-1 keys, and have between M/2 and M children, except the root. All data items are stored at leaves. All leaves have to same depth, and store between L/2 and L data items.
9/2/2013 Data Structure: Trees 59
Search
Search for 66
12
4 8 60 54 56 57 59 60 69 61 62 66 67 70 71 76 77 79 80 81 82 83 85 86 90 93 95 97 98 99
60
52
78
83 91
19 26 37 46 0 5 1 6 8 2 7 9 11 13 20 27 38 49 12 14 21 28 44 50 17 22 31 45 19 26 35
9/2/2013
Insert
Insert 55
12
4 8 60 54 56 57 59 60 69 61 62 66 67 70 71 76 77 79 80 81 82 83 85 86 90 93 95 97 98 99
61
52
78
83
Split leave
91
19 26 37 46 0 5 1 6 8 2 7 9 11 13 20 27 38 49 12 14 21 28 44 50 17 22 31 45 19 26 35
9/2/2013
Insert
Insert 32
12
4 8
Insert key 31
52 78
83
Split leave
91
19 26 37 46 0 5 1 6 8 2 7 9 11 13 20 27 38 49 12 14 21 28 44 50 17 22 31 45 19 26 35 36
9/2/2013
70 71 76 77
79 80 81 82 83
85 86 90
93 95 97 98 99
62
9/2/2013
63