Binary Search Trees (BSTs)

Slides:



Advertisements
Similar presentations
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Advertisements

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.
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.
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)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
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.
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
Binary Search Trees Chapter 7 Objectives
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.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) 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.
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."
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,
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.
CS261 Data Structures Binary Search Trees Concepts.
(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
Binary Search Trees Chapter 7 Objectives
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 25 Binary Search Trees
Trees Chapter 11 (continued)
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
BST Trees
Binary search tree. Removing a node
Binary Search Trees Chapter 7 Objectives
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
Trees.
Topic 18 Binary Search Trees
Tree data structure.
Revised based on textbook author’s notes.
Chapter 20: Binary Trees.
Binary Search Trees.
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.
Topic 19 Binary Search Trees
Chapter 21: Binary Trees.
General Trees & Binary Trees
Binary Search Tree AVL Tree
Node Removal From BST Source:
Tree data structure.
Data Structures ADT List
Find in a linked list? first last 7  4  3  8 NULL
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 Chapter 9 2/22/2019 B.Ramamurthy.
(edited by Nadia Al-Ghreimil)
Data Structures ADT List
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
CSC 143 Binary Search Trees.
Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Search Trees (BSTs)
Presentation transcript:

Binary Search Trees (BSTs)

Binary Search Trees (BSTs) 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 106 nodes  106 steps required at least. In a BST this operation can be performed very efficiently: O(log2n). A binary search tree of 106 nodes  log2(106)  20 steps only are required.

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

BST: Example. 91 65 73 53 14 35 93 50 33 40 44 56 55 58 81 80 71 87 69

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.

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 and nodes in the right subtree > N. Domain: the number of nodes in a BST is bounded; type/class name is BST

ADT Binary Search Tree Operations: 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.

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. 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

ADT Binary Search Tree 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 ( ) Method Retrieve (Type e) 8. Method Empty ( boolean empty ).

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

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

ADT Binary Search Tree public class BST <T> { BSTNode<T> 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;

ADT Binary Search Tree public boolean findkey(int tkey){ BSTNode<T> 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; }

ADT Binary Search Tree public boolean insert (int k, T val){ BSTNode<T> p, q = current; if (findkey(k)){ current = q; /* findkey() has modified current */ return false; /* key already in the BST */ } p = new BSTNode<T>(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;}}

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.

BST Deletion: Case 1 70 60 90 30 10 20 110 40 50 80 100 70 60 90 30 10 20 110 40 50 100 Delete 80

BST Deletion: Case 2 70 60 90 30 10 20 110 40 50 80 100 Delete 110 70

BST Deletion: Case 3 70 60 90 30 10 20 110 40 50 80 100 70 50 90 30 10 20 110 40 80 100 Delete 60 Rightmost Node in Left Subtree

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

ADT Binary Search Tree private BSTNode<T> remove_aux(int key, BSTNode<T> p, Boolean flag) { BSTNode<T> 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);}

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;

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

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