DAST Tirgul 6. What is a Binary Search Tree? The keys in a binary search tree (BST) are always stored in such a way as to satisfy the search property:

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

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.
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.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
The complexity and correctness of algorithms (with binary trees as an example)
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
Binary Search Trees Comp 550.
CS202 - Fundamental Structures of Computer Science II
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.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Tirgul 5 AVL trees.
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.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
DAST 2005 Tirgul 7 Binary Search Trees. DAST 2005 Motivation We would like to have a dynamic ADT that efficiently supports the following common operations:
TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
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 ),
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
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.
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.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Binary Search Trees Algorithms and Data Structures.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.
Data Structures AVL Trees.
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)
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Binary Search Trees What is a binary search tree?
DAST Tirgul 7.
AA Trees.
Data Structures – LECTURE Balanced trees
Search Trees.
Binary Search Trees A binary search tree is a binary tree
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
CS202 - Fundamental Structures of Computer Science II
Binary Search Trees.
Chapter 10.1 Binary Search Trees
Lecture 7 Algorithm Analysis
CS200: Algorithms Analysis
Red-Black Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Ch. 12: Binary Search Trees
Lecture 7 Algorithm Analysis
Lecture 7 Algorithm Analysis
CS202 - Fundamental Structures of Computer Science II
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Binary SearchTrees [CLRS] – Chap 12.
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

DAST Tirgul 6

What is a Binary Search Tree? The keys in a binary search tree (BST) are always stored in such a way as to satisfy the search property: Let x be a node in a BST. If y is a node in the left subtree of x, then key[y]≤ key[x]. If y is a node in the right subtree of x, then key[x] < key[y]. Different BSTs can represent the same set of values. The worst-case running time for most operations in search-tree is proportional to the height of the tree.

Examples of BST A BST on 6 nodes with height 2. A less efficient BST with height 4 that contains the same keys.

BST Delete TREE_DELETE (tree T, node z) If z is a leaf we change it’s parent so it will point to null and not to z. If z has only one child x we make x the child of z’s parent instead of z. If z has 2 children we delete y the successor of z and insert it’s value into z. We can delete y since it has no left son why?

BST Delete TREE_DELETE (tree T, node z) if left(z)=null or right(z)=null –child  left(z) –If child = null child =right(z) –If z = left( parent (z) ) left( parent ( z ) )  child –Else right( parent ( z ) )  child –delete z else –y  successor(z) –value(z) = value(y) –TREE_DELETE(T,y)

defenitions d(u,v)-The distance between u and v two nodes in a tree is the number of edges in the path from u to v. a b e c d f d(a,f)=3

defenitions D(T)-The diameter of a tree d is max(d(u,v)) for all pairs of nodes u,v in the tree. a b e c d f D(T)=d(a,e)=4

Finding the diameter of a binary tree Diameter(Node n) –If n = null Return (-1,-1) –(Left_h, Left_d) = Diameter(left(n)) –(Right_h, Right_d) = Diameter(right(n)) –Return (max(Left_h, Right_h)+1, max(Right_d, Left_d, Left_h+ Right_h+2))

Finding the diameter of a binary tree a b e c d f 0,0 1, 1 4,4 2,3 0,0 3,3

Complexity Diameter(Node n) –If n = null Return (-1,-1) –(Left_h, Left_d) = Diameter(left(n)) –(Right_h, Right_d) = Diameter(right(n)) –Return (max(Left_h, Right_h)+1, max(Right_d, Left_d, Left_h+ Right_h+2) O(N) Visits every node in the tree.

Correctness proof intuition The algorithm is correct since the diameter of the tree is the maximum between: –The diameter of the right subtree. –The diameter of the left subtree. –The distance from the root to the most deep left leaf + the root to the most deep right leaf. (this is the sum of the heights of he right and left subtrees +2).

AVL tree In an AVL tree, the heights of the two child subtrees of any node differ by at most one

AVL with height Each node in the tree holds it’s height. 5/2 7/13/1 5/02/0 8/0

Finding the diameter of an AVL with height Diameter(Node n) –If n = null Return 0 –Else return (right(n).height + left(n).height+2).

Correctness proof Claim-the diameter of a tree of height h is at most 2h. Proof by induction on h: Basis: h=0 (diameter is 0). Assume for h-1 we will prove for h.

Correctness proof –If the diameter is the diameter of the right or left subtree. Than from the induction assumption we know that it is at most 2(h-1)<2h. –If the diameter is sum of the heights of the right and left subtrees +2 the diameter is at most 2h. Since the maximal height of the subtrees is h-1. D(T)=2(h-1)+2=2h.

Correctness proof We will want to show that in an AVL the sum of the heights of the right and left subtrees +2 is larger than the diameter of the right and left subtree.

Correctness proof From the claim we know that the diameter of both children is at most 2(h-1). The sum of the heights of the subtrees is at least (h-1)+(h-2). So we see that h-1+h-2+2=2h-1>2(h-1).

Unbalanced trees-Question from exercise 2 The recurrence of this algorithm is

Unbalanced trees The recurssion tree The height of the tree is log 5/4 n since it is equal to x in the equation n(4/5) x =1. why? Why not n(3/5) x =1?

Unbalanced trees

Unbalanced trees

Unbalanced trees From the induction assumption we know level l does at most cn 2 work. So level l+1 does at most cn 2 work since it contains only pairs of children of nodes from level l.

Unbalanced trees We will want to prove that T(n)=O(n 2 logn) So we will prove by induction that T(n)<dnlogn.

Unbalanced trees Basis: T(0)=O(1). We will assume for m<n and prove for n.

Unbalanced trees This is true if -dn 2 (log(5)- (9/25)log(3)-(16/25)log(4))+cn 2 <0