Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
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.
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.
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
Trees and Red-Black Trees Gordon College Prof. Brinton.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
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.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
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.
B + -Trees (Part 1) COMP171. Slide 2 Main and secondary memories  Secondary storage device is much, much slower than the main RAM  Pages and blocks.
General Trees and Variants CPSC 335. General Trees and transformation to binary trees B-tree variants: B*, B+, prefix B+ 2-4, Horizontal-vertical, Red-black.
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
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.
Splay Trees and B-Trees
1 B-Trees Section AVL (Adelson-Velskii and Landis) Trees AVL tree is binary search tree with balance condition –To ensure depth of the tree is.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
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.
1 B Trees - Motivation Recall our discussion on AVL-trees –The maximum height of an AVL-tree with n-nodes is log 2 (n) since the branching factor (degree,
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
CSIT 402 Data Structures II
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
Data Structures CSCI 2720 Spring 2007 Balanced Trees.
COSC 2007 Data Structures II Chapter 15 External Methods.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
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.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Starting at Binary Trees
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 :
M-ary Trees. m-ary trees Some trees need to be searched efficiently, but have more than two children l parse trees l game trees l genealogical trees,
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Trees Data Structures and Algorithms (60-254). Recursive Definition of a Tree A tree T is either empty or it consists of a root and zero or more nonempty.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
CompSci Memory Model  For this course: Assume Uniform Access Time  All elements in an array accessible with same time cost  Reality is somewhat.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
AVL Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Balanced Binary Search Trees
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
AVL Search Tree put(9)
CS202 - Fundamental Structures of Computer Science II
AVL Tree By Rajanikanth B.
AVL-Trees (Part 1).
Presentation transcript:

Balanced Trees

Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes Need a structure that tends to maintain balance – How? Grow in ‘width’ first, then height Accommodate horizontal growth More data at each level Nodes of two forms –One data member and two children (“Two node”) –Two data members and three children (“Three node”)

2-3 Tree Nodes > L< S S > S< S >S <L S L

BST Insertion

2-3 Tree Insertion (39)

2-3 Tree Insertion (38)

2-3 Tree Insertion (37)

2-3 Tree Insertion (36)

2-3 Tree Insertion (36)

2-3-4 Trees, Red-Black Trees

2-3 Tree Insertion (38 – Leaf Node)

Generalized Insertion – Leaf Node P S M Le M P SeL

2-3 Tree Insertion (36 Internal Node)

2-3 Tree Insertion (36)

Generalized Insertion, Internal Node P S M Le cbad e cbad LS M P

2-3-4 Trees If allowing – 2 values/node – 3 children/node is better… What about – 3 values/node – 4 children/node ??? Definition S M L > L< S>S <M>M <L >L<S>S<L S L S >S<S – See 2-3 Tree “3-Node” …AND…

Insertion in Tree Same scheme as 2-3 Tree – Traverse down tree – If node is 4-node Split tree Insert into new tree – This means what in terms of… Growth in Height versus Width? How far ‘up’ will insertion cascade?

Insertion in Tree (20)

Insertion in Tree (70)

Generalized Insertion in Tree P S M Le cbad e cbad LS M P Splitting a 4-Node with a 2-Node Parent P S M Le cbad

Generalized Insertion in Tree Splitting a 4-Node with a 3-Node Parent f cbad P Q S M Lee cbad M P Q LSff cbad P Q S M Le

Binary Search Tree Representation Color-code node-type information – 2-Node Ordinary binary search tree node – 4-Node with values A, B, and C Center B is Black Children A and C are Red – 3-Node with values A, and B (two choices) A is Black parent, B is Red right child –or – B is Black parent, A is Red left child – Ignore color-code Structure of binary search tree Two ways to change tree – Color change – Rotations

B AC STUV Red-Black Tree Representation A B C STUV 4-Node

Red-Black Tree Representation A B STU B AU ST A SB TU 3-Node

Red-Black Tree Translation

Red-Black Tree Insertion B AC STUV A B C STUV Parent (root) is 4-Node B AC STUV B AC STUV Representation Red-Black Representation

Red-Black Tree Insertion Parent is 2-Node Representation Red-Black Representation P S M Le cbad e cbad LS M P M SL ABCD P EM SL ABCD P E

Red-Black Tree Insertion Parent is 3-Node (version 1 of 2) Representation Red-Black Representation e cbad M P Q LSf f cbad P Q S M Le M SL ABCD P Q FE M SL ABCD P Q FE

Red-Black Tree Insertion Parent is 3-Node (version 2 of 2) - Rotation M SL ABCD P Q FE M SL ABCD P Q F E f cbad P Q S M Le

Adelson-Velskii and Landis (AVL)

AVL Tree Binary tree For every node x, define its balance factor balance factor of x = height of left subtree of x - height of right subtree of x Balance factor of every node x is - 1, 0, or 1 The height of an AVL tree that has n nodes is at most 1.44 log 2 (n+2). The height of every n node binary tree is at least log 2 (n+1)

AVL Search Tree

insert(9)

insert(29) RR imbalance => new node is in right subtree of right subtree of blue node (node with bf = -2)

insert(29) RR rotation

AVL Rotations Single Rotation – RR – LL Double Rotation – RL new node in left subtree of right subtree Go right down and then go left down – LR

LL Rotation Algorithm BinaryNode LL_Rotate ( BinaryNode k2) { BinaryNode k1 = k2.left; k2.left = k1.right; k1.right = k2; return k1; }

RR Rotation Algorithm BinaryNode RR_Rotate ( BinaryNode k1 ) { BinaryNode k2 = k1.right; k1.right = k2.left; k2.left = k1; return k2; }

Single Rotation Running Example Consider the formation of an AVL tree by inserting the keys 1, 2, 3, 4, 5, 6 and 7 sequentially

Single Rotation (2) Consider the formation of an AVL tree by inserting the keys 1, 2, 3, 4, 5, 6 and 7 sequentially

Why Double Rotation? Single Rotation cannot solve LR or RL

LR Double Rotation BinaryNode LR_doubleRotate ( BinaryNode k3 ){ k3.left = RR_Rotate ( k3.left ); return LL_Rotate ( k3 ); }

LR Double Rotation Example

RL Double Rotation BinaryNode RL_doubleRotate ( BinaryNode k1 ) { k1.right = LL_Rotate ( k1.right ); return RR_Rotate ( k1 ); }

More Double Rotation Example Consider the insertion of 15, 14, 13 into the AVL tree built by inserting 1,...,7 sequentially

Double Rotation Example (2) Consider the insertion of 15,..., 10, 9, 8, and 8.5 into the AVL tree built by inserting 1,...,7 sequentially

Single Left Rotation

Double Rotation (Step One)

Double Rotation (Step Two)

B-Trees Binary tree is still not efficient enough for searching A perfectly balanced binary tree has 5 levels for 25 nodes, while a 5-ary tree has only 3 levels Especially useful for external usage

B-tree A B-tree of order m has the following properties: The data items are stored at the leaves in order The root is either a leaf or has 2 to m children All non-leaf nodes (except the root) have m/2 to m children Each non-leaf node has at most m-1 keys: key i is the data value in subtree i+1 All leaves are at the same depth and have L/2 to L items

B-tree Insertion - Split L and M are determined by the sizes of a node, the size of an item and the size of a key Requiring nodes to be half full guarantees that the B-tree will be pretty balanced and will not degenerate into a simple binary tree, or even into a linked list A B-tree of order 3 is also known as a 2-3 tree Insertion of 55 in the example B-tree causes a split into two leaves

B-tree Insertion - Cascaded Split Further insertion of 40 in the B-tree causes a split into two leaves and then a split of the parent node

B-tree Deletion Deletion is done similarly If the number of items in a leaf falls below the minimum, adopt an item from a neighboring leaf If the number of items in the neighboring leaves are also minimum, combine two leaves. Their parent will then lose a child and it may need to be combined with its neighbor This combination process should be recursively executed up the tree until: – Getting to the root – A parent has more than the minimum number of children