Binary Search Trees 1. 50 2773 84483 7493 Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Trees Types and Operations
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.
Binary Search Trees Comp 550.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Department of Computer Science University of Maryland, College Park
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
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.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
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:
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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.
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 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Binary Search Tree Qamar Abbas.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Chapter 20 Binary Search Trees
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
10 Binary-Search-Tree Data Structure  Binary-trees and binary-search-trees  Searching  Insertion  Deletion  Traversal  Implementation of sets using.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
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.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Binary Search Trees … From
2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
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.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
Binary Search Trees Chapter 7 Objectives
Exam information in lab Tue, 18 Apr 2017, 9:00-noon programming part
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
Trees.
Chapter 16 Tree Implementations
Revised based on textbook author’s notes.
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Node Removal From BST Source:
Search Sorted Array: Binary Search Linked List: Linear Search
CMSC 341 Binary Search Trees.
CMSC 341 Binary Search Trees.
Trees.
Binary Search Tree (BST)
CMSC 341 Binary Search Trees 2/21/2006.
Chapter 20 Binary Search Trees
Presentation transcript:

Binary Search Trees 1

Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary search tree  in a binary search tree: 1. all nodes in the left subtree have data elements that are less than the data element of the root node 2. all nodes in the right subtree have data elements that are greater than the data element of the root node 3. rules 1 and 2 apply recursively to every subtree 3

right subtree (all elements > 50) left subtree (all elements < 50) 51 76

Implementing a BST  what types of data elements can a BST hold?  hint: we need to be able to perform comparisons such as less than, greater than, and equal to with the data elements 5

6 public class BinarySearchTree > { E must implement Comparable where G is either E or an ancestor of E

Implementing a BST: Nodes  we need a node class that:  has-a data element  has-a link to the left subtree  has-a link to the right subtree 7

8 public class BinarySearchTree > { private static class Node { private E data; private Node left; private Node right; /** * Create a node with the given data element. The left and right child * nodes are set to null. * data * the element to store */ public Node(E data) { this.data = data; this.left = null; this.right = null; }

Implementing a BST: Fields and Ctor  a BST has-a root node  creating an empty BST should set the root node to null 9

10 /** * The root node of the binary search tree. */ private Node root; /** * Create an empty binary search tree. */ public BinarySearchTree() { this.root = null; }

Implementing a BST: Adding elements  the definition for a BST tells you everything that you need to know to add an element  in a binary search tree: 1. all nodes in the left subtree have data elements that are less than the data element of the root node 2. all nodes in the right subtree have data elements that are greater than the data element of the root node 3. rules 1 and 2 apply recursively to every subtree 11

12 /** * Add an element to the tree. The element is inserted into the tree in a * position that preserves the definition of a binary search tree. * element * the element to add to the tree */ public void add(E element) { if (this.root == null) { this.root = new Node (element); } else { BinarySearchTree.add(element, null, this.root); // recursive static method }

13 /** * Add an element to the subtree rooted at root. The element is inserted into the tree in a * position that preserves the definition of a binary search tree. * element the element to add to the subtree parent the parent node to the subtree root the root of the subtree */ private static > void add(E element, Node parent, Node root) { if (root == null && element.compareTo(parent.data) < 0) { parent.left = new Node (element); } else if (root == null) { parent.right = new Node (element); } else if (element.compareTo(root.data) < 0) { BinarySearchTree.add(element, root, root.left); } else { BinarySearchTree.add(element, root, root.right); }

Predecessors and Successors in a BST  in a BST there is something special about a node's:  left subtree right-most child  right subtree left-most child 14

rightmost child 51 leftmost child rightmost child = inorder predecessor leftmost child = inorder successor 76 right subtree (all elements > 50) left subtree (all elements < 50)

Predecessors and Successors in a BST  in a BST there is something special about a node's:  left subtree right-most child = inorder predecessor  the node containing the largest value less than the root  right subtree left-most child = inorder successor  the node containing the smallest value greater than the root  it is easy to find the predecessor and successor nodes if you can find the nodes containing the maximum and minimum elements in a subtree 16

17 /** * Find the node in a subtree that has the smallest data element. * subtreeRoot * the root of the subtree the node in the subtree that has the smallest data element. */ public static Node minimumInSubtree(Node subtreeRoot) { if (subtreeRoot.left() == null) { return subtreeRoot; } return BinarySearchTree.minimumInSubtree(subtreeRoot.left); }

18 /** * Find the node in a subtree that has the largest data element. * subtreeRoot * the root of the subtree the node in the subtree that has the largest data element. */ public static Node maximumInSubtree(Node subtreeRoot) { if (subtreeRoot.right() == null) { return subtreeRoot; } return BinarySearchTree.maximumInSubtree(subtreeRoot.right); }

19 /** * Find the node in a subtree that is the predecessor to the root of the * subtree. If the predecessor node exists, then it is the node that has the * largest data element in the left subtree of subtreeRoot. * subtreeRoot * the root of the subtree the node in a subtree that is the predecessor to the root of the * subtree, or null if the root of the subtree has no * predecessor */ public static Node predecessorInSubtree(Node subtreeRoot) { if (subtreeRoot.left() == null) { return null; } return BinarySearchTree.maximumInSubtree(subtreeRoot.left); }

20 /** * Find the node in a subtree that is the successor to the root of the * subtree. If the successor node exists, then it is the node that has the * smallest data element in the right subtree of subtreeRoot. * subtreeRoot * the root of the subtree the node in a subtree that is the successor to the root of the * subtree, or null if the root of the subtree has no * successor */ public static Node successorInSubtree(Node subtreeRoot) { if (subtreeRoot.right() == null) { return null; } return BinarySearchTree.minimumInSubtree(subtreeRoot.right); }

Deletion from a BST  to delete a node in a BST there are 3 cases to consider: 1. deleting a leaf node 2. deleting a node with one child 3. deleting a node with two children 21

Deleting a Leaf Node  deleting a leaf node is easy because the leaf has no children  simply remove the node from the tree  e.g., delete 93 22

delete 93

Deleting a Node with One Child  deleting a node with one child is also easy because of the structure of the BST  remove the node by replacing it with its child  e.g., delete 83 25

delete 83

Deleting a Node with Two Children  deleting a node with two children is a little trickier  can you see how to do it? 28

Deleting a Node with Two Children  replace the node with its inorder predecessor OR inorder successor  call the node to be deleted Z  find the inorder predecessor OR the inorder successor  call this node Y  copy the data element of Y into the data element of Z  delete Y  e.g., delete 50 29

delete 50 using inorder predecessor

Z Y inorder predecessor to Z

copy Y data to Z data Y inorder predecessor to Z Z

Z delete Y Y

delete 50 using inorder successor

Z Y inorder successor to Z

Z Y inorder successor to Z copy Y data to Z data

Z Y delete Y