Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.

Slides:



Advertisements
Similar presentations
AVL Trees CSE 373 Data Structures Lecture 8. 12/26/03AVL Trees - Lecture 82 Readings Reading ›Section 4.4,
Advertisements

Chapter 12 Binary Search Trees
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.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Trees Types and Operations
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
BST Data Structure A BST node contains: A BST contains
CSE 326: Data Structures AVL Trees
B+ Trees Similar to B trees, with a few slight differences
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.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
AVL Trees ITCS6114 Algorithms and Data Structures.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Binary Search Trees Chapter 7 Objectives
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
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.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Starting at Binary 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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
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.
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.
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.
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/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Data Structures Using C++ 2E Chapter 11 Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Binary Search Trees Chapter 7 Objectives
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
UNIT III TREES.
CSIT 402 Data Structures II
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees (Chapter 4) Binary Search Trees - Review Definition
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
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
TCSS 342, Winter 2006 Lecture Notes
AVL Trees CENG 213 Data Structures.
The DSW Algorithm The building block for tree transformations in this algorithm is the rotation There are two types of rotation, left and right, which.
AVL Trees CSE 373 Data Structures.
AVL Trees (a few more slides)
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
Binary Trees.
Trees.
CSE 373 Data Structures Lecture 8
Amir Kamil 8/8/02 1 B+ Trees Similar to B trees, with a few slight differences All data is stored at the leaf nodes (leaf pages); all other nodes (index.
Presentation transcript:

Binary trees -2 Chapter 6 1

2

Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals We have the pointers reference the next node in an inorder traversal; called threads We need to know if a pointer is an actual link or a thread, so we keep a Boolean for each pointer 3

Threaded Tree Example

5 Threaded Tree Traversal Start at the leftmost node in the tree, print it, and follow its right thread – following a thread to the right, will output the node and continue to its right – following a link to the right, will go to the leftmost node, print it, and continue

6 Threaded Tree Traversal Start at leftmost node, print it Output 1

7 Threaded Tree Traversal Follow thread to right, print node Output 1 3

8 Threaded Tree Traversal Follow link to right, go to leftmost node and print Output 1 3 5

9 Threaded Tree Traversal Follow thread to right, print node Output

10 Threaded Tree Traversal Follow link to right, go to leftmost node and print Output

11 Threaded Tree Traversal Follow thread to right, print node Output

Amir Kamil8/8/0212 Threaded Tree Traversal Follow link to right, go to leftmost node and print Output

13 Threaded Tree Traversal Follow thread to right, print node Output

14 Threaded Tree Traversal Follow link to right, go to leftmost node and print Output

Threaded Tree Traversal Code 15 Node leftMost(Node n) { Node ans = n; if (ans == null) { return null; } while (ans.left != null) { ans = ans.left; } return ans; } void inOrder(Node n) { Node cur = leftmost(n); while (cur != null) { print(cur); if (cur.rightThread) { cur = cur.right; } else { cur = leftmost(cur.right); }

Traversal Through Tree Transformation (read only) Systematic visiting of all nodes in the tree. Each node visited once, does not specify order. Breadth-first traversal: visit each node, starting from the lowest level and moving down by level, visiting nodes from left to right. Depth-first traversals: go in subtree as deep as you can, backtrack. Differ on the order of visiting root, left, right subtrees. preorder: VLR. inorder: LVR. postorder: LRV. 16

Insertion searching does not modify the tree. To insert a new node with key el (new), a tree node with a dead end has to be reached, new node attached to it. found using same procedure as searching: compare key of currently scanned node el (cur) to el (new). If el (new) less than the key el (cur) try left child; otherwise try right child. If the child is empty, discontinue search and make the child point to a new node of key el. 17

18

Inserting in threaded tree 19 for inorder, only takes care of successors. Node with a right child: has its successor somewhere in the right subtree, does not need a thread. Why ? Threads are for "climbing up the tree", not for going down. A node with no right child has its successor somewhere. Inherits successor from parent. If a node becomes a left node, its parent is successor.

20

Deleting (try) 21

Balancing a tree The main idea of DSW algorithm is a rotation There are two types of rotation, left and right Rotate Right( Gr, Par, Ch ) – If Par is not the root of the tree Grandparent Gr of child Ch, becomes Ch’s parent by replacing Par; – Right subtree of Ch becomes left subtree of Ch’s parent Par; – Node Ch acquires Par as its right child 22

A picture will help 23

24

25

AVL tree The height of left and right subtree of every node differ by at most one. 26

AVL Trees27 Balanced and unbalanced BST Is this “balanced”?

AVL Trees28 Perfect Balance Want a complete tree after every operation – tree is full except possibly in the lower right This is expensive – For example, insert 2 in the tree on the left and then rebuild as a complete tree Insert 2 & complete tree

AVL Trees29 AVL - Good but not Perfect Balance AVL trees are height-balanced binary search trees Balance factor of a node – height(left subtree) - height(right subtree) An AVL tree has balance factor calculated at every node – For every node, heights of left and right subtree can differ by no more than 1 – Store current heights in each node

AVL Trees30 Node Heights height of node = h balance factor = h left -h right empty height = height=2 BF=1-0= Tree A (AVL)Tree B (AVL)

AVL Trees31 Node Heights after Insert height of node = h balance factor = h left -h right empty height = balance factor 1-(-1) = 2 Tree A (AVL)Tree B (not AVL)

AVL Trees32 Insert and Rotation in AVL Trees Insert operation may cause balance factor to become 2 or –2 for some node – only nodes on the path from insertion point to root node have possibly changed in height – So after the Insert, go back up to the root node by node, updating heights – If a new balance factor (the difference h left -h right ) is 2 or –2, adjust tree by rotation around the node

AVL Trees33 Single Rotation in an AVL Tree

AVL Trees34 Let the node that needs rebalancing be . There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of . The rebalancing is performed through four separate rotation algorithms. Insertions in AVL Trees

AVL Trees35 j k XY Z Consider a valid AVL subtree AVL Insertion: Outside Case h h h

AVL Trees36 j k X Y Z Inserting into X destroys the AVL property at node j AVL Insertion: Outside Case h h+1h

AVL Trees37 j k X Y Z Do a “right rotation” AVL Insertion: Outside Case h h+1h

AVL Trees38 j k X Y Z Do a “right rotation” Single right rotation h h+1h

AVL Trees39 j k X Y Z “Right rotation” done! (“Left rotation” is mirror symmetric) Outside Case Completed AVL property has been restored! h h+1 h

AVL Trees40 j k XY Z AVL Insertion: Inside Case Consider a valid AVL subtree h h h

AVL Trees41 Inserting into Y destroys the AVL property at node j j k X Y Z AVL Insertion: Inside Case Does “right rotation” restore balance? h h+1h

AVL Trees42 j k X Y Z “Right rotation” does not restore balance… now k is out of balance AVL Insertion: Inside Case h h+1 h

AVL Trees43 Consider the structure of subtree Y… j k X Y Z AVL Insertion: Inside Case h h+1h

AVL Trees44 j k X V Z W i Y = node i and subtrees V and W AVL Insertion: Inside Case h h+1h h or h-1

AVL Trees45 j k X V Z W i AVL Insertion: Inside Case We will do a left-right “double rotation”...

AVL Trees46 j k X V Z W i Double rotation : first rotation left rotation complete

AVL Trees47 j k X V Z W i Double rotation : second rotation Now do a right rotation

AVL Trees48 j k X V Z W i Double rotation : second rotation right rotation complete Balance has been restored h h h or h-1

AVL Trees49 Implementation balance (1,0,-1) key right left No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you don’t perform rotations Once you have performed a rotation (single or double) you won’t need to go back up the tree

AVL Trees50 Example of Insertions in an AVL Tree Insert 5, 40

AVL Trees51 Example of Insertions in an AVL Tree Now Insert 45

AVL Trees52 Single rotation (outside case) Imbalance Now Insert 34

AVL Trees53 Double rotation (inside case) Imbalance Insertion of

Heap A heap is a binary tree T that stores a key- element pairs at its internal nodes. Properties : Min Heap: key(parent) < key(child) Max Heap: key(parent) > key(child)] 54

Example 55

Example of a complete binary max- heap if B is a child node of A, then key(A) ≥ key(B). This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap. Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a min-heap. 56

57

58

59

Max heap Terminate heap when – reach root – key parent is greater than key child 60

61

62

63

64

6.8 and 6.10 no need 65