Trees.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

Binary Trees, Binary Search Trees COMP171 Fall 2006.
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
Trees, Binary Trees, and Binary Search Trees COMP171.
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
AVL Trees Data Structures Fall 2006 Evan Korth Adopted from a presentation by Simon Garrett and the Mark Allen Weiss book.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
Trees Basic concepts of trees Implementation of trees Traversals of trees Binary trees Binary search trees AVL trees B-trees.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
COSC 2P03 Week 51 Representation of an AVL Node class AVLNode { AVLnode left; AVLnode right; int height; int height(AVLNode T) { return T == null? -1 :
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Trees CS 202 – Fundamental Structures of Computer Science II Bilkent.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
1 Trees - Part II © Dave Bockus. 2 Building a Binary Search Tree h i b c e d f m k a Input: i h b m e c f a d k.
CMSC 341 Binary Search Trees. 8/3/2007 UMBC CMSC 341 BST 2 Binary Search Tree A Binary Search Tree is a Binary Tree in which, at every node v, the values.
AVL Tree 1. is a binary search tree that For each node: –The height of its left and right subtree can only differ at most 1. –If a tree is an empty tree,
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
1 Trees. 2 Outline Preliminaries –What is Tree? –Implementation of Trees using C++ –Tree traversals and applications Binary Trees Binary Search Trees.
CC 215 Data Structures Trees
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Representation of an AVL Node
AVL Tree Mohammad Asad Abbasi Lecture 12
CMSC 341 Introduction to Trees.
CSE 373 Data Structures Lecture 7
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
Depict the Tree Structure in a picture
CS223 Advanced Data Structures and Algorithms
Trees 3: The Binary Search Tree
AVL Trees CENG 213 Data Structures.
CMSC 341 Introduction to Trees.
CMSC 341 Introduction to Trees.
Trees CSE 373 Data Structures.
Representation of an AVL Node
Trees.
Trees Basic concepts of trees Implementation of trees
Binary Trees, Binary Search Trees
Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Tree.
Trees BST, AVL, SPLAY TREES
Trees CSE 373 Data Structures.
CMSC 341 Binary Search Trees 8/3/2007 CMSC 341 BST.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Presentation transcript:

Trees

Outline Binary trees Binary search trees AVL trees B trees Applications 11/16/2018 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 T2 Tk … 11/16/2018 Data Structure: Trees

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

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

Terminologies height 11/16/2018 Data Structure: Trees

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

Binary Trees

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

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

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

Binary Search Trees

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

Example 8 3 11 2 4 9 12 1 6 5 7 11/16/2018 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; } 11/16/2018 Data Structure: Trees

FIND 8 Find 6 < 3 11 > 2 4 9 12 > 1 6 5 7 11/16/2018 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 } 11/16/2018 Data Structure: Trees

INSERT 10 8 Insert 10 > 3 11 < 2 4 9 12 10 1 6 5 7 11/16/2018 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; } 11/16/2018 Data Structure: Trees

FindMax, FindMin 8 3 11 2 4 9 12 max 1 6 min 5 7 11/16/2018 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; 11/16/2018 Data Structure: Trees

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Tree Traversal For Binary Trees

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

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

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

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

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

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

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

Example 12 52 78 4 8 83 91 19 26 37 46 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

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

Search Search for 66 12 52 78 4 8 83 91 19 26 37 46 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

Insert Insert 55 Split leave 12 52 78 4 8 83 91 19 26 37 46 60 69 1 2 12 52 78 4 8 83 91 19 26 37 46 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

Insert Insert 32 Insert key 31 Split leave Insert key 31 Split node 12 52 78 4 8 Insert key 31 Split node 83 91 19 26 37 46 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

11/16/2018 Data Structure: Trees