Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees.

Similar presentations


Presentation on theme: "Trees."— Presentation transcript:

1 Trees

2 Outline Binary trees Binary search trees AVL trees B trees
Applications 11/16/2018 Data Structure: Trees

3 Trees

4 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 T2 Tk 11/16/2018 Data Structure: Trees

5 Terminologies parent root children siblings subtrees grandchildren
11/16/2018 Data Structure: Trees

6 Terminologies path ancestor length of path depth
length of path from the root descendant 11/16/2018 Data Structure: Trees

7 Terminologies height 11/16/2018 Data Structure: Trees

8 Tree: Implementation class TreeNode { Object element;
TreeNode firstChild; TreeNode nextSibling; } nextSibling=null nextSibling=null nextsibling firstChild=null firstChild=null firstChild nextSibling=null firstChild=null firstChild=null 11/16/2018 Data Structure: Trees

9 Binary Trees

10 Binary Trees A tree in which no node can have more than 2 children.
11/16/2018 Data Structure: Trees

11 Binary Tree: Implementation
Class BinaryNode { Object element; BinaryNode left; BinaryNode right; } 3 node.element=3 node.left=node node.right=node 5 node.element=5 node.left=node node.right=null 9 node.element=9 node.left=null node.right=null 11/16/2018 Data Structure: Trees

12 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 } 11/16/2018 Data Structure: Trees

13 Binary Search Trees

14 Binary Search Trees Properties of a binary search tree T
T is a binary tree. 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. 11/16/2018 Data Structure: Trees

15 Example 8 3 11 2 4 9 12 1 6 5 7 11/16/2018 Data Structure: Trees

16 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; } 11/16/2018 Data Structure: Trees

17 FIND 8 Find 6 < 3 11 > 2 4 9 12 > 1 6 5 7 11/16/2018
Data Structure: Trees

18 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 } 11/16/2018 Data Structure: Trees

19 INSERT 10 8 Insert 10 > 3 11 < 2 4 9 12 10 1 6 5 7 11/16/2018
Data Structure: Trees

20 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; } 11/16/2018 Data Structure: Trees

21 FindMax, FindMin 8 3 11 2 4 9 12 max 1 6 min 5 7 11/16/2018
Data Structure: Trees

22 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; 11/16/2018 Data Structure: Trees

23 REMOVE 11 Remove 7 3 13 2 7 12 14 1 5 9 4 6 10 11/16/2018 Data Structure: Trees

24 REMOVE 11 Remove 7 3 13 2 7 12 14 1 9 10 8 11/16/2018 Data Structure: Trees

25 REMOVE 11 Remove 7 3 13 2 7 12 14 1 5 6 4 11/16/2018 Data Structure: Trees

26 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; 11/16/2018 Data Structure: Trees

27 AVL Trees

28 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. 11/16/2018 Data Structure: Trees

29 AVL Trees 11 11 3 7 2 1 13 14 12 5 6 4 3 13 2 7 12 14 1 5 8 not AVL tree AVL tree 11/16/2018 Data Structure: Trees

30 Single Right Rotation k2 k1 k1 Zh k2 Xh+1 Xh+1 Yh Yh Zh 11/16/2018
Data Structure: Trees

31 Single Left Rotation k2 k1 k1 Xh k2 Yh Zh+1 Zh+1 Xh Yh 11/16/2018
Data Structure: Trees

32 What cannot be solved by single rotation
k2 k1 k2 Xh k1 Zh Yh+1 Zh Xh Yh+1 11/16/2018 Data Structure: Trees

33 Double Rotation k3 k Xh Yh+1 Zh k3 k2 Xh Zh k1 Y’h Y’’h k3 k1 Xh Zh k2
11/16/2018 Data Structure: Trees

34 Double Rotation k3 k1 Xh Zh k2 Y’h Y’’h k3 k2 Xh Zh k1 Y’h Y’’h
11/16/2018 Data Structure: Trees

35 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. 11/16/2018 Data Structure: Trees

36 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; } 11/16/2018 Data Structure: Trees

37 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 ); } 11/16/2018 Data Structure: Trees

38 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 ); } ; // Duplicate; do nothing t.height=max(height(t.left),height(t.right))+1; return t; } 11/16/2018 Data Structure: Trees

39 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 k2 k1 11/16/2018 Data Structure: Trees

40 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; } k2 k1 k2 k1 11/16/2018 Data Structure: Trees

41 Method doubleWithLeftChild
private static AvlNode doubleWithLeftChild (AvlNode k3) { k3.left = rotateWithRightChild(k3.left); return(rotateWithLeftChild(K3); } k3 k2 k1 k3 k2 k1 k3 k2 k1 11/16/2018 Data Structure: Trees

42 Method doubleWithRightChild
private static AvlNode doubleWithRightChild (AvlNode k3) { k3.right = rotateWithLeftChild(k3.right); return(rotateWithRightChild(k3); } k3 k2 k1 k3 k2 k3 k1 k2 k1 11/16/2018 Data Structure: Trees

43 Binary Search Trees AVL Trees
Running Time Binary Search Trees AVL Trees

44 Running time: Method find
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 } T(0) = c h is the height of the tree. T(h) = T(h-1) + k’ T(h) = O(h) c T(h-1) 11/16/2018 Data Structure: Trees

45 Running time: Method insert
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 ); return t; } T(0) = c h is the height of the tree. T(h) = T(h-1) + k’ T(h) = O(h) c T(h-1) 11/16/2018 Data Structure: Trees

46 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. 11/16/2018 Data Structure: Trees

47 Running Time: Method Remove
private BinaryNode remove(Comparable x,BinaryNode t) { if(t == null) return t; 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) { t.element = findMin(t.right).element; t.right = remove(t.element, t.right); } 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) c T(h-1) O(h) 11/16/2018 Data Structure: Trees

48 Running Time: Method insertion
private AvlNode insert(Comparable x,AvlNode t) { if (t == null) t=new AvlNode(x,null,null); else if (x.compareTo( t.element ) < 0) { t.left = insert( x, t.left ); // rotate } else if(x.compareTo( t.element ) > 0) { t.right = insert( x, t.right ); // rotate } // calculate height return t; T(0) = c h is the height of the tree. T(h) = T(h-1) + k’ T(h) = O(h) c T(h-1) k k’ 11/16/2018 Data Structure: Trees

49 Tree Traversal For Binary Trees

50 Inorder Traversal + (a – (b * (c / d))) + (e – f) - - a * e f / b c d
11/16/2018 Data Structure: Trees

51 Method inorder public static void inorder (BinaryNode t)
{ if ( t!=null ) { inorder(t.left); System.out.println(t.element); inorder(t.right); } 11/16/2018 Data Structure: Trees

52 Preorder Traversal + + – a * b / c d – e f - - a * e f / b c d
11/16/2018 Data Structure: Trees

53 Method preorder public static void preorder (BinaryNode t)
{ if ( t!=null ) { System.out.println(t.element); inorder(t.left); inorder(t.right); } 11/16/2018 Data Structure: Trees

54 Postorder Traversal + a b c d / * – e f – + - - a * e f / b c d
11/16/2018 Data Structure: Trees

55 Method postorder public static void postorder (BinaryNode t)
{ if ( t!=null ) { inorder(t.left); inorder(t.right); System.out.println(t.element); } 11/16/2018 Data Structure: Trees

56 B Trees

57 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) 11/16/2018 Data Structure: Trees

58 Example 4 8 83 91 60 69 1 2 5 6 7 8 9 11 12 79 80 81 82 83 85 86 90 93 95 97 98 99 54 56 57 59 60 61 62 66 67 70 71 76 77 13 14 17 19 20 21 22 26 27 28 31 35 38 44 45 49 50 11/16/2018 Data Structure: Trees

59 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. 11/16/2018 Data Structure: Trees

60 Search Search for 66 4 8 83 91 60 69 1 2 5 6 7 8 9 11 12 79 80 81 82 83 85 86 90 93 95 97 98 99 54 56 57 59 60 61 62 66 67 70 71 76 77 13 14 17 19 20 21 22 26 27 28 31 35 38 44 45 49 50 11/16/2018 Data Structure: Trees

61 Insert Insert 55 Split leave 12 52 78 4 8 83 91 19 26 37 46 60 69 1 2
4 8 83 91 60 69 1 2 5 6 7 8 9 11 12 79 80 81 82 83 85 86 90 93 95 97 98 99 54 56 57 59 60 61 62 66 67 70 71 76 77 13 14 17 19 20 21 22 26 27 28 31 35 38 44 45 49 50 11/16/2018 Data Structure: Trees

62 Insert Insert 32 Insert key 31 Split leave Insert key 31 Split node
4 8 Insert key 31 Split node 83 91 60 69 1 2 5 6 7 8 9 11 12 79 80 81 82 83 85 86 90 93 95 97 98 99 54 56 57 59 60 61 62 66 67 70 71 76 77 13 14 17 19 20 21 22 26 27 28 31 35 36 38 44 45 49 50 11/16/2018 Data Structure: Trees

63 11/16/2018 Data Structure: Trees


Download ppt "Trees."

Similar presentations


Ads by Google