1 Binary Search Trees (BSTs). 2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes.

Slides:



Advertisements
Similar presentations
Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 1 The ADT Binary Search Tree Definition The ADT Binary Search Tree is a.
Advertisements

A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Data Structures – CSC212 (1) Dr Muhammad Hussain Lecture - Binary Tree ADT Binary Tree ADT It is an ordered tree with the following properties 1.Each node.
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.
1 AVL Trees. 2 Consider a situation when data elements are inserted in a BST in sorted order: 1, 2, 3, … BST becomes a degenerate tree. Search operation.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Trees II CSC 172 SPRING 2002 LECTURE 15. Binary Trees Every binary tree has two “slots” for children It may have none, either, or both An empty (0-node)
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Binary Search Trees Chapter 6.
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.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
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.
Building Java Programs Binary Search Trees; TreeSet.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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 Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Binary Search Trees CS340. Overview of a Binary Search Tree A set of nodes T is a binary search tree if either of the following is true T is empty If.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
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
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.
Binary Search Trees Chapter 7 Objectives
Chapter 25 Binary Search Trees
BST Trees
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Binary Search Trees.
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Topic 18 Binary Search Trees
Binary Search Trees.
Topic 19 Binary Search Trees
General Trees & Binary Trees
Node Removal From BST Source:
Data Structures ADT List
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Binary Search Trees (BSTs)
Lecture 12 CS203 1.
General Trees & Binary Trees
Binary Search Trees (BSTs)
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Data Structures ADT List
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
CSC 143 Binary Search Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Search Trees (BSTs)
Presentation transcript:

1 Binary Search Trees (BSTs)

2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes O(n) time in a binary tree. In a binary tree of 10 6 nodes  10 6 steps required at least. In a BST this operation can be performed very efficiently: O(log 2 n). A binary search tree of 10 6 nodes  log 2 (10 6 )  20 steps only are required.

3 Binary Search Trees (BSTs) A binary search tree is a binary tree such that for each node, say N, the following statements are true: 1.If L is any node in the left subtree of N, then L is less than N. 2.If R is any node in the right subtree of N, then R is greater than N.

4 BST: Example

5 BST: Searching The search operation in a binary search tree can be carried out as: While (the target element is not found and there is more tree to search) do if the target element is “ less than ” the current element then search the left subtree else search the right subtree.

6 ADT Binary Search Tree Elements: The elements are nodes (BSTNode), each node contains the following data type: Type Structure: hierarchical structure; each node can have two children: left or right child; there is a root node and a current node. If N is any node in the tree, nodes in the left subtree N. Domain: the number of nodes in a BST is bounded; type/class name is BST

7 ADT Binary Search Tree Operations: 1.Method FindKey (int tkey, boolean found). results: If bst contains a node whose key value is tkey, then that node is made the current node and found is set to true; otherwise found is set to false and either the tree is empty or the current node is the node to which the node with key = tkey would be attached as a child if it were added to the BST.

8 ADT Binary Search Tree 2.Method Insert (int k, Type e, boolean inserted) requires: Full (bst) is false. input: key, e. results: if bst does not contain k then inserted is set to true and node with k and e is inserted and made the current element; otherwise inserted is set to false and current value does not change. output: inserted. 3.Method Remove_Key (int tkey, boolean removed) input: tkey results: Node with key value tkey is removed from the bst and removed set to true. If BST is not empty then root is made the current. output: removed

9 ADT Binary Search Tree 4.Method Update(Type e) requires: Empty(bst) is false. results: current node’s element is replaced with e. These operations have the same specification as ADT Binary Tree. 5.Method Traverse (Order ord) 6.Method DeleteSub ( ) 7.Method Retrieve (Type e) 8.Method Empty ( boolean empty ).

10 ADT Binary Search Tree 9.Method Full (boolean full)

11 ADT Binary Search Tree public class BSTNode { public int key; public T data; public BSTNode left, right; /** Creates a new instance of BSTNode */ public BSTNode(int k, T val) { key = k; data = val; left = right = null; }

12 ADT Binary Search Tree public class BST { BSTNode root, current; /** Creates a new instance of BST */ public BST() { root = current = null; } public boolean empty(){ return root == null ? true: false; } public T retrieve () { return current.data; }

13 ADT Binary Search Tree public boolean findkey(int tkey){ BSTNode p = root,q = root; if (empty()) return false; while (p != null){ q = p; if (p.key == tkey){ current = p; return true;} else if (tkey < p.key) p = p.left; else p = p.right } current = q; return false; }

14 ADT Binary Search Tree public boolean insert (int k, T val){ BSTNode p, q = current; if (findkey(k)){ current = q; /* findkey() has modified current */ return false; /* key already in the BST */ } p = new BSTNode (k, val); if (empty()) { root = current = p; return true;} else { /* current is pointing to parent of the new key. */ if (k < current.key) current.left = p; else current.right = p; current = p; return true;}}

15 BST Node Deletion There are three cases: –Case 1: Node to be deleted has no children. –Case 2: Node to be deleted has one child. –Case 3: Node to be deleted has two children. In all these case it is always a leaf node that gets deleted.

16 BST Deletion: Case Delete

17 BST Deletion: Case Delete 110

18 BST Deletion: Case Rightmost Node in Left Subtree Delete 60

19 ADT Binary Search Tree public boolean remove_key (int tkey){ Flag removed = new Flag(false); BSTNode p; p = remove_aux(tkey, root, removed); current = root = p; return removed; }

20 ADT Binary Search Tree private BSTNode remove_aux(int key, BSTNode p, Boolean flag) { BSTNode q, child = null; if (p == null) return null; if (key < p.key) p.left = remove_aux(key, p.left, flag); //go left else if (key > p.key) p.right = remove_aux(key, p.right, flag); //go right else { flag = true; if (p.left != null && p.right != null){ //two children q = find_min(p.right); p.key = q.key; p.data = q.data; p.right = remove_aux(q.key, p.right, flag);}

21 ADT Binary Search Tree else { if (p.right == null) //one child case child = p.left; else if (p.left == null) //one child case child = p.right; return child; } return p; }

22 ADT Binary Search Tree private BSTNode find_min(BSTNode p){ if (p == null) return null; while (p.left != null){ p = p.left; } return p; }

23 ADT Binary Search Tree public boolean update(int key, T data){ remove_key(current.key); return insert(key, data); }