1 Balanced Search Trees (Walls & Mirrors - Chapter 12 & end of Chapter 10)

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
Chapter 4: Trees Part II - AVL Tree
Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Heaps & Priority Queues (Walls & Mirrors - Remainder of Chapter 11)
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Balanced Binary Search Trees
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
A balanced life is a prefect life.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Balanced Search Trees CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Data Structures and Algorithms1 B-Trees with Minimum=1 2-3 Trees.
B-Trees Disk Storage What is a multiway tree? What is a B-tree?
1 Trees & More Tables (Walls & Mirrors - Chapter 10 & Beginning of 11)
Trees and Red-Black Trees Gordon College Prof. Brinton.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part B Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
1 B-Trees Disk Storage What is a multiway tree? What is a B-tree? Why B-trees? Comparing B-trees and AVL-trees Searching a B-tree Insertion in a B-tree.
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Multi-Way search Trees Trees: a. Nodes may contain 1 or 2 items. b. A node with k items has k + 1 children c. All leaves are on same level.
B-Trees and B+-Trees Disk Storage What is a multiway tree?
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
Homework #3 Due Thursday, April 17 Problems: –Chapter 11: 11.6, –Chapter 12: 12.1, 12.2, 12.3, 12.4, 12.5, 12.7.
Marc Smith and Jim Ten Eyck
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Advanced Implementation of Tables.
1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
Tree.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Data Structures CSCI 2720 Spring 2007 Balanced Trees.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
2-3 Trees, Trees Red-Black Trees
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Starting at Binary Trees
Binary Search Tree vs. Balanced Search Tree. Why care about advanced implementations? Same entries, different insertion sequence: 10,20,30,40,50,60,70,
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
File Organization and Processing Week Tree Tree.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
1 Multi-Level Indexing and B-Trees. 2 Statement of the Problem When indexes grow too large they have to be stored on secondary storage. However, there.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Indexing Database Management Systems. Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B + -Tree Index Files File Organization 2.
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Data Structures Balanced Trees CSCI 2720 Spring 2007.
AA Trees.
Multiway Search Trees Data may not fit into main memory
(edited by Nadia Al-Ghreimil)
Data Structures Balanced Trees CSCI
CS202 - Fundamental Structures of Computer Science II
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
2-3-4 Trees Red-Black Trees
Advanced Implementation of Tables
(edited by Nadia Al-Ghreimil)
Advanced Implementation of Tables
Presentation transcript:

1 Balanced Search Trees (Walls & Mirrors - Chapter 12 & end of Chapter 10)

2 If you don’t find it in the Index, look very carefully through the entire catalogue. – Consumer’s Guide, Sears, Roebuck & Co. (1897) One mustn’t ask apple trees for oranges. – Gustave Flaubert (Pensées de Gustave Flaubert) A fool sees not the same tree that a wise man sees. – William Blake (The Marriage of Heaven and Hell)

3 A tree’s a tree. How many more do you need to look at? – Ronald Reagan (Speech, Sept. 12, 1965)

4 Overview Building a Minimum-Height Binary Search Tree 2-3 Trees Trees Red-Black Trees AVL Trees General Trees N-ary Trees

5 Balanced Binary Trees Recall that a binary tree is balanced if the difference in height between any node’s left and right subtree is  1.

6 Balanced Search Trees If a binary search tree containing n nodes is balanced, insertion, deletion and retrieval will all take O( log n ) time. Otherwise, each of these operations could take O( n ) time, which is no better than a sequential search through a linked list balanced unbalanced

7 Building a Minimum-Height Binary Search Tree Basic idea: 1) Write items, in sorted order, from a binary search tree to a file using inorder traversal. 2) Invoke buildtree, which a) Creates a new tree node, t ; b) Calls itself recursively to read half of the items from the file and place them in t ’s left subtree; and then c) Calls itself recursively to read the remaining items and place them in t ’s right subtree. When step 2 completes, t will be the root of a (balanced) minimum-height binary search tree.

8 Building a Min-Height Binary Search Tree: Algorithm // build a minimum-height binary search tree from n items in a file // sorted by search-key; treePtr will point to the tree’s root buildTree( TreeNode *&treePtr, int n ) { if( n > 0 ) { treePtr = new TreeNode; // create a new TreeNode treePtr -> leftChild = treePtr -> rightChild = NULL; buildTree( treePtr -> leftChild, n/2 ); // build left subtree readItem( treePtr -> item ); // get next item from input file buildTree( treePtr -> rightChild, (n – 1)/2 ); // build right subtree }

9 Building a Min-Height Binary Search Tree Unbalanced binary search tree Items from the tree are written to a file using inorder traversal, placing them in the following order: 10, 20, 22, 25, 30, 35, 40, 50, 60 buildTree is invoked with n = 9, which: – creates a new node, t 1 ; – calls itself recursively with t 1 ’s leftChild pointer and n = [ 9/2 ] = 4. [ x ] denotes the greatest integer in x

10 Building a Min-Height Binary Search Tree buildTree( n = 4 ): – creates a new node, t 2, and makes t 1 ’s leftChild pointer point to it; – calls itself recursively with t 2 ’s leftChild pointer and n = [ 4/2 ] = 2. Items in file: 10, 20, 22, 25, 30, 35, 40, 50, 60 n = 9: t 1 n = 4: t 2

11 Building a Min-Height Binary Search Tree buildTree( n = 2 ): – creates a new node, t 3, and makes t 2 ’s leftChild pointer point to it; – calls itself recursively with t 3 ’s leftChild pointer and n = [ 2/2 ] = 1. Items in file: 10, 20, 22, 25, 30, 35, 40, 50, 60 n = 9: t 1 n = 4: t 2 n = 2: t 3

12 Building a Min-Height Binary Search Tree buildTree( n = 1 ): – creates a new node, t 4, and makes t 3 ’s leftChild pointer point to it; – calls itself recursively with t 4 ’s leftChild pointer and n = [ 1/2 ] = 0. Items in file: 10, 20, 22, 25, 30, 35, 40, 50, 60 n = 9: t 1 n = 4: t 2 n = 2: t 3 n = 1: t 4

13 Building a Min-Height Binary Search Tree buildtree( n = 0 ): – returns immediately to the place where it was called. buildTree( n = 1 ): – reads the first item (= 10) from the input file and stores it in t 4 ; – calls itself recursively with t 4 ’s rightChild pointer and n = [ (1 – 1) / 2 ] = 0. Items in file: 10, 20, 22, 25, 30, 35, 40, 50, 60 n = 9: t 1 n = 4: t 2 n = 2: t 3 10 n = 1: t 4

14 Building a Min-Height Binary Search Tree buildtree( n = 0 ): – returns immediately to the place where it was called. buildTree( n = 1 ): – is done and returns buildTree( n = 2 ): – reads the next item (= 20) from the input file and stores it in t 3 ; – calls itself recursively with t 3 ’s rightChild pointer and n = [ (2 – 1) / 2 ] = 0. Unread items in file: 20, 22, 25, 30, 35, 40, 50, 60 n = 9: t 1 n = 4: t 2 20 n = 2: t 3 10 n = 1: t 4

15 Building a Min-Height Binary Search Tree Unread items in file: 22, 25, 30, 35, 40, 50, n = 9: t 1 n = 4: t 2 20 n = 2: t 3 10 n = 1: t 4 buildtree( n = 0 ): – returns immediately buildTree( n = 2 ): – is done and returns buildTree( n = 4 ): – reads the next item (= 22) from the input file and stores it in t 2 ; – calls itself recursively with t 2 ’s rightChild pointer and n = [ (4 – 1)/2 ] = 1.

16 Building a Min-Height Binary Search Tree Unread items in file: 25, 30, 35, 40, 50, 60 buildTree( n = 1 ): – creates a new node, t 5, and makes t 2 ’s rightChild pointer point to it; – calls itself recursively with t 5 ’s leftChild pointer and n = [ 1/2 ] = n = 9: t 1 n = 4: t 2 n = 2: t 3 10 n = 1: t 4 n = 1: t 5

17 Building a Min-Height Binary Search Tree Unread items in file: 25, 30, 35, 40, 50, 60 buildtree( n = 0 ): – returns immediately buildTree( n = 1 ): – reads the next item (= 25) from the input file and stores it in t 5 ; – calls itself recursively with t 5 ’s rightChild pointer and n = [ (1 – 1) / 2 ] = n = 9: t 1 n = 4: t 2 n = 2: t 3 10 n = 1: t 4 25 n = 1: t 5

18 Building a Min-Height Binary Search Tree Unread items in file: 30, 35, 40, 50, 60 buildtree( n = 0 ): – returns immediately buildTree( n = 1 ): – is done and returns buildTree( n = 4 ): – is done and returns buildTree( n = 9 ): – reads the next item (= 30) from the input file and stores it in t 1 ; – calls itself recursively with t 1 ’s rightChild pointer and n = [ (9 – 1)/2 ] = n = 9: t 1 n = 4: t 2 n = 2: t 3 10 n = 1: t 4 25 n = 1: t 5

19 Building a Min-Height Binary Search Tree Unread items in file: 35, 40, 50, 60 buildTree( n = 4 ): – creates a new node, t 6, and makes t 1 ’s rightChild pointer point to it; – calls itself recursively with t 6 ’s leftChild pointer and n = [ 4/2 ] = n = 9: t 1 n = 4: t 2 n = 2: t 3 10 n = 1: t 4 25 n = 1: t 5 n = 4: t 6

20 Building a Min-Height Binary Search Tree Unread items in file: 35, 40, 50, 60 buildTree( n = 2 ): – creates a new node, t 7, and makes t 6 ’s leftChild pointer point to it; – calls itself recursively with t 7 ’s leftChild pointer and n = [ 2/2 ] = n = 9: t n = 2: t 7 n = 4: t 6

21 Building a Min-Height Binary Search Tree Unread items in file: 35, 40, 50, 60 buildTree( n = 1 ): – creates a new node, t 8, and makes t 7 ’s leftChild pointer point to it; – calls itself recursively with t 8 ’s leftChild pointer and n = [ 1/2 ] = n = 9: t n = 2: t 7 n = 4: t 6 n = 1: t 8

22 Building a Min-Height Binary Search Tree Unread items in file: 35, 40, 50, 60 buildtree( n = 0 ): – returns immediately. buildTree( n = 1 ): – reads the next item (= 35) from the input file and stores it in t 8 ; – calls itself recursively with t 8 ’s rightChild pointer and n = [ (1 – 1) / 2 ] = n = 9: t n = 2: t 7 n = 4: t 6 35 n = 1: t 8

23 Building a Min-Height Binary Search Tree Unread items in file: 40, 50, n = 9: t n = 2: t 7 n = 4: t n = 1: t 8 buildtree( n = 0 ): – returns immediately buildTree( n = 1 ): – is done and returns buildTree( n = 2 ): – reads the next item (= 40) from the input file and stores it in t 7 ; – calls itself recursively with t 7 ’s rightChild pointer and n = [ (2 – 1)/2 ] = 0.

24 Building a Min-Height Binary Search Tree Unread items in file: 50, n = 9: t n = 2: t 7 n = 4: t n = 1: t 8 buildtree( n = 0 ): – returns immediately buildTree( n = 2 ): – is done and returns buildTree( n = 4 ): – reads the next item (= 50) from the input file and stores it in t 6 ; – calls itself recursively with t 6 ’s rightChild pointer and n = [ (4 – 1)/2 ] = 1.

25 Building a Min-Height Binary Search Tree Unread items in file: 60 buildTree( n = 1 ): – creates a new node, t 9, and makes t 6 ’s rightChild pointer point to it; – calls itself recursively with t 9 ’s leftChild pointer and n = [ 1/2 ] = n = 9: t n = 4: t n = 1: t 9

26 Building a Min-Height Binary Search Tree Unread items in file: n = 9: t n = 4: t n = 1: t 9 buildtree( n = 0 ): – returns immediately buildTree( n = 1 ): – reads the next item (= 60) from the input file and stores it in t 9 ; – calls itself recursively with t 9 ’s rightChild pointer and n = [ (1 – 1)/2 ] = 0.

27 Building a Min-Height Binary Search Tree No Unread items in file n = 9: t n = 4: t n = 1: t 9 buildtree( n = 0 ): – returns immediately buildTree( n = 1 ): – is done and returns buildTree( n = 4 ): – is done and returns buildTree( n = 9 ): – is done and returns, having created a minimum height binary search tree from items read from the input file.

28 Building a Min-Height Binary Search Tree buildtree is guaranteed to produce a minimum-height binary search tree. That is, – a tree with n nodes will have height  log 2 (n + 1) . Although the tree will be balanced, it will not necessarily be complete. However, searching, inserting and deleting items from a binary search tree can be efficient as long as the tree remains balanced – it is not necessary for the tree to be complete. One strategy for maintaining a balanced binary search tree is to periodically (e.g. overnight or over a weekend) dump the tree to a file and rebuild the tree. Another strategy is to maintain the tree’s balance while insertions and deletions are being done. We shall spend the remainder of the lecture considering this strategy.

Tree Intuitively, a 2-3 tree is a tree in which each parent node has either 2 or 3 children, and all leaves are at the same level. According to Donald Knuth, 2-3 trees were invented by John E. Hopcroft.

Tree: Definition Formally, T is a 2-3 tree of height h if a) T is empty (h = 0); OR b) T consists of a root and 2 subtrees, T L, T R : T L and T R are both 2-3 trees, each of height h – 1, the root contains one data item with search key S, S > each search key in T L, S < each search key in T R ; OR … S TLTL TRTR root T

Tree: Definition (Cont’d.) c) T consists of a root and 3 subtrees, T L, T M, T R : T L, T M, T R are all 2-3 trees, each of height h – 1, the root contains two data items with search keys S and L, each search key in T L < S < each search key in T M, each search key in T M < L < each search key in T R. TLTL TRTR root T TMTM S L

Tree: Example Each non-leaf has 2 or 3 children, All leaves are at the same level, and At each node, the search keys have the relationships described on the preceding viewgraphs

Tree: Efficiency Insertion and deletion can be defined so that the 2-3 tree remains balanced and its other properties are maintained. In particular, a 2-3 tree with n nodes never has a height greater than  log 2 (n + 1) , which is the same as the minimum height of a binary tree with n nodes. Consequently, finding an item in a 2-3 tree is never worse than O( log n), regardless of the insertions or deletions that were done previously.

Tree Insertion: Basic Idea 1) Find the leaf where a new item, X, should be inserted and insert it. 2) If the leaf now contains 2 items, you are done. 3) If the leaf now contains 3 items, X, Y, Z, then replace the leaf by two new nodes, n1 and n2, with the smallest of X, Y, Z going into n1, the largest going into n2, and the middle value going into the leaf’s parent node, p ; make n1 and n2 children of parent p. 4) If parent p now contains 2 items (and has 3 children) you are done. 5) If parent p now contains 3 items (and has 4 children) proceed as in step 3, except that p’s two leftmost children are attached to n1, and p’s two rightmost children are attached to n2. 6) Repeat steps 3 - 5, until arriving at a parent node containing 2 items.

Tree Insertion: Example Insert 36

Tree Insertion: Example (Cont’d.) Since the leaf with 36 in it now contains 3 items, replace the leaf by two new nodes containing 36 (the smallest) and 38 (the largest). Move 37 (the middle value) up to its parent, p. Make nodes containing 36 and 38 children of parent, p p p

Tree Insertion: Example (Cont’d.) Since node p now contains 3 items, replace p by two new nodes containing 30 (the smallest) and 39 (the largest). Since p has no parent, create a new node, r, and move 37 (the middle value) into it. Make nodes containing 30 and 39 children of r. Finally, p’s leftmost children are attached to the node containing 30; p’s rightmost children are attached to the node containing p r

Tree Insertion: Algorithm // insert newItem into 2-3 Tree, ttTree insertItem( Two3Tree ttTree, ItemType newItem ) { KeyType searchKey = getKey( newItem ); /* find the leaf, leafNode, in which searchKey belongs */ /* add newItem to leafNode */ if( /* leafNode now contains 3 items */ ) splitNode( leafNode ); }

Tree Insertion: Algorithm (Cont’d.) splitNode( TreeNode n ) { if( /* n is the root of a tree */ ) /* create a new TreeNode, p */ else /* set p to the parent of n */ /* replace node n by n1 and n2, with p as their parent; */ /* give n1 the item in n with the smallest search key; */ /* give n2 the item in n with the largest search key */ if( /* n is not a leaf */ ) { /* make n1 the parent of n’s two leftmost children */ /* make n2 the parent of n’s two rightmost children */ } /* give node p the item in n with the middle search key */ if( /* p now contains 3 items */ ) splitNode( p ); }

Tree: Remarks If a 2-3 tree needs to grow after inserting an item, it grows upwards at the root. Deletion from a 2-3 tree is a bit more complicated than insertion, involving merging nodes and redistributing items. Refer to Chapter 12 of Walls & Mirrors for details.

Tree A tree is an extension to the concept of a 2-3 tree: –each parent node has up to 4 children, –all leaves are at the same level, –each node can contain up to 3 items. Insertions and deletions in a tree require fewer steps than in a 2-3 tree.

Tree: Definition Formally, T is a tree of height h if a) T is empty (h = 0); OR b) T consists of a root and 2 subtrees, T L, T R : T L and T R are both trees, each of height h – 1, the root contains one data item with search key S, S > each search key in T L, S < each search key in T R ; OR … S TLTL TRTR root T

Tree: Definition (Cont’d.) c) T consists of a root and 3 subtrees, T L, T M, T R : T L, T M, T R are all trees, each of height h – 1, the root contains two data items with search keys S and L, each search key in T L < S < each search key in T M, each search key in T M < L < each search key in T R ; OR … TLTL TRTR root T TMTM S L

Tree: Definition (Cont’d.) d) T consists of a root and 4 subtrees, T L, T ML, T MR, T R : T L, T ML, T MR, T R are all trees, each of height h – 1, the root contains three data items with search keys S, M and L, each search key in T L < S < each search key in T ML, each search key in T ML < M < each search key in T MR, each search key in T MR < L < each search key in T R. TLTL TRTR S M L T root T ML T MR

Tree: Example each parent node has up to 4 children each node contains up to 3 items all leaves are at the same level the search keys have the relationships described on the preceding viewgraphs

Tree Insertion: Strategy When one inserts a new item into a 2-3 tree, the intended destination node is split when it overflows. Steps are then taken, recursively, to determine whether any ancestor of this node also needs to be split. While searching for the place to insert a new item into a tree, any node encountered containing 3 items (and 4 children) is immediately split before the destination node is found. This difference ensures that when the destination node in a tree is finally found, there will be room in the node for the new item, and ancestor nodes do not need to be revisited. This difference also simplifies the insertion algorithm for a tree.

Tree Insertion: Basic Idea 1) Search for the leaf where a new item, W, should be inserted. 2) If a node, n, containing 3 items, X, Y, Z, is encountered, then replace n by two new nodes, n1 and n2, with the smallest of X, Y, Z going into n1, the largest going into n2, and the middle value going into n’s parent node, p ; if p does not exist (n is also a root), create a new node p ; make n1 and n2 children of parent p. 3) If W’s search key < search key of middle( X, Y, Z ), then continue searching with node n1. Otherwise, continue searching with node n2. 4) When the destination leaf is found, it will contain 1 or 2 items. Insert W, and you are done.

Tree Insertion: Example Insert 105

Tree Insertion: Example While searching for the leaf into which 105 can be inserted, node n containing 3 items is encountered. Replace n by two new nodes, n1, containing 80 (the smallest) and n2, containing 100 (the largest). Move 90 (the middle value) up to its parent, p. Make n1 and n2 children of p n p

Tree Insertion: Example Since 105 > 90, search for the leaf into which 105 can be inserted continues with node n2. The leaf containing 110 is found and, since it contains only one item, 105 is inserted p n1n2

Tree Insertion: Example Insertion is complete without backtracking!

52 Red-Black Tree 2-3 trees have an advantage over binary search trees, since they are always balanced trees have an advantage over 2-3 trees, since, in addition to being always balanced, insertion and deletion require only one pass from the root to a leaf. However, trees require more storage than 2-3 trees due to the fact that each node must carry space for 3 items, regardless of how many items are actually stored. Red-black trees address this issue by representing trees as binary search trees, thereby only allocating space for existing data items.

53 Red-Black Tree Let all child pointers in the original tree be black. Use red child pointers to link the 2-nodes that result when 3-nodes and 4-nodes are split to form binary tree nodes. S M L abcd S L abc S L ab c M LS abcd L S bc a or  

54 Red-Black Tree: Example tree corresponding red-black tree

55 Red-Black Tree: Observations Since a red-black tree is a binary search tree, you can use the binary search tree algorithms to search and traverse it. (Ignore the color of the pointers.) Since a red-black tree represents a tree, the tree algorithms can be used to insert and delete items. The primary open issue is how to split a node in a tree when it is implemented as a red-black tree. This process is illustrated on the following viewgraphs.

56 Red-Black Tree: Splitting a Node 1. Splitting a 4-node that is a root: S M L abcd M LS abcd tree node Corresponding red-black tree nodes M LS abcd split change red pointers to black

57 Red-Black Tree: Splitting a Node (Cont’d.) 2. Splitting a 4-node with a 2-node parent: corresponding red-black tree nodes split change color of pointers M LS abcd P e tree nodes abcd S M L P e LS abcd M P e M LS abcd P e

58 Red-Black Tree: Splitting a Node (Cont’d.) 3. Splitting a 4-node with a 3-node parent: corresponding red-black tree nodes split tree nodes abcd S M Lef P Q M LS abcd P e Q f abcd ef S L M P Q M LS abcd e Q f P rotate and change color of pointers

59 Red-Black Tree: Summary Since red-black trees are always balanced, searching, inserting, and deleting an item from a tree of n nodes is never worse than O( log n ). Since inserting and deleting an item in a red-black tree frequently requires only changing the color of pointers, these operations are more time-efficient in a red-black tree than in the corresponding tree. Finally, since it is unnecessary to reserve extra space in the nodes of a red-black tree to accommodate potential, future items, red-black trees are more space-efficient than either 2-3 trees or trees.

60 AVL Tree The AVL Tree is named after its inventors, G. M. Adel’son- Vel’skii and E. M. Landis, and refers to one of the oldest methods for maintaining a balanced, binary tree. An AVL tree is a balanced, binary search tree in which: –insertions and deletions are done as in a typical, binary search tree; however, –after each insertion or deletion, a check is done to determine whether any node in the tree has left and right subtrees with heights that differ by > 1; –if so, the nodes in the tree are rearranged to restore its balance. The process of restoring the balance to an AVL tree is called a rotation.

61 AVL Tree: Restoring Balance Unbalanced binary search tree Balanced tree after one left rotation deleted edgenew edge

62 AVL Tree: Restoring Balance (Cont’d.) deleted edgenew edge Unbalanced binary search tree Balanced tree after two rotations created & deleted edge

63 AVL Tree: Remarks The height of an AVL tree with n nodes will always be close to the theoretical minimum of  log 2 (n + 1) . Consequently, search, insertion and deletion can all be done efficiently as O( log n ) operations. Also, no extra space needs to be reserved in each node for potential, future items, as in a 2-3 tree or tree. However, implementation of a tree or a red-black tree will usually be simpler than the implementation of an AVL tree.

64 General Trees General trees are similar to binary trees, except that there is no restriction on the number of children that any node may have. One way to implement a a general tree is to use the same node structure that is used for a pointer-based binary tree. Specifically, given a node n, –n’s left pointer points to its left-most child (like a binary tree) and, –n’s right pointer points to a linked list of nodes that are siblings of n (unlike a binary tree).

65 General Trees: Example Pointer-based implementation of the general tree B A EFGHI DC A HFGEI BCD A general tree

66 General Trees: Example (Cont’d.) B A EFGHI DC A F G E H B C D I Binary tree with the pointer structure of the preceding general tree

67 N-ary Trees An n-ary tree is a generalization of a binary tree, where each node can have no more than n children. Since the maximum number of children for any node is known, each parent node can point directly to each of its children -- rather than requiring a linked list. This results in a faster search time (if you know which child you want). The disadvantage of this approach is that extra space reserved in each node for n child pointers, many of which may not be used.

68 N-ary Trees: Example A HFGEI BCD An n-ary tree with n = 3 Pointer-based implementation of the n-ary tree A B EFGHI C D