COMP : Trees. COMP20012 Trees 219

Similar documents
Trees. A tree is a directed graph with the property

CE 221 Data Structures and Algorithms

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Priority Queues (Heaps)

Priority Queues (Heaps)

CSCI2100B Data Structures Trees

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

CMSC 341. Binary Heaps. Priority Queues

Priority Queues. 04/10/03 Lecture 22 1

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Heaps, Heap Sort, and Priority Queues.

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

Figure 1: A complete binary tree.

Why Do We Need Trees?

CMSC 341 Lecture 14: Priority Queues, Heaps

CSCI2100B Data Structures Heaps

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Recall: Properties of B-Trees

Priority Queues Heaps Heapsort

CS 315 April 1. Goals: Heap (Chapter 6) continued review of Algorithms for Insert DeleteMin. algoritms for decrasekey increasekey Build-heap

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Programming II (CS300)

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

Binary Trees

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Programming II (CS300)

Programming II (CS300)

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Binary Trees, Binary Search Trees

Priority Queues Heaps Heapsort

Figure 1: A complete binary tree.

CS350: Data Structures Heaps and Priority Queues

Garbage Collection: recycling unused memory

Trees. (Trees) Data Structures and Programming Spring / 28

Algorithms and Data Structures

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Trees. CSE 373 Data Structures

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Analysis of Algorithms

Self-Balancing Search Trees. Chapter 11

Trees, Part 1: Unbalanced Trees

Data Structures Lesson 9

CMSC 341 Leftist Heaps

CMSC 341 Lecture 15 Leftist Heaps

Heaps. Heaps. A heap is a complete binary tree.

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

CMSC 341 Lecture 15 Leftist Heaps

DATA STRUCTURES AND ALGORITHMS

Chapter 20: Binary Trees

Course Review. Cpt S 223 Fall 2009

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

Tree Structures. A hierarchical data structure whose point of entry is the root node

Binary search trees (chapters )

Advanced Set Representation Methods

Algorithms. AVL Tree

ITI Introduction to Computing II

Data Structures and Algorithms

CE 221 Data Structures and Algorithms

Heap Model. specialized queue required heap (priority queue) provides at least

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Sorting and Searching

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

Balanced Search Trees. CS 3110 Fall 2010

3137 Data Structures and Algorithms in C++

ITI Introduction to Computing II

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

UNIT III BALANCED SEARCH TREES AND INDEXING

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Sorting and Searching

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

CMPE 160: Introduction to Object Oriented Programming

Trees, Binary Trees, and Binary Search Trees

Data Structures Brett Bernstein

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

[ DATA STRUCTURES ] Fig. (1) : A Tree

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Unit III - Tree TREES

CS102 Binary Search Trees

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Trees. Eric McCreath

Data Structure - Binary Tree 1 -

Data and File Structures Laboratory

Binary heaps (chapters ) Leftist heaps

Course Review for Finals. Cpt S 223 Fall 2008

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Binary Heaps. CSE 373 Data Structures Lecture 11

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Transcription:

COMP20012 3: Trees COMP20012 Trees 219

Trees Seen lots of examples. Parse Trees Decision Trees Search Trees Family Trees Hierarchical Structures Management Directories COMP20012 Trees 220

Trees have natural recursive structure Any node in tree has number of children each of which is a tree. COMP20012 Trees 221

Mathematical descriptions of Graphs and Trees A directed graph is a pair (N,E) consisting of a set of nodes N, together with a relation E on N. There is no restriction on the relation E. a E b iff there is an edge from a to b n2 n1 n3 n4 n5 COMP20012 Trees 222

A tree is a graph (N,P) (where the relation P is called has parent), with the following property For any node n, there is at most one node n with n P n. n2 n1 n3 n4 n5 n6 n7 n8 COMP20012 Trees 223

If there is no node n with n P n, n is called a root node. A tree with a single root node is called a rooted tree. Often the word tree is used to mean rooted tree, and the more general collection is known as a forest of trees. For any node n, the set {n n P n} is called the set of children of n. If a node n has no children it is called a leaf Not difficult to see that this is equivalent to the more normal recursive definition of a rooted tree COMP20012 Trees 224

A (rooted) tree also has the property that it is a directed graph such that There is one node (the root) from which all other nodes can be reached by exactly one path. COMP20012 Trees 225

Height and Depth For any node n in a tree the depth of n is the length of the path from the root to n (so the root has depth 0) For any node n in a tree the height of n is the length of the longest path from n to a leaf (so all leaves have height 0) The height of a tree is the height of its root. COMP20012 Trees 226

Representations of Trees In this section we look at different ways in which rooted trees can be represented in a programming language Have seen both Java and C representations of binary trees. COMP20012 Trees 227

public class BinaryNode { private Object element; private BinaryNode left; private BinaryNode right;... } public class BinaryTree { private BinaryNode root;... } COMP20012 Trees 228

and in C typedef struct TreeNode *PtrToNode; struct TreeNode { ElementType element; PtrToNode left; PtrToNode right; }; typedef PtrToNode Tree; COMP20012 Trees 229

A B C E F G H D COMP20012 Trees 230

Non-binary trees are almost as simple public class NaryNode { private Object element; private NaryNode firstchild; private NaryNode firstsibling;... } public class NaryTree { private NaryNode root;... } COMP20012 Trees 231

and typedef struct TreeNode *PtrToNode; struct TreeNode { ElementType element; PtrToNode FirstChild; PtrToNode NextSibling; }; typedef PtrToNode Tree; COMP20012 Trees 232

This looks almost the same as the binary tree representation, but is interpreted quite differently The nary tree A B C D E F G H I J COMP20012 Trees 233

is represented as A B C D E F G H I J COMP20012 Trees 234

Traversing Trees Can list the nodes of a tree in one of several orders Preorder: List the node, then recursively list all children subtrees Postorder: Recursively list all children subtrees, then list the node Inorder: Only suitable for binary trees. List left subtree, node, then right subtree Level order: list all the nodes at each level followed by all nodes at the next level etc. Exercise: Write preorder and postorder listing functions for both the binary and n-ary trees. How would you implement a level-order traversal? COMP20012 Trees 235

Other representations All the above representations use explicit pointers to represent the tree structure Now look at some implicit representations which do not use pointers. COMP20012 Trees 236

Array based representations Suppose the nodes of a tree have names 1...n (or something that we can conveniently map to and from 1...n). We can represent a tree (or even a forest of trees) with these nodes by use of single array. The array element a[i] should contain the parent of the node i, or if i is a root node, i itself. COMP20012 Trees 237

So the array Represents the forest of trees index 1 2 3 4 5 6 contents 1 2 5 5 5 2 1 2 5 6 3 4 Note that there is no restriction here on the amount of branching in the tree, since we give the parent relation directly, and not the children. COMP20012 Trees 238

Could also have data associated with each node This representation is not particularly useful if tree traversal is required How would we find the first child of a node or next sibling in this context? COMP20012 Trees 239

This representation is very useful for representing a partition of the set 1...n. A partition of a set X is a set of subsets X i with the properties The union of all the sets X i is X i.e. S X i = X All the sets X i are pairwise disjoint i.e. i,j,i j X i X j = /0 So {{1,3},{2},{4}} is a partition of the set {1,2,3,4}. A partition of a set is just a way of breaking it up into disjoint chunks. [x1] [x5] [x2] [x4] [x3] Partitions are used to represent equivalence relations on some set (see COMP10020 notes) COMP20012 Trees 240

Equivalence Relations Often elements of a set are regarded as equivalent because they have some set of properties in common even though they are not identical. For example two electrical components might be regarded as equivalent, even if they are produced by different manufacturers. Two different Java functions might be regarded as equivalent if they always produced the same results, even if they were implemented in completely different ways. COMP20012 Trees 241

It seems obvious that any notion of equivalence should satisfy the following: 1 a is equivalent to a 2 If a is equivalent to b then b is equivalent to a 3 If a is equivalent to b and b is equivalent to c then a is equivalent to c For any set A there will be many possible equivalence relations. COMP20012 Trees 242

Equivalence Classes and Partitions The idea of an equivalence relation R on a set A is that if a R b then in some sense a and b are the same. Any equivalence relation on a set A divides A in a natural way into disjoint parts. Each part contains elements that are equivalent to each other, so two elements a, b are put into the same part if and only if a R b. COMP20012 Trees 243

One example of their use is in generation of mazes. Start with a rectangular grid of cells, with walls between every cell. Choose a desired entry square and exit square in out COMP20012 Trees 244

Construct a partition by using the equivalence relation, a is related to b if there is a path from a to b. Initially this partition will consist of a set of sets of single elements, since with all the walls in place the only thing that is related to a is a itself. So the initial state of the partition will be {{1},{2}...{n}} COMP20012 Trees 245

Now choose a wall at random, and if the two cells separated by that wall are not already connected, remove the wall, and merge the partition members containing the two cells. in out For example {{1},{2}...{6,7}...{n}} Continue this process until the entry and exit cells are in the same partition member. COMP20012 Trees 246

{{25, 26} {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44}} +--+--+--+--+--+ * + + + + + + + + + +--+--+ + + +--+--+--+ +--+ +--+ + + +--+--+ + + + +--+--+ +--+ + + +--+--+--+--+ + +--+ + +--+ +--+--+--+--+--+ COMP20012 Trees 247

+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ * + +--+ + +--+--+--+--+ +--+ +--+--+ + + +--+ +--+--+--+--+ + +--+ + + +--+ + + +--+--+ +--+ + + + + +--+ +--+ + +--+--+--+ + + + +--+ + +--+ +--+ +--+ +--+--+--+--+ +--+ +--+--+ +--+--+ + + + + + + + + +--+--+--+ +--+ +--+ + +--+--+--+ +--+--+ + +--+ + + + +--+--+ +--+ +--+ +--+--+--+ + + +--+--+--+--+--+ + + + + +--+ +--+--+ +--+ +--+ + + + + +--+ +--+--+--+--+ + +--+ +--+ + + + + +--+ +--+ + + +--+ + + + +--+--+--+--+--+ + +--+--+--+ + +--+ + + + + +--+--+ +--+--+--+ +--+--+ +--+--+ +--+--+--+--+--+--+--+--+ + +--+--+--+ +--+ + +--+--+ +--+--+ + + +--+--+ +--+--+--+ + +--+ +--+ +--+ +--+--+--+ + +--+--+ + +--+ +--+--+--+--+--+ + +--+ + + + + +--+--+--+ +--+--+ +--+ +--+--+ + +--+ + + + +--+ + + +--+ +--+--+--+ + +--+--+ +--+--+ +--+--+--+--+ +--+ +--+ + + +--+--+--+ + +--+--+ + + +--+--+ + + +--+ +--+ +--+ +--+--+--+ + +--+ + + +--+--+ +--+--+--+--+--+ + + +--+--+ +--+ +--+ +--+ +--+ + + + + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ COMP20012 Trees 248

A simple way of representing a partition is by using a forest of trees For example X = {1,2,3,4,5,6} The partition {{1},{2,6},{3,4,5}} can be represented by the forest 1 2 5 6 3 4 COMP20012 Trees 249

The same partition could also be represented by the forest 1 6 4 2 5 3 To determine whether 2 elements are in the same member of the partition, just find the root of the trees they are in. COMP20012 Trees 250

To combine two elements of the partition, just graft the trees together, by making the root of one tree the parent of the root of the other. e.g. to combine {2,6} and {3,4,5} from the first representation of the above partition or 2 6 2 6 4 3 5 5 4 3 COMP20012 Trees 251

Can optimise the tree by flattening, by making the root of each node its parent 5 2 3 4 6 We will see other applications of this technique later in the course COMP20012 Trees 252

Binary Heaps This is another type of tree with a pointer-free representation. An essentially complete binary tree is one in which all nodes have exactly two children, except possibly those at the lowest level, which is filled from left to right. How many leaves does such a tree have? COMP20012 Trees 253

A binary tree has the heap property if the value of the key at any node is less than or equal to the values of all the keys of its children. 13 16 22 35 20 32 24 26 33 Sometimes greater than or equal to is used instead. Note difference between this and an ordered binary tree COMP20012 Trees 254

A binary heap is a binary tree which has the type invariant it is essentially complete it has the heap property The smallest element in a heap is always at its root. All operations on a heap must preserve the type invariant COMP20012 Trees 255

The above heap (or any complete binary tree) can be stored in an array as follows index 0 1 2 3 4 5 6 7 8 9 10 value 13 16 20 22 35 32 24 26 33 COMP20012 Trees 256

In general, the root is stored in slot 1, and the children of the element at position i can be found at positions 2i and 2i + 1 To insert into a heap just insert after the last element (as long as there is room.) This can destroy the heap property, so then need to repair the heap to reestablish the invariant. COMP20012 Trees 257

Increase size of heap by one to open up a hole at the last element If the item to be inserted is less than the parent of the hole, move the parent into the hole and create a new hole Repeat this until the parent of the hole is less than the element to be inserted and insert the element into the hole COMP20012 Trees 258

Inserting 15 into the heap 1 2 13 13 16 20 16 20 22 35 32 24 22 32 24 26 3 33 26 4 33 35 13 13 20 15 20 22 16 32 24 22 16 32 24 26 33 35 26 33 35 COMP20012 Trees 259

The major use of heaps is as priority queues, so a general delete is not usually needed. Usual form of delete is deletemin, which removes the smallest element from the heap COMP20012 Trees 260

Delete Minimum This is performed as follows:- Remove the minimum element, which will be at the root, creating a hole, and decreasing the size of the heap by 1, remembering the last element in the heap If last element is smaller than the children of the hole, insert it, and end Move the hole down by moving up the smaller of the two children until last element can be placed in the hole (which may be at a leaf) Move the old last element into the hole COMP20012 Trees 261

Delete Minimum Example 1 2 15 15 20 20 22 16 32 24 22 16 32 24 3 26 33 35 26 33 35 4 15 15 16 20 16 20 22 32 24 22 35 32 24 26 33 35 26 33 COMP20012 Trees 262

Delete next lowest 1 2 3 16 16 16 20 20 22 20 22 35 32 24 22 35 32 24 35 32 24 26 33 26 33 26 33 4 5 16 16 22 20 22 20 26 35 32 24 26 35 32 24 33 33 COMP20012 Trees 263

... and the next lowest 1 2 20 22 20 22 26 35 32 24 26 35 32 24 33 33 3 4 20 20 22 24 22 24 33 26 35 32 26 35 32 33 COMP20012 Trees 264

Building Heaps Given a list of keys, a heap can be built simply by using the insert function described above. A more efficient O(n) technique is the following: Put the list elements into the heap array in any order, without worrying about heap invariant Turn the array into a heap as follows: Starting at the rightmost, deepest node with a child, swap its contents with that of one of its children to ensure the heap property for the tree below, then percolate that element down if necessary Work leftwards and upwards to the root. COMP20012 Trees 265

Building Heap example For example: index 0 1 2 3 4 5 6 7 8 9 10 value 43 6 20 16 13 42 4 12 33 COMP20012 Trees 266

Building Heap example 1 2 3 43 43 43 6 20 6 20 6 4 16 13 42 4 12 13 42 4 12 13 42 20 12 33 16 33 16 33 4 5 6 43 4 4 6 4 6 43 6 20 12 13 42 20 12 13 42 20 12 13 42 43 16 33 16 33 16 33 COMP20012 Trees 267

Using this technique followed by removing the smallest element from the heap until it is empty, gives an O(n logn) sort technique called heapsort COMP20012 Trees 268

Priority Queues Normal queues are FIFO devices In a Priority Queue each element entering a queue is assigned a value, usually a number, and the first element to leave the queue is that with the lowest value. Used in Operating Systems etc Most common implementation of Priority Queues is the heap. COMP20012 Trees 269

An interface for Priority Queues public interface PriorityQueue { boolean insert( Comparable x ); Comparable findmin( ); Comparable deletemin( ); boolean isempty( ); void makeempty( ); int size( ); } COMP20012 Trees 270

Binary Heaps in Java This code is largely taken from Weiss, with some minor modifications public class BinaryHeap { private static final int DEFAULT_CAPACITY = 100; private int currentsize; // Number of elements private Comparable [ ] array; // The heap array COMP20012 Trees 271

/** * Construct the binary heap. */ public BinaryHeap( ) { currentsize = 0; array = new Comparable[ DEFAULT_CAPACITY + 1 ]; } COMP20012 Trees 272

/** * Construct the binary heap from an array. * @param items the inital items in the binary heap. */ public BinaryHeap( Comparable [ ] items ) { currentsize = items.length; array = new Comparable[ items.length + 1 ]; } for( int i = 0; i < items.length; i++ ) array[ i + 1 ] = items[ i ]; buildheap( ); COMP20012 Trees 273

/** * Insert into the priority queue. * Duplicates are allowed. * @param x the item to insert. */ public boolean insert( Comparable x ) { if( currentsize + 1 == array.length ) doublearray( ); // Percolate up int hole = ++currentsize; array[ 0 ] = x; } for( ; x.compareto( array[ hole / 2 ] ) < 0; hole /= 2 ) array[ hole ] = array[ hole / 2 ]; array[ hole ] = x; return true; COMP20012 Trees 274

/** * Find the smallest item in the priority queue. * @return the smallest item. */ public Comparable findmin( ) { if( isempty( ) ) throw new RuntimeException( "Empty binary heap" ); return array[ 1 ]; } COMP20012 Trees 275

/** * Remove the smallest item from the priority queue. * @return the smallest item. */ public Comparable deletemin( ) { Comparable minitem = findmin( ); array[ 1 ] = array[ currentsize-- ]; percolatedown( 1 ); } return minitem; COMP20012 Trees 276

/** * Establish heap order property from an arbitrary * arrangement of items. Runs in linear time. */ private void buildheap( ) { for( int i = currentsize / 2; i > 0; i-- ) percolatedown( i ); } COMP20012 Trees 277

/** * Test if the priority queue is logically empty. * @return true if empty, false otherwise. */ public boolean isempty( ) { return currentsize == 0; } /** * Returns size. * @return current size. */ public int size( ) { return currentsize; } COMP20012 Trees 278

/** * Make the priority queue logically empty. */ public void makeempty( ) { currentsize = 0; } COMP20012 Trees 279

/** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */ private void percolatedown( int hole ) { int child; Comparable tmp = array[ hole ]; for( ; hole * 2 <= currentsize; hole = child ) { child = hole * 2; if( child!= currentsize && array[ child + 1 ].compareto( array[ child ] ) < 0 ) child++; if( array[ child ].compareto( tmp ) < 0 ) array[ hole ] = array[ child ]; else break; } array[ hole ] = tmp; } COMP20012 Trees 280

/** * Internal method to extend array. */ private void doublearray( ) { Comparable [ ] newarray; } newarray = new Comparable[ array.length * 2 ]; for( int i = 0; i < array.length; i++ ) newarray[ i ] = array[ i ]; array = newarray; COMP20012 Trees 281

Ordered Binary Trees A binary tree is said to be ordered if, for every node n in the tree, the values of the keys in its left subtree are smaller that the key at n, and those in the right subtree are greater than the key at n. The operations required on an ordered binary tree are Initialise a tree Find the location (if any) of a given key in a tree. Insert a given key in a tree. Delete a given key from a tree. List the contents of a tree. Seen implementations of these before COMP20012 Trees 282

If we adopt a naïve approach to insertion and the data is pre-sorted, the cost of building a tree becomes quadratic in the size of the data (Why?) One solution is to attempt to keep the tree balanced There are several possible definitions of the term balanced, one is A tree is balanced if every node has left and right subtrees whose heights differ by at most 1 This is another type invariant A tree with this property is called an AVL tree. (Adelson, Velski and Landis) COMP20012 Trees 283

For example These trees are balanced COMP20012 Trees 284

These trees are not COMP20012 Trees 285

The problem is that when a new node is inserted, or an old one deleted, the tree can become unbalanced When inserting a new node in an AVL tree, find a place to put it in the usual way Then check the tree to be sure that it is still balanced If tree has lost the AVL property, only nodes between inserted element and root can have balance destroyed. (Why?) COMP20012 Trees 286

AVL trees: restoring the balance For example if we insert 6 into the tree below, unbalances tree First unbalanced ancestor of new node is node containing 9 Can restore balance by rotating unbalanced subtree. 5 5 2 9 2 7 1 4 7 1 4 6 9 3 6 3 COMP20012 Trees 287

Suppose n is the first node (going up) which is unbalanced. Four different possibilities Imbalance due to insertion in left subtree of left child Imbalance due to insertion in right subtree of right child Imbalance due to insertion in right subtree of left child Imbalance due to insertion in left subtree of right child COMP20012 Trees 288

Single right rotation, imbalance in left subtree of left child B A A B T3 T1 T1 T2 T2 T3 COMP20012 Trees 289

Single left rotation, imbalance in right subtree of right child A B B A T1 T3 T2 T3 T1 T2 COMP20012 Trees 290

Now if we insert 8 into the tree below, unbalances tree First unbalanced ancestor of new node is again node containing 9 Need two rotations to restore balance 5 2 9 1 4 7 3 8 COMP20012 Trees 291

5 5 2 9 2 9 1 4 7 1 4 8 3 8 3 7 5 2 1 4 8 7 9 3 COMP20012 Trees 292

Double left-right rotation, imbalance on right subtree of left child C A T4 B T1 T2 T3 COMP20012 Trees 293

C B B T4 A C A T3 T1 T2 T3 T4 T1 T2 COMP20012 Trees 294

Double right-left rotation, imbalance on left subtree of right child A T1 C B T4 T2 T3 COMP20012 Trees 295

A B T1 B A C C T2 T3 T4 T1 T2 T3 T4 COMP20012 Trees 296

Balancing algorithm performs at most 1 constant time operation on each of the ancestors of the unbalanced node. So, restoring the AVL property to the tree is an O(log N) operation. COMP20012 Trees 297

AVL Trees: An example Starting tree 1 2 3 4 6 5 7 COMP20012 Trees 298

Insert 16 1 2 3 4 5 6 7 16 COMP20012 Trees 299

Insert 15 1 2 3 4 5 6 15 7 16 COMP20012 Trees 300

Insert 14, initial position 2 1 3 4 5 6 15 7 16 14 COMP20012 Trees 301

After one rotation 1 2 3 4 6 5 7 15 14 16 COMP20012 Trees 302

Final position, after two rotations 2 1 4 7 3 6 15 5 14 16 COMP20012 Trees 303

Insert 13 1 2 3 4 6 5 7 13 15 14 16 COMP20012 Trees 304

Insert 12 1 2 3 4 6 5 7 15 13 12 14 16 COMP20012 Trees 305

Insert 11 1 2 3 4 6 5 7 11 13 12 15 14 16 COMP20012 Trees 306

Insert 10 1 2 3 4 6 5 7 13 11 10 12 15 14 16 COMP20012 Trees 307

Insert 8 1 2 3 4 6 5 10 8 7 13 11 12 15 14 16 COMP20012 Trees 308

Insert 9 1 2 3 4 6 5 8 7 13 11 9 10 12 15 14 16 COMP20012 Trees 309

2-3 Trees 2-3 trees are search trees that maintain their balance by relaxing the structural constraint of being a binary tree. Some nodes have 2 children and some have 3, hence the name COMP20012 Trees 310

A 2-3 tree is a tree with the following properties The root is either a leaf or has either 2 or 3 children All data is stored at the leaves. All leaves are at the same depth The first two of these constraints are structural and the third is part of the type invariant COMP20012 Trees 311

The type invariant is completed with the following constraint In each node, we store the value of smallest leaf in tree of second child, and value of smallest leaf in tree of third child, if there is one 7 16 5 8 12 19 2 5 7 8 12 16 19 COMP20012 Trees 312

Insert 18 7 16 5 8 12 18 19 2 5 7 8 12 16 18 19 COMP20012 Trees 313

Insert 10 7 16 5 8 12 18 19 2 5 7 8 10 12 16 18 19 Wants to go between 8 and 12, so need to split 3 node into 2 2 s But root now appears to have 4 children, so need to split and make new root COMP20012 Trees 314

10 7 16 5 8 12 18 19 2 5 7 8 10 12 16 18 19 COMP20012 Trees 315

B trees 2-3 trees can be generalised to trees that have the following properties, for some natural number M The root is either a leaf or has between 2 and M children All non-leaf nodes (except the root) have between M/2 and M children All leaves have the same depth. Such trees are called B-trees of order M. Main use is in database systems, where tree is on disk rather than in memory Want to minimise the number of disk accesses B-trees are in general very shallow COMP20012 Trees 316