Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)

Slides:



Advertisements
Similar presentations
Binary Search Tree Smt Genap
Advertisements

Trees Types and Operations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
CS 201 Data Structures and Algorithms Chapter 4: Trees (BST) Text: Read Weiss, §4.3 1Izmir University of Economics.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees, Binary Trees, and Binary Search Trees. 2 Trees * Linear access time of linked lists is prohibitive n Does there exist any simple data structure.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Review: Search Linear Search Binary Search Search demos: – ndan/dsal/appldsal.htmlhttp://
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.
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.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
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.
David Stotts Computer Science Department UNC Chapel Hill.
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.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Trees - Part II © Dave Bockus. 2 Building a Binary Search Tree h i b c e d f m k a Input: i h b m e c f a d k.
Binary Search Trees (BST)
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 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
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.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
BCA-II Data Structure Using C
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
BST Trees
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Cinda Heeren / Geoffrey Tien
Binary Search Tree (BST)
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Lecture 22 Binary Search Trees Chapter 10 of textbook
CSE 373 Data Structures Lecture 7
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Binary Search Trees.
Trees, Binary Trees, and Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CMSC 341 Binary Search Trees 11/19/2018.
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
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 &
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Presentation transcript:

Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)

Binary Trees A tree in which no node can have more than two children The depth of an “average” binary tree is considerably smaller than N, even though in the worst case, the depth can be as large as N – 1. typical binary tree Worst-case binary tree

Node Struct of Binary Tree  Possible operations on the Binary Tree ADT  Parent, left_child, right_child, sibling, root, etc  Implementation  Because a binary tree has at most two children, we can keep direct pointers to them class Tree { int key; Tree* left, right; }

Binary Search Trees (BST) A data structure for efficient searching, inser-tion and deletion (dictionary operations) All operations in worst-case O(log n) time Binary search tree property For every node x: All the keys in its left subtree are smaller than the key value in x All the keys in its right subtree are larger than the key value in x

Binary Search Trees A binary search tree Not a binary search tree Example: Tree height = 3 Key requirement of a BST: all the keys in a BST are distinct, no duplication

Binary Search Trees Average height of a binary search tree is O(log N) Maximum height of a binary search tree is O(N) (N = the number of nodes in the tree) The same set of keys may have different BSTs

Searching BST Example: Suppose T is the tree being searched: If we are searching for 15, then we are done. If we are searching for a key < 15, then we should search in the left subtree. If we are searching for a key > 15, then we should search in the right subtree.

Search (Find) Find X: return a pointer to the node that has key X, or NULL if there is no such node Tree* find(int x, Tree* t) { if (t == NULL) return NULL; else if (x key) return find(x, t->left); else if (x == t->key) return t; else return find(x, t->right); } Time complexity: O(height of the tree) = O(log N) on average. (i.e., if the tree was built using a random sequence of numbers.)

Inorder Traversal of BST Inorder traversal of BST prints out all the keys in sorted order Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20

findMin/ findMax  Goal: return the node containing the smallest (largest) key in the tree  Algorithm: Start at the root and go left (right) as long as there is a left (right) child. The stopping point is the smallest (largest) element Tree* findMin(Tree* t) { if (t==NULL)_return NULL; while (t->left != NULL) t = t->left; return t; }  Time complexity = O(height of the tree)

Insertion To insert(X):  Proceed down the tree as you would for search.  If x is found, do nothing (or update some secondary record)  Otherwise, insert X at the last spot on the path traversed  Time complexity = O(height of the tree) X = 13

Another example of insertion Example: insert(11). Show the path taken and the position at which 11 is inserted. Note: There is a unique place where a new key can be inserted.

Code for insertion Insert is a recursive (helper) function that takes a pointer to a node and inserts the key in the subtree rooted at that node. void insert(int x, Tree* & t) { if (t == NULL) t = new Tree(x, NULL, NULL); else if (x key) insert(x, t->left); else if (x > t->key) insert(x, t->right); else ; // duplicate; do nothing }

Deletion under Different Cases  Case 1: the node is a leaf  Delete it immediately  Case 2: the node has one child  Adjust a pointer from the parent to bypass that node

Deletion Case 3  Case 3: the node has 2 children  Replace the key of that node with the minimum element at the right subtree  Delete that minimum element  Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2.  Time complexity = O(height of the tree)

Code for Deletion First recall the code for findMin. Tree* findMin(Tree* t) { if (t==NULL)_return NULL; while (t->left != NULL) t = t->left; return t; }

Code for Deletion void remove(int x, BinaryTree* & t) { // remove key x from t if (t == NULL) return; // item not found; do nothing if (x key) remove(x, t->left); else if (x > t->key) remove(x, t->right); else if (t->left != NULL && t->right != NULL) { t->key = findMin(t->right)->key; remove(t->element, t->right); } else { Tree* oldNode = t; t = (t->left != NULL) ? t->left; t->right; delete oldNode; }

Summary of BST All the dictionary operations (search, insert and delete) as well as deleteMin, deleteMax etc. can be performed in O(h) time where h is the height of a binary search tree. Good news:  h is on average O(log n) (if the keys are inserted in a random order).  code for implementing dictionary operations is simple. Bad news:  worst-case is O(n).  some natural order of insertions (sorted in ascending or descending order) lead to O(n) height. (check this!) Solution:  enforce some condition on the tree structure that keeps the tree from growing unevenly.