Binary Search Tree Chapter 10.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Binary Search Trees
Advertisements

Chapter 12 Binary Search Trees
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
© 2004 Goodrich, Tamassia Binary Search Trees   
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
Binary Search Trees1 Part-F1 Binary Search Trees   
BST Data Structure A BST node contains: A BST contains
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
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.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Binary Search Tree Qamar Abbas.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
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.
Binary Search Trees (BST)
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
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
Part-D1 Binary Search Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 11 (continued)
Search Trees.
Binary Search Trees < > =
Trees Chapter 11 (continued)
Binary search tree. Removing a node
Binary Search Tree (BST)
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Chapter 10.1 Binary Search Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Binary Search Trees < > = Binary Search Trees
Chapter 16 Tree Implementations
Tree data structure.
Binary Trees, Binary Search Trees
Binary Search Trees.
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Copyright © Aiman Hanna All rights reserved
Binary Search Trees < > =
Tree data structure.
Binary Search Trees < > = Binary Search Trees
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Search Trees (BST)
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Chapter 12: Binary Search Trees
Binary Search Trees.
Dictionaries < > = /17/2019 4:20 PM Dictionaries
Binary Search Trees < > =
Binary Trees, Binary Search Trees
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
Binary Search Trees < > =
Chapter 20: Binary Trees.
Basic Data Structures - Trees
Search Sorted Array: Binary Search Linked List: Linear Search
1 Lecture 13 CS2013.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

Binary Search Tree Chapter 10

What is BST? binary search tree is a binary tree T such that each internal node v of T stores an entry (k,x) such that: • Keys stored at nodes in the left subtree of v are less than or equal to k. • Keys stored at nodes in the right subtree of v are greater than or equal to k.

Searching Given a search key k and a node v of T. TreeSearch, returns a node (position) w of the subtree T(v) of T rooted at v, such that one of the following occurs: • w is an internal node and w's entry has key equal to k. • w is an external node representing k's proper place in an inorder traversal of T(v), but k is not a key contained in T(v).

(a) A binary search tree T representing a dictionary D with integer keys;

(b) nodes of T visited when executing operations find(76) (successful) and find(25) (unsuccessful) on D

O(h) – height of tree

Analysis of BST

Update Operation Insertion By the BST ordering property, a new node is always inserted as a leaf node. 5 6 8 2 4 7 9 3 6 8 2 4 7 9 3 6 8 2 4 7 9 3 6 8 2 4 7 9 3 5 5 1 1 1 1 5

Insertion of an entry with key 78 into the search tree Insertion of an entry with key 78 into the search tree. Finding the position to insert is shown in (a),

The resulting tree is shown in (b).

Removal There are three cases: Example: Delete 5 in the tree below: The node to be deleted is a leaf node. The node to be deleted has one non-empty child. The node to be deleted has two non-empty children. CASE 1: DELETING A LEAF NODE Example: Delete 5 in the tree below: 7 7 15 2 4 1 8 40 6 3 9 5 Delete 5 2 4 1 15 8 40 9 3 6

Example: (a) The right subtree of the node x to be deleted is empty. CASE 2: THE NODE TO BE DELETED HAS ONE NON-EMPTY CHILD (a) The right subtree of the node x to be deleted is empty. Example: target 20 35 10 8 5 22 40 3 25 6 target Delete 10 20 temp 5 35 3 8 22 40 6 25

(b) The left subtree of the node x to be deleted is empty. 7 15 2 4 1 8 40 6 3 12 5 target temp 14 9 7 15 2 4 1 12 40 6 3 5 target 14 9 Delete 8

CASE 3: DELETING A NODE THAT HAS TWO NON-EMPTY CHILDREN DELETION BY COPYING: METHOD#1 Copy the minimum key in the right subtree of x to the node x, then delete the one-child or leaf- node with this minimum key. 7 15 2 4 1 8 40 6 3 9 5 Delete 7

Case 3: DELETING A NODE THAT HAS TWO NON-EMPTY CHILDREN DELETION BY COPYING: METHOD#2 Copy the maximum key in the left subtree of x to the node x, then delete the one-child or leaf-node with this maximum key. Example: 7 15 2 4 1 8 40 6 3 9 5 Delete 7 6 15 2 4 1 8 40 5 3 9

Removal from the binary search tree where the entry to remove (with key 32) is stored at a node (w) with an external child: (a) before the removal; (b) after the removal.

Removal from the binary search tree of where the entry to remove (with key 65) is stored at a node (w) whose children are both internal: (a) before the removal; (b) after the removal.

BST Performance? Deletion Insertion Retrieval Data Structure O(log n) BSTs provide good logarithmic time performance in the best and average cases. Average case complexities of using linear data structures compared to BSTs: Deletion Insertion Retrieval Data Structure O(log n) FAST BST O(n) SLOW FAST* Sorted Array Sorted Linked List *using binary search