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.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Trees Types and Operations
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS202 - Fundamental Structures of Computer Science II
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1 Red-Black Trees. 2 Black-Height of the tree = 4.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
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.
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
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.
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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 ),
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.
CSIT 402 Data Structures II
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
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.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Data Structures AVL Trees.
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.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
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.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Binary Search Trees (BST)
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 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 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.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 CMSC 341 Introduction to Trees Textbook sections:
Red-Black Trees an 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.
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.
CSE 373 Data Structures Lecture 7
Binary Search Trees What is a binary search tree?
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Data Structures – LECTURE Balanced trees
Balancing Binary Search Trees
UNIT III TREES.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
CS 201 Data Structures and Algorithms
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
AVL Trees CENG 213 Data Structures.
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Trees.
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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 subtrees T 1,T 2,…T k each of whose roots are connected by an edge from the root of T. 2 … nodes, vertices arcs, edges

The terminology of trees 3 B HGF EDC K JI A: Root of the tree B,C,D,E: Children of the root B,C,D,E: Sibblings E is an ancestor of K K is a descendant of E F,G,C,H,I,K are the leaves of the tree The depth of a node is the length of the path from the root to the node Length of path = # of edges Depth of E =1 Height of a node = length of the longest path from that node to a leaf Height of E = 2 Height of tree = height of root Height of example tree is 3 A

Applications Trees are a ubiquitous data structure in Computer Science. The Unix directory structure is a tree. Allows user to store files in a logical/organized fashion. Issues: How to implement tree structure How to insert and delete nodes in a tree structure How to find nodes in a tree structure How to traverse the entire tree structure How to organize structure for efficiency These issues will be at the core of much of the discussion of trees. 4

Binary Trees 5 A B ED C H GF Root T Right T Left A binary tree T is either empty or it consists of a root and two (possibly empty) binary subtrees T Left and T Right each of whose roots are connected by an edge from the root of T. If the left (right) binary subtree is not empty, its root is called the left (right) child of the root of the tree. The degree of a node is the number of children it has.

Problem Show by induction that the number of degree 2 nodes (n T 2) in any binary tree is 1 less than the number of leaves (n T 0). Proof: The induction is on the number of nodes n in the binary tree. For n = 1, there is only the root node with 0 children. Hence, 1 leaf node and 0 nodes of degree 2. Suppose the claim is true for all binary trees with n or less nodes. Consider a binary tree with n+1 nodes. It has one of the following structures: Let n L and n R be the number of nodes in the left and right subtrees respectively. 6

Proof - continued 7 Since n L  n and n R  n, the claim is true for the left subtree in case (i) and the right subtree in case (ii). Since in both these cases the degree 2 nodes and the leaves belong to these subtrees alone, the claim holds for the entire tree. In case (iii), the claim is true for both subtrees. If we add the root to the count of degree 2 nodes of these trees, the claim follows.

Exercise Show by induction that a binary tree with n nodes has height at least  log n . 8

Operations that we would like to do on a binary tree Find its height Find its size (number of nodes) Visit its nodes in a systematic way Etc. 9

Height of a binary tree Height(T) = 1 + max(Height(T L ),Height(T R )) Height(  ) = -1 Write a recursive function based on the above definition. height(T) if (T is empty) return –1 else return 1 + max(height(T.left),height(T.right)) 10

Size of a binary tree Size(  ) = 0 Size(T) = 1 + Size(T L ) + Size(T R ) Write a recursive function based on the above definition. size(T) if (T is empty) return 0 else return 1 + size(T.left) + size(T.right) 11

Tree traversals Many problems require that we visit the nodes of a binary tree structure in a systematic way. Three such ways are: 1.Preorder traversal 2.Postorder traversal 3.Inorder traveral 12

Preorder traversal 1.Visit the root 2.Visit the left subtree in preorder 3.Visit the right subtree in preorder 13 For example, printing the labels of the nodes in the given tree using a preorder traversal gives: A,B,D,E,C,F,G,H

Postorder traversal 1.Visit the left subtree in postorder 2.Visit the right subtree in postorder 3.Visit the root For example, printing the labels of the nodes in the given tree using a postorder traversal gives: D,E,B,F,H,G,C,A 14

Inorder traversal 1.Visit the left subtree in inorder 2.Visit the root 3.Visit the right subtree in inorder For example, printing the labels of the nodes in the given tree using an inorder traversal gives: D,B,E,A,F,C,H,G 15

Printing printPreorder(T) print T.data// visit root if (T.left is not empty) printPreorder(T.left)// visit left if (T.right is not empty) printPreorder(T.right)// visit right printPostorder(T) if (T.left is not empty) printPostorder(T.left)// visit left if (T.right is not empty) printPostorder(T.right)// visit right print T.data// visit root printInorder(T) if (T.left is not empty) printInorder(T.left)// visit left print T.data// visit root if (T.right is not empty) printInorder(T.right)// visit right 16

Heapsort We can define another sorting algorithm that uses a binary tree. Definition: An (essentially) complete binary tree is a binary tree that is complete at all levels except possibly the last. Examples: Definition: A heap is an essentially complete binary tree with the additional property: The key stored at any node is  the keys stored at its children, if any. 17

Replacing  by  Example: Array Representation of a heap: Key values placed in array level-by-level and left to right. Children of node at index i placed at indices 2i+1 and 2i+2. Given, an array of n elements, how can we heapify them? 18

Algorithm Heapify Input: Indices i and j. An array A[0..n-1] of key values. Output: A[i] sinks to appropriate place in A[i..j] Procedure Heapify(i, j) if A[i] is a leaf node then return Set A[k]  max{A[2i+1],A[2i+2]} // k is index of max. where 2i+1 and 2i+2  j if (A[i] < A[k]) then Exchange(A[i],A[k]) Heapify(k, j) 19

Example 20 Heapify(1,6): causes 10 to sink Heapify(0,6): causes 12 to sink

Example 21 Heapify(2,6): causes 12 to continue sinking

Heapify and BuildHeap Heapify(0,n-1) is O(log n) Heapify(i,n-1) only fixes element i To build the entire heap, we must apply heapify to all of the elements in turn. 22

Algorithm BuildHeap Input: An array A[0..n-1] of key values. Output: An array A[0..n-1] that satisfies the heap property. Procedure BuildHeap(A) for i  n div downto 0 Heapify(i, n-1) Initial array: 23 i: 7 div 2 – 1 = 2 BuildHeap -Heapify(2,6): no change

BuildHeap Example 24 Heapify(1,6): causes 10 to sink Heapify(2,6): causes 12 to continue sinking

Algorithm Heapsort Input: An array A[0..n-1] of key-values. Output: A sorted array A[0..n-1] of key-values. BuildHeap(A) for i  n-1 downto 1 Exchange(A[0],A[i]) Heapify(0,i-1) 25

Example 26

Example Continued 27

28

29

30

Complexity of Heapsort BuildHeap is O(n) : True but difficult to prove!! Heapify is O(log n) Heapify is called n times in heapsort. Therefore, Heapsort is O(n log n) 31

Binary Search Trees Definition: A Binary Search Tree (BST) is a binary tree that satisfies the property that for every node x in the BST, x.Key > every key in x.Left (left subtree), and x.Key < every key in x.Right (right subtree). A node of a binary search tree may be represented with 5 fields: For simplicity, we usually omit the data field in examples. Also, the parent field can often be omitted depending on the intended application and/or the particular implementation. 32

An example of a BST BST property ⇒ Inorder traversal of a BST visits the nodes in sorted order: 2,3,4,5,7,8 (ascending order) 33

1.Searching for a node with a given key, k: TreeSearch(x,k) // x is the node where search starts if (x == NULL or k == x.Key) return x else if (k < x.Key) return TreeSearch(x.Left,k) else return TreeSearch(x.Right,k) 34

2. Minimum and Maximum keys TreeMin(x) while (x.Left != NULL) x = x.Left return x; TreeMax(x) while (x.Right != NULL) x = x.Right return x; 35

3. Successor of a key Definition: The successor of a node x is the node with the smallest key greater than x.Key TreeSuccessor(x) if (x.Right != NULL) return TreeMin(x.Right) else p = x.Parent while (p != NULL and x == p.Right) x = p p = x.Parent return p 36

Example: Successor of 13? Note that 13 is the maximum key in the BST rooted at 6 37

4. Predecessor of a key Definition: The predecessor of a node x is the node with thelargest key smaller than x.Key. TreePredecessor(x) if (x.Left != NULL) return TreeMax(x.Left) else p = x.Parent while (p != NULL and x == p.Left) x = p p = x.Parent return p 38

Example: Predecessor of 13? Note that 13 is the minimum key in the BST rooted at 16 39

Insertion Node TreeInsert(x, k) if (x == NULL) Create new node t with key k else if (k < x.Key) x.Left ← TreeInsert(x.Left, k) else if (k > x.Key) x.Right ← TreeInsert(x.Right, k) else Throw exception “Existing key!” return t 40

Example: Insert key 13 into existing BST 41

Deletion Assumption: The node to be deleted is given. Case 1: Node has no children Simply remove the node. Example: Delete 13 42

Deletion Case 2: Node has one child Replace node with its child. Example: Delete 13 43

Deletion Case 3: Node has two children Replace node with minimum key m in right subtree and recursively delete m in right subtree. Example: Delete 13 44

Balanced Search Trees Definition: Binary Search Trees with a worst-case height of O(log n) are called balanced trees We can guarantee O(log n) performance for each search tree operation for balanced trees. We will discuss two kinds of balanced trees (a) AVL trees (b) Red-black trees 45

AVL Trees Named after its discoverers: Adelson-Velskii and Landis, 1962 Definition: An empty binary tree is an AVL tree. If T is a non-empty binary tree with T L and T R as its left and right sub-trees, then T is an AVL tree iff (a) T L and T R are AVL trees and (b) | h L − h R | ≤ 1 where h L and h R are the heights of T L and T R respectively. 46

AVL search tree Definition: An AVL search tree is a binary search tree that is also an AVL tree Henceforth, by an AVL tree we shall mean an AVL search tree. balance = height of left subtree – height of right subtree Node of an AVL tree: 47

Examples of AVL search trees 48 Balance factor = left balance– right balance

Height of an AVL tree Let N h be the minimum number of nodes in an AVL tree of height h. ThenN h = N h−1 + N h−2 +1, N 0 =1, N 1 =2 assuming the worst-case scenario in which one sub-tree has height h-1 and the other h-2 The solution to the recurrence given above is: N h = F h+3 -1, where F n is the n th Fibonacci number 49

50

Insertion into an AVL tree Inserting key 32 causes the tree to become unbalanced 51

Imbalance due to: Insertion into left sub-tree of left child of a node 52

Balanced by LL-rotation 53

Tree balanced by LL rotation 54

Pseudocode: LL rotation Input: Binary Node k2 Procedure withLeftChild( k2) Set k1 ← k2.Left if (k2 is Left child) k2.Parent.Left ← k1 else k2.Parent.Right ← k1 k2.Left ← k1.Right k1.Right ← k2 // Also, update Parent fields! return k1 55

Imbalance due to Insertion into right sub-tree of right child of a node 56

Balanced by RR-rotation 57

Example of RR rotation: Insert 45 Tree unbalanced after insertion 58

After the RR rotation: 59

Pseudocode: RR rotation Input: Binary Node k1 Procedure withRightChild( k1) Set k2 ← k1.Right if (k1 is Left child) k1.Parent.Left ← k2 else k2.Parent.Right ← k2 k1.Right ← k2.Left k2.Left ← k1 // Update Parent fields too! return k2 60

Imbalance caused by Insertion into right sub-tree of the left child of a node 61

Balanced by LR-rotation 62

Example of LR rotation: 63

Remark: An LR rotation is often called a double rotation because it can be viewed as an RR rotation, followed by an LL rotation On the example: After the RR-rotation 64

After the LL-rotation 65

Imbalance caused by Insertion into left sub-tree of right child Balance is restored by an RL rotation Details are the same as in the case of a LR rotation 66

An example of RL rotation: 67

After the RL rotation: 68

69

Pseudocode: LR rotation Input: Binary Node k3 Procedure doubleWithLeftChild( k3) Set k3.Left ← withRightChild(k3.Left) return withLeftChild(k3) 70

Pseudocode: RL rotation Input: Binary Node k3 Procedure doubleWithRightChild( k3) Set k3.Right ← withLeftChild(k3.Right) return withRightChild(k3) 71

Balance: 72

Balance: 73

74

Deletion from an AVL tree We delete a node just as in a binary search tree Let q be the parent of the physically deleted node. We might need to go all the way from q to the root in order to balance the tree and restore AVL-balance 75

Three cases arise: C1: Balance at q has changed to 0 ⇒ height has decreased by 1 (of the tree rooted at q) and we need to check the balance factor of the ancestors of q. C2: Balance at q has changed to +1 or –1 ⇒ height of subtrees at q is unchanged, so the balances of all the ancestors of q remain unchanged C3: Balance at q is ±2 ⇒ tree needs to be balanced at q 76

Three cases: example Case C1, delete root Case C2, delete node circled in green Case C3, delete node circled in blue 77

Types of Imbalance Let A be the first node on the path from q to the root where the balance has changed to ±2 The imbalance is of type L if deletion has taken from the left sub-tree of A R if deletion has taken from the right sub-tree of A We discuss the R-type imbalance 78

79

80

81

82

83

84

85

A Red-Black Tree 86

87

88

Important conclusion For a red-black tree with n nodes 89

Insertion into a red-black tree (Method 1) Insert node as a leaf Restore properties P1-P4, bottom-up OK? Observations Inserted node must be colored red Red-black property P3 is violated if it has a red parent 6 cases. We’ll study 3 of them: Inserted node is in the “left” subtree of G. 90

Case 1: Inserted node has a red parent, which has a black sibling Sub-case 1.1: X is an outer child 91 Single rotation restoration (LL)

Sub-case 1.2: X is an inner child Double rotation restoration (LR) 92

Case 2: Inserted node has a red parent, which has a red sibling. 93 Single rotation restoration: LL

More on Case 2 This case is problematic because: P could have a red parent, and we need to propagate the restoration up to the root 94

95

96

97

98

Top-down insertion Rule: Walk down the tree (starting from root). If we hit a node X with (two) red children, then flip the color of X and its children. Claim. If the parent of X is red, Then X’s parent’s sibling cannot be red, and the single rotation (LL) or the double rotation (LR) can be applied. 99

100

101

102

Deletion from a red-black tree Deletion is as the case of a binary search tree, but we need to guarantee that the color of the physically deleted node is red Start from the root… As we search down the tree, we maintain the invariant that the color of the current node X is red 103

104

Let X be the current node Let P be its parent Let T be its sibling Always ensure that, P is red ⇒ both X and T are black … why? Two cases: Case 1: X has 2 black children 105

Case 1.1: T has 2 black children 106

Case 1.2: T has an outer child that is red 107

Case 1.3: T has an inner red child 108

Case 2: X has a red child 109

Deletion from a red-black tree Note that the rotation is the main case when X has two black children, always when X red. When X has a red child we have to move one level down on the search path (whether the child along the search path is red, the rotation is both). Note that in Case 1.2, if both children of T are red we can still apply the case. 110