Binary Search Tree 황승원 Fall 2011 CSE, POSTECH 2 2 Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than.

Slides:



Advertisements
Similar presentations
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
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.
Chapter 4: Trees Part II - AVL Tree
Trees Types and Operations
Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.
Binary Search Trees Definition Of Binary Search Tree A binary tree. Each node has a (key, value) pair. For every node x, all keys in the left subtree.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Dictionaries Collection of items. Each item is a pair.  (key, element)  Pairs have different keys.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Binary Search Trees Dictionary Operations:  IsEmpty()  Search(key)  Insert(key, value)  Delete(key)
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Binary Search Trees CSE, POSTECH. Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and.
Binary Search Trees Chapter 7 Objectives
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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Balanced Search Trees Chapter Chapter Contents AVL Trees Single Rotations Double Rotations Implementation Details 2-3 Trees Searching Adding Entries.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Tree Data Structures.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
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.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
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.
Binary Search Trees (BST)
SNU IDB Lab. Ch 15. Binary Search Trees © copyright 2006 SNU IDB Lab.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Binary Search Trees Dictionary Operations:  get(key)  put(key, value)  remove(key) Additional operations:  ascend()  get(index) (indexed binary search.
Dynamic Dictionaries Primary Operations:  get(key) => search  put(key, element) => insert  remove(key) => delete Additional operations:  ascend()
Binary Search Trees Dictionary Operations:  search(key)  insert(key, value)  delete(key) Additional operations:  ascend()  IndexSearch(index) (indexed.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
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.
Dictionaries Collection of items. Each item is a pair. (key, element)
Binary Search Trees Chapter 7 Objectives
BST Trees
Binary search tree. Removing a node
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Dynamic Dictionaries Primary Operations: Additional operations:
Digital Search Trees & Binary Tries
Binary Search Trees Dictionary Operations: Additional operations:
Binary Trees, Binary Search Trees
Binary Search Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Trees, Binary Search Trees
Binary Search Trees Chapter 7 Objectives
Lecture 9: Self Balancing Trees
Chapter 20: Binary Trees.
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.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Binary Search Tree 황승원 Fall 2011 CSE, POSTECH

2 2 Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and hashing – Particularly ideal for accessing data sequentially or by rank Types of search trees – Binary search trees – AVL trees – Red-black trees – B-trees

3 3 Binary Search Tree Definition A binary tree that may be empty. A nonempty binary search tree satisfies the following properties 1. Each node has a (key, value) pair and no two nodes have the same key (i.e., all keys are distinct). 2. For every node x, all keys in the left subtree of x are smaller than that in x. 3. For every node x, all keys in the right subtree of x are larger than that in x. 4. The left and right subtrees of the root are also binary search trees

4 Binary Search Trees Dictionary Operations:  get(key)  put(key, value)  remove(key) Additional operations:  ascend()  Elements in the ascending order  get(rank) (indexed binary search tree)  Get the element in the rank th order  remove(rank) (indexed binary search tree)  Remove the element in the rank th order

5 Complexity Of Dictionary Operations get(), put() and remove() n is number of elements in dictionary

6 Definition Of Binary Search Tree A binary tree. Each node has a (key, value) pair. For every node x, all keys in the left subtree of x are smaller than that in x. For every node x, all keys in the right subtree of x are greater than that in x.

7 Example Binary Search Tree Only keys are shown.

8 The Operation ascend() Do an inorder traversal. O(n) time.

9 The Operation get() Complexity is O(height) = O(n), where n is number of nodes/elements.

10 The Operation put() Put a pair whose key is

11 The Operation put() Put a pair whose key is

12 The Operation put() Put a pair whose key is

13 The Operation put() Complexity of put() is O(height)

14 The Operation remove() Three cases:  Element is in a leaf.  Element is in a degree 1 node.  Element is in a degree 2 node.

15 Remove From A Leaf Remove a leaf element. key =

16 Remove From A Leaf (contd.) Remove a leaf element. key =

17 Remove From A Degree 1 Node Remove from a degree 1 node. key =

Remove From A Degree 1 Node (contd.) Remove from a degree 1 node. key =

19 Remove From A Degree 2 Node Remove from a degree 2 node. key =

20 Remove From A Degree 2 Node Replace with largest key in left subtree (or smallest in right subtree)

21 Remove From A Degree 2 Node Replace with largest key in left subtree (or smallest in right subtree)

22 Remove From A Degree 2 Node Replace with largest key in left subtree (or smallest in right subtree)

23 Remove From A Degree 2 Node Largest key must be in a leaf or degree 1 node

24 Another Remove From A Degree 2 Node Remove from a degree 2 node. key =

25 Remove From A Degree 2 Node Replace with largest in left subtree

26 Remove From A Degree 2 Node Replace with largest in left subtree

27 Remove From A Degree 2 Node Replace with largest in left subtree

28 Remove From A Degree 2 Node Complexity is O(height). 35 7

29 Binary Search Trees with Duplicates We can remove the requirement that all elements in a binary search tree need distinct keys How? – Replace “smaller” in property 2 by “smaller or equal to” – Replace “larger” in property 3 by “larger or equal to” Then binary search trees can have duplicate keys

30 Indexed Binary Search Tree Binary search tree. Each node has an additional field.  leftSize = number of nodes in its left subtree

31 Example Indexed Binary Search Tree leftSize values are in red

32 leftSize And Rank Rank of an element is its position in inorder (inorder = ascending key order). [2,6,7,8,10,15,18,20,25,30,35,40] rank(2) = 0 rank(15) = 5 rank(20) = 7 leftSize(x) = rank(x) with respect to elements in subtree rooted at x

33 leftSize And Rank sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]

34 get(index) And remove(index) sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]

35 get(index) And remove(index) if index = x.leftSize desired element is x.element if index < x.leftSize desired element is index ’ th element in left subtree of x if index > x.leftSize desired element has index=(index - x.leftSize-1) in the right subtree of x

36 Challenge O(height)=O(n) In which case? How to avoid this?

37 Coming Up Next READING: Ch 15 NEXT: Balanced Binary Tree (Ch 16.1~2)