One more definition: A binary tree, T, is balanced if T is empty, or if abs ( height (leftsubtree of T) - height ( right subtree of T) ) <= 1 and if the.

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

1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
AVL Trees Balancing. The AVL Tree An AVL tree is a balanced binary search tree. What does it mean for a tree to be balanced? It means that for every node.
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and black antennas.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.
Balanced Search Trees AVL Trees 2-3 Trees 2-4 Trees.
CS202 - Fundamental Structures of Computer Science II
CS Data Structures Chapter 10 Search Structures (Selected Topics)
AVL Trees / Slide 1 Delete Single rotation Deletion.
C++ Programming:. Program Design Including
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
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.
ICS 220 – Data Structures and Algorithms Week 7 Dr. Ken Cosh.
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.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS Data Structures Chapter 10 Search Structures.
CSIT 402 Data Structures II
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
CS 61B Data Structures and Programming Methodology Aug 7, 2008 David Sun.
Data Structures Using Java1 Chapter 10 Binary Trees.
1 More Trees Trees, Red-Black Trees, B Trees.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
1 B+ Trees Brian Lee CS157B Section 1 Spring 2006.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Trees Chapter 15.
Search Trees.
Binary search tree. Removing a node
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Tree data structure.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
(edited by Nadia Al-Ghreimil)
Chapter 8 – Binary Search Tree
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
TCSS 342, Winter 2006 Lecture Notes
Wednesday, April 18, 2018 Announcements… For Today…
Tree data structure.
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Brian Lee CS157B Section 1 Spring 2006
CSE 373: Data Structures and Algorithms
(edited by Nadia Al-Ghreimil)
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
A Binary Tree is a tree in which each node has at most 2 children
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

One more definition: A binary tree, T, is balanced if T is empty, or if abs ( height (leftsubtree of T) - height ( right subtree of T) ) <= 1 and if the left subtree of T and the right subtree of T are balanced. That is, a binary tree, T, is balanced if for every node N of T, abs ( height (left subtree of N) - height (right subtree of N) ) <= 1

balanced Unbalanced at this node

The basic reason for using a binary search tree for storing data is to optimize search time. If a binary search tree, T, containing n nodes is full, this optimized search time is realized, and n = 2 k - 1 for some non-negative integer k The height (T) = k - 1 The tree T has k levels. The tree T has nodes at levels 0 (root node), 1, 2, 3... k - 1 (and all leaf nodes of T are at level k - 1. Solving the equation above gives k = log 2 (n + 1)

n = 31 n = 2 k - 1 k = log 2 (n + 1) = 5 The height of the tree is k - 1 = 4 All leaf nodes are at level 4.

At the opposite extreme, a binary search tree with n nodes could be a chain, or vine. A tree such as this one has n levels, and a height of n - 1 The maximum number of comparisons in a search for a particular node is n - one for each level.

The maximum number of comparisons in a search of a full binary search tree for a particular node is k = log 2 (n + 1) - one comparison at each level. The maximum number of comparisons in a search of a binary search tree which is a chain for a particular node is n - one for each level. For example if n = 262,143 If the n nodes are stores in a full binary search tree, a maximum of 18 comparisons are needed in a search for a particular node. If the n nodes are stored in a binary search tree that forms a chain, a maximum of 262,143 comparisons are needed in a search for a particular node.

Clearly a full binary search tree realizes the optimal search time. But a full tree has no room to grow, or shrink. Between these two extremes, a balanced tree or a complete tree yields close to the optimal search time, and still has room to grow.

The minimum height of a binary tree with n nodes is ceiling ( log 2 ( n + 1) ) - 1 And the number of levels is ceiling (log 2 ( n + 1) ) To show this: Let k be the smallest integer for which n <= 2 k - 1 Then 2 k < n <= 2 k - 1 Add one to all three parts of this inequality and take the log 2 of all three parts: k - 1 < log 2 ( n + 1) <= k

If the equality holds, the tree is full, and k = log 2 ( n + 1) = the number of levels height of the tree = log 2 ( n + 1) - 1 Otherwise, log 2 ( n + 1) is not an integer; round it up, and k = ceiling (log 2 ( n + 1) ) = the number of levels and ceiling (log 2 ( n + 1) ) - 1 = the height of the tree.

Suppose T is a binary search tree with 300,000 nodes having minimal height, for instance T may be a complete tree. The smallest integer, k, for which n <= 2 k - 1where n = 300,000 Is = 524, = 262,143 So 2 k < n <= 2 k - 1 And the maximum number of comparisons in a search of this binary search tree for a particular node is 19 And if T is a complete tree, there are 150,000 leaf nodes, more than 112,000 are at next lowest level so the tree can grow without degrading search times.

In practice searching a set of data occurs MUCH MORE frequently than adding a new item of data, or removing an existing item of data. The algorithm presented in the text follows the premise that whenever a node is added to, or removed from, a balanced tree, the tree is tested, and if is unbalanced, the tree is rebalanced with one or more rotation operations.

A newer algorithm that will be presented in class follows a different premise. The tree is initially built as a complete binary search tree. As nodes are added, and removed (following the algorithms illustrated in class), the tree may become closer to a chain, and further from a complete tree. Consequently the search times become degraded. A statistical utility tracks the search times, and when the average number of comparisons per search exceeds log 2 (n + 1) by some percentage, a rebalancing utility is called to reform the binary search tree to a complete binary search tree. So rebalancing occurs only when performance is suffering.

The rebalancing algorithm 1. Converts the tree to a vine. A vine is a binary tree in which the left child of every node is NULL. 2.Convert the chain to a complete binary search tree.

Step One - converting the tree to a vine: For each node, N, in the tree if N has a left child rotate N and its left child to the right (clockwise). If the left chilld of N has a right subtree, that subtree becomes the left subtree of N.

Step Two - converting the vine to a complete binary tree. The sequence { 2 k - 1: k >= 1} = { 1, 3, 7, 15, 31,... } plays an important role in this step. Let n = the number of nodes in the vine. Let k be the smallest integer so that 2 k < n <= 2 k - 1

Case I: n = 2 k the resulting complete tree will be a full tree. 1. At every second node, N, (and its parent), rotate to the left (counter clockwise). If N has a left subtree, it becomes the right subtree of N’s parent. The number of rotations = 2 k ( a value in the sequence above). 2. Repeat these rotations at every second node in the right chain for 2 k nodes (the next smaller value in the sequence above). At the last repetition, perform a single left rotation at the second node and its parent.

Case II: 2 k < n < 2 k - 1 The resulting tree will be complete, but not full. 1. Do a left rotation about every second node for a total of n - (2 k-1 - 1) nodes. This is the number of nodes that will be in the lowest level of the complete tree. The resulting chain of right children will contain 2 k nodes. 2. Apply Case I.