Data Structure & Algorithm 09 – Binary Search Tree JJCAO.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
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.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Search Trees Comp 550.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
Binary Search Trees CIS 606 Spring Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
6/14/2015 6:48 AM(2,4) Trees /14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
© 2004 Goodrich, Tamassia (2,4) Trees
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Fundamentals of Python: From First Programs Through Data Structures
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
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.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
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 ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 21 Binary Heap.
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.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
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.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
CS 367 Introduction to Data Structures Lecture 8.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Binary Search Trees What is a binary search tree?
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Trees, Binary Search Trees
Binary SearchTrees [CLRS] – Chap 12.
Binary Search Trees Comp 122, Spring 2004.
Binary Trees, Binary Search Trees
Presentation transcript:

Data Structure & Algorithm 09 – Binary Search Tree JJCAO

Dictionary ADT 2

Priority Queue ADT Priority Queue – a set of elements S, each with a key: insert(S,x) - insert element x into S S <- S U {x} max(S) - return element of S with largest key extract-max(S) - remove and return element of S with largest key Possible implementations: Heap 3

DynamicArray StackQueueHeap PriorityQueue HashTable Dictionary 4

(Binary) Search Trees ADT Operations: Initialize - the data structure Search - For an element with a given key Insert - a new element Delete - a specified element Predecessor - return element with closest smaller key Successor - return element with closest larger key Join – combine two dictionaries to make a larger one PrintSorted - print the dictionary in sorted order Minimum - return the element with smallest key Maximum - return the element with largest key 5 Dictionary

Binary Search Trees 6

Different binary search trees can represent the same set of values. The worst-case running time for most search-tree operations is proportional to the height of the tree. (a) A binary search tree on 6 nodes with height 2. (b) A less efficient binary search tree with height 4 that contains the same keys. 7

class Node{ int m_key; //data item (key) double m_data; //data item Node* m_leftChild; //this node’s left child Node* m_rightChild; //this node’s right child }; 8 class Tree{ private: Node* pRoot; //first node of tree public: Tree() : pRoot(NULL) { } Node* search(int key) { /*body not shown*/ } Node* min(int key) ) { /*body not shown*/ } … }; … }; Array is not work here since it may be incomplete.

BST: Search 9

Running Time: O(h) 10

Tree Walks 11

InOrder Walk An InOrder traversal of a BST prints the elements in sorted order, in O(n) time. 12

Tree Walks 13

BST: Minimum/Maximum 14

BST: Successor Running Time: O(h) 15

BST: Insert Running Time: O(h) 16

Randomly built binary search trees Theorem: If we insert n random elements into an initially empty Binary Search Tree, then the average node depth is O(lgn) Assume: Trees are formed by insertions only All input permutations are equally likely 17

BST: Delete Running Time: O(h) 18

Deletion: leaf 19

Deletion: single child 20

Deletion: two children 21

Balanced Trees ⇒ all leaves have similar depth - O(lgn) 22

Balanced Trees: Trees 2-nodes – as in a BST – hold 1 key and two children 3-nodes hold 2 keys and have 3 links to children 4-nodes hold 3 keys and have 4 links to children 23

Insert Do an unsuccessful search if search terminated at a 2-node, turn it into a 3-node if search terminated in a 3-node, turn it into a 4-node if search terminates at a 4-node, – split 4-node into one 2-node and one 3-node – pass one key back up to its parent 24

Insert - Splitting 4-nodes 25

Properties of n node trees Property 1: Worst-case Search takes O(log n) time Property 2: Worst-case for Insert makes O(log n) splits Average-case Insert seems to be < 1 split 26

Balanced Trees: Red-Black Trees RB-tree => std::set, std::map STL 源码剖析 源码之前,了无秘密 天下大事,必作于细 27

Over! 28

29 Teach yourself data structures and algorithms in 24 hours

30