You are on page 1of 63

Trees

Outline
Binary trees Binary search trees AVL trees B trees Applications

9/2/2013

Data Structure: Trees

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

Data Structure: Trees

Terminologies
parent

root

children
subtrees

siblings

grandchildren

9/2/2013

Data Structure: Trees

Terminologies
path ancestor

length of path
depth length of path from the root descendant

9/2/2013

Data Structure: Trees

Terminologies

height

9/2/2013

Data Structure: Trees

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

Data Structure: Trees

10

Binary Tree: Implementation


Class BinaryNode { Object element; BinaryNode left; BinaryNode right; } 5

node.element=3 node.left=node node.right=node

node.element=9 node.element=5 node.left=null node.left=node node.right=null node.right=null


9/2/2013 Data Structure: Trees 11

Binary Tree: Implementation


class BinaryNode { // Constructors BinaryNode ( Comparable theElement ) { this ( theElement, null, null); }

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

Binary Search Trees

Binary Search Trees


Properties of a binary search tree T 1. T is a binary tree. 2. For each node n in T, whose left subtree is Tl and right subtree is Tr,
the item in each node in Tl is smaller than the item in n.

the item in each node in Tr is larger than the item in n.


Data Structure: Trees 14

9/2/2013

Example
8 3
2 1 5
9/2/2013

11
4 6 7
15

12

Data Structure: Trees

Binary Search Trees


public class BinarySearchTree { public BinarySearchTree( ) { root = null; public void insert( Comparable x ) { root = insert( x, root ); public void remove( Comparable x ) { root = remove( x, root ); public Comparable find( Comparable x ) { return elementAt( find( x, root ) ); public void makeEmpty( ) { root = null; ... private BinaryNode root;
9/2/2013 Data Structure: Trees

}
} } } } }
16

FIND
Find 6

8 < 3
> 2 1 5 4 9 12

11

>
6 7
17

9/2/2013

Data Structure: Trees

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

Data Structure: Trees

18

INSERT
Insert 10

8 3

10 >

11
< 4 6 5 7
19

2 1

12 10

9/2/2013

Data Structure: Trees

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

Data Structure: Trees

Methods findMin & findMax


private BinaryNode findMin( BinaryNode t ) { if( t == null ) return null; else if( t.left == null ) return t; return findMin( t.left ); } private BinaryNode findMax( BinaryNode t ) { if( t != null ) while( t.right != null ) t = t.right; return t; }
9/2/2013 Data Structure: Trees 22

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

Data Structure: Trees

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

Data Structure: Trees

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

not AVL tree

AVL tree
9/2/2013

29

Single Right Rotation


k2 k1 k1
Zh k2 Xh+1 Yh

Xh+1

Yh

Zh

9/2/2013

Data Structure: Trees

30

Single Left Rotation


k2 Xh Yh k1 k2 Zh+1 Xh

k1

Yh

Zh+1

9/2/2013

Data Structure: Trees

31

What cannot be solved by single rotation


k2 k1 Xh Yh+1 Yh+1

k1

k2
Zh

Zh

Xh

9/2/2013

Data Structure: Trees

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

Data Structure: Trees

33

Double Rotation
k3 k1 k2

Zh

Xh

k2
Xh

k1
Yh

k3

Yh

Zh

Yh

Yh

9/2/2013

Data Structure: Trees

34

Height of AVL Tree

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

Data Structure: Trees

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

Data Structure: Trees

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

Data Structure: Trees

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

Data Structure: Trees

42

Running Time

Binary Search Trees AVL Trees

Running time: Method find


BinaryNode find(Comparable x,BinaryNode t) { if( t == null ) return null; c if( x.compareTo( t.element ) < 0 ) T(h-1) return find( x, t.left ); else if( x.compareTo( t.element ) > 0 ) return find( x, t.right ); T(h-1) else return t; // Match }

T(0) = c h is the height of the tree. T(h) = T(h-1) + k T(h) = O(h)


9/2/2013 Data Structure: Trees 44

Running time: Method insert


BinaryNode insert (Comparable x, BinaryNode t) { if( t == null ) t = new BinaryNode( x, null, null ); c else if( x.compareTo( t.element ) < 0 ) T(h-1) t.left = insert( x, t.left ); else if( x.compareTo( t.element ) > 0 ) t.right = insert( x, t.right ); T(h-1) return t; }

T(0) = c h is the height of the tree. T(h) = T(h-1) + k T(h) = O(h)


9/2/2013 Data Structure: Trees 45

Running Time: Method findMax


private BinaryNode findMax( BinaryNode t ) { if( t != null ) while( t.right != null ) t = t.right; return t; }

T(h)=O(h),

where h is the height of the tree.

9/2/2013

Data Structure: Trees

46

Running Time: Method Remove


private BinaryNode remove(Comparable x,BinaryNode t) { if(t == null) return t; c if( x.compareTo(t.element) < 0 ) T(h-1) t.left = remove(x, t.left); else if ( x.compareTo(t.element) > 0 ) T(h-1) t.right = remove(x, t.right); else if (t.left!=null && t.right!=null) { t.element = findMin(t.right).element; O(h) t.right = remove(t.element, t.right); T(h-1) } else t = (t.left != null) ? t.left : t.right; return t; }

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

Running Time: Method insertion


private AvlNode insert(Comparable x,AvlNode t) { if (t == null) t=new AvlNode(x,null,null); c else if (x.compareTo( t.element ) < 0) T(h-1) { t.left = insert( x, t.left ); // rotate ... } k else if(x.compareTo( t.element ) > 0) { t.right = insert( x, t.right ); T(h-1) k // rotate ... } k // calculate height return t;

T(0) = c h is the height of the tree. T(h) = T(h-1) + k T(h) = O(h)


9/2/2013 Data Structure: Trees 48

Tree Traversal

For Binary Trees

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

Data Structure: Trees

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

Data Structure: Trees

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

Data Structure: Trees

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

Data Structure: Trees

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

Data Structure: Trees

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

Data Structure: Trees

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

Data Structure: Trees

Insert
Insert 32
12
4 8

Insert key 31
52 78
83

Split leave

Insert Splitkey node 31


60 54 56 57 59 60 69 61 62 66 67

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

Data Structure: Trees

9/2/2013

Data Structure: Trees

63

You might also like