Binary Search Trees ©Robert E. Tarjan 2013. Dictionary: contains a set S of items, each with associated information. Operations: Access(x): Determine.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
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.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
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.
Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.
Binary Search Trees Comp 550.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
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)
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Binary Trees Chapter 6.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
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:
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary Search Tree Qamar Abbas.
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)
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
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 (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Balanced Binary Search Trees
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary 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.
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?
Binary search tree. Removing a node
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Multiway search trees and the (2,4)-tree
Binary Search Tree Chapter 10.
Section 8.1 Trees.
Tree data structure.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Red-Black Trees Motivations
Chapter 21: Binary Trees.
Tree data structure.
Binary Search Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Red Black Trees (Guibas Sedgewick 78)
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Splay Trees Binary search trees.
Presentation transcript:

Binary Search Trees ©Robert E. Tarjan 2013

Dictionary: contains a set S of items, each with associated information. Operations: Access(x): Determine if x is in S. If so, return x’s information. Insert(x):(x not in S) Insert x and its information. Delete(x):(x in S) Delete x and its information.

Binary Search Universe of items (or of access keys or index values) is totally ordered, allowing binary comparison Binary search: Maintain S in sorted order. To find x in S: If S empty, stop (failure). If S non-empty, compare x to some item y in S. If x = y, stop (success). If x < y, search in {z in S|z < y}. If x > y, search in {z in S| z > y}.

Implementation: Binary Search Tree F M X P D B E

Binary tree: Each node x has a left child left(x) and a right child right(x), either or both of which can be null. Node x is the parent of both of its children: p(left(x)) = p(right(x)) = x. A node is binary, unary, or a leaf if it has 0, 1, or 2 null children, respectively. n = #(non-null) nodes

Binary Tree Parameters Depth (path length from top): d(root) = 0 d(left(x)) = d(right(x)) = d(x) + 1 Height (path length to bottom): h(null) = –1 h(x) = 1 + max{h(left(x)), h(right(x))} Size (number of nodes in subtree): s(null) = 0 s(x) = 1 + s(left(x)) + s(right(x))

Binary tree representation At a minimum, each node contains pointers to its left and right children. Depending on the application, each node x holds additional information, e.g. an item and its associated data; a pointer to p(x); size(x).

Binary Search Tree: Internal representation Items (key plus data) in nodes, one per node, in symmetric order (in-order): items in left subtree are less, items in right subtree are greater. To find an item takes O(d + 1) time, where d = depth of item’s node, or of null node reached by search if item is not in tree.

Binary Search Tree Internal representation F M X P D B E

Binary Search Tree: External representation Actual items (keys plus data) are in external (previously null) nodes. Internal (previously non-null) nodes hold dummy items (keys only) to support search: on equality, branch left. All items are in symmetric order. All searches stop at an external node. Twice as many nodes, but some simplifications, notably in deletion

G M Y P D B E BD EG P M Z Y Binary Search Tree External representation

Insertion (internal) Search. Replace null by node with item. Insert R F M X P D B E R

Deletion Lazy: Find item. Remove its data but leave its node (with key) so search is still possible. Eager: Find item. Remove node. Repair tree. Internal representation: If leaf, delete node (replace by null). If unary, replace by other child. If binary?

Delete E Delete X F M X P D B E R

F M P D B R

If binary, swap with successor (or predecessor). Now leaf or unary node; delete. To find successor, follow left path from right child. Delete M: Swap with P; delete. F M X R D BE P Q Y G

F P X R D BE Q Y G

Best case All leaves have depths within 1: depth  lgn . (lg: base-two logarithm) Can achieve if tree is static (insertion order chosen by implementation, no deletions) E MB FILORDTA GS Q K CJHPU

Worst case Natural but bad insertion order: sorted. Insert A, B, C, D, E, F, G,… Depth of tree is n – 1. Worst-case access cost is n. = list! A B C D E F G