Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.

Slides:



Advertisements
Similar presentations
Computer Science C++ High School Level By Guillermo Moreno.
Advertisements

Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
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 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.
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.
Recursion and Binary Tree ICS 51 – Introductory Computer Organization.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
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,
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
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.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
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.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
CHAPTER 10.1 BINARY SEARCH TREES    1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS.
Lecture 9 Binary Trees Trees General Definition Terminology
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary 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.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Binary trees יום ראשון 08 אוקטובר 2017
Binary Search Trees Chapter 7 Objectives
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Recursive Objects (Part 4)
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.
Chapter 10.1 Binary Search Trees
Section 8.1 Trees.
Trees.
Data Structures & Algorithm Design
Binary Search Trees.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Find in a linked list? first last 7  4  3  8 NULL
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Chapter 20: Binary Trees.
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Trees Trees.
Presentation transcript:

Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of Computer Science California State University, Fresno Fall 2006

Introduction to Data Structure, Fall 2006 Slide- 2 California State University, Fresno Binary Search Trees A binary search tree (BST) is a binary tree with following properties: 1.Each node has a value. 2.A total order is defined on these values. 3.The left subtree of a node contains only values less than the node's value. 4.The right subtree of a node contains only values greater than or equal to the node's value.

Introduction to Data Structure, Fall 2006 Slide- 3 California State University, Fresno Binary Search Trees Binary Search Tree 1

Introduction to Data Structure, Fall 2006 Slide- 4 California State University, Fresno Binary Search Trees

Introduction to Data Structure, Fall 2006 Slide- 5 California State University, Fresno Binary Search Tree Traversal – Inorder

Introduction to Data Structure, Fall 2006 Slide- 6 California State University, Fresno CurrentNodeAction Root = 50Compare item = 37 and < 50, move to the left subtree Node = 30Compare item = 37 and > 30, move to the right subtree Node = 35Compare item = 37 and > 35, move to the right subtree Node = 37Compare item = 37 and 37. Item found Binary Search Tree – Search

Introduction to Data Structure, Fall 2006 Slide- 7 California State University, Fresno For a tree with root “r”, to search a node with a value of “target” If(r->data == target) search is successful; Else if(target data) search in the left subtree with root “r->left”; Else search in the right subtree with root “r->right” Binary Search Tree – Search

Introduction to Data Structure, Fall 2006 Slide- 8 California State University, Fresno bool BST_Search(struct tnode* root, int target) { if(root == NULL) return FALSE; if(r->data == target) return TRUE; else if(target data) return(BST_Search(root->left, target)); else return(BST_Search(root->right, target)); } Binary Search Tree – Search

Introduction to Data Structure, Fall 2006 Slide- 9 California State University, Fresno CurrentNodeAction Root = 50Compare item = 38 and < 50, move to the left subtree Node = 30Compare item = 38 and > 30, move to the right subtree Node = 35Compare item = 38 and > 35, move to the right subtree Node = 37Compare item = 38 and 37 Got to right subtree. However, it is empty, so insert as right child! Binary Search Tree – Insertion 38

Introduction to Data Structure, Fall 2006 Slide- 10 California State University, Fresno Binary Search Tree Node – Insertion Insert 41 to the tree 41

Introduction to Data Structure, Fall 2006 Slide- 11 California State University, Fresno Binary Search Tree Node – Insertion Insert 31 to the tree How to build a BST, given the list of values?

Introduction to Data Structure, Fall 2006 Slide- 12 California State University, Fresno Binary Search Tree – Insertion For a tree with root “r”, to insert a node with a value of “target” If(r->data == target) node is already existed, return; Else if (target data) if (r->left == NULL) insert as left child of “r”; else insert in the left subtree with root “r->left”; Else if (r->right == NULL) insert as right child of “r”; else insert in the right subtree with root “r->right”

Introduction to Data Structure, Fall 2006 Slide- 13 California State University, Fresno Binary Search Tree – Insertion For a tree with root “r”, to insert a node with a value of “target” If(r == NULL) point r to the new node with value of target; Else if (target data) insert in the left subtree with root “r->left”; Else insert in the right subtree with root “r->right”

Introduction to Data Structure, Fall 2006 Slide- 14 California State University, Fresno void InsertNode(struct node **node_ptr, struct node *newNode) { struct node *node = *node_ptr; if (node == NULL) *node_ptr = newNode; else if (newNode->value value) InsertNode(&node->left, newNode); else InsertNode(&node->right, newNode); } Binary Search Tree – Insertion

Introduction to Data Structure, Fall 2006 Slide- 15 California State University, Fresno Binary Search Tree Traversal – Deletion Challenge: How to maintain the order after deletion? Bad Solution: 30 is out of place

Introduction to Data Structure, Fall 2006 Slide- 16 California State University, Fresno Binary Search Tree Traversal – Deletion Challenge: How to maintain the order after deletion? Good Solution

Introduction to Data Structure, Fall 2006 Slide- 17 California State University, Fresno Binary Search Tree Traversal – Deletion Challenge: How to maintain the order after deletion?

Introduction to Data Structure, Fall 2006 Slide- 18 California State University, Fresno Binary Search Tree Node – Deletion (1) Delete 15 from the tree For leaf nodes, simply delete it.

Introduction to Data Structure, Fall 2006 Slide- 19 California State University, Fresno Binary Search Tree Node – Deletion (2) Delete 60 from the tree 62 For internal nodes with only one child, simply replace with that child.

Introduction to Data Structure, Fall 2006 Slide- 20 California State University, Fresno Binary Search Tree Node – Deletion (3) Delete 10 from the tree 60 For internal nodes with only one child, simply replace with that child. 15

Introduction to Data Structure, Fall 2006 Slide- 21 California State University, Fresno Binary Search Tree Node – Deletion (4) Delete 50 from the tree 60 For internal nodes with two children, we can replace it with the previous in-order node. 37

Introduction to Data Structure, Fall 2006 Slide- 22 California State University, Fresno Binary Search Tree Node – Deletion (5) Delete 37 from to the tree 60 For internal nodes with two children, we can also replace it with the next in-order node

Introduction to Data Structure, Fall 2006 Slide- 23 California State University, Fresno Binary Search Tree Node – Deletion (6) Delete 25 from the tree 60 For internal nodes with only one subtree, simply replace with the root of the subtree.

Introduction to Data Structure, Fall 2006 Slide- 24 California State University, Fresno Binary Search Tree Node – Deletion (7) Delete 55 from the tree 60 For internal nodes with only one subtree, we can replace it with the root of the subtree.

Introduction to Data Structure, Fall 2006 Slide- 25 California State University, Fresno void DeleteNode(struct node*& node) { struct node*& temp = node; if (node->left == NULL) { //replace the node with the root of right subtree node = node->right; delete temp; } else if (node->right == NULL) { //left subtree is not empty but right subtree is empty // replace the node with the root of left subtree node = node->left; delete temp; } else { temp = node->left; while (temp->right != NULL) { temp = temp->right; } node->value = temp->value; delete temp; } } Binary Search Tree – Deletion

Introduction to Data Structure, Fall 2006 Slide- 26 California State University, Fresno 1. How to find the next in-order node? 2. Check if a tree is a BST. 3. How to remove the duplicates. O(nlogn) Binary Search Tree – Problems

Introduction to Data Structure, Fall 2006 Slide- 27 California State University, Fresno Binary Search Tree – Next Node (1) Node 30 For internal nodes with right subtree, return the left- most leaf node of its right subtree. 31

Introduction to Data Structure, Fall 2006 Slide- 28 California State University, Fresno Binary Search Tree – Next Node (2) Node For internal nodes with right subtree, return the left- most leaf node of its right subtree.

Introduction to Data Structure, Fall 2006 Slide- 29 California State University, Fresno Binary Search Tree – Next Node (3) Node For nodes without right subtree, if it is the left child of its parent, return its parent.

Introduction to Data Structure, Fall 2006 Slide- 30 California State University, Fresno Binary Search Tree – Next Node (4) Node For nodes without right subtree, if it is the left child of its parent, return its parent.

Introduction to Data Structure, Fall 2006 Slide- 31 California State University, Fresno Binary Search Tree – Next Node (5) Node For nodes without right subtree, if it is the right child of its parent, return the parent of its ancestor where the ancestor is the left child. If there is no such ancestor, return NULL. 40

Introduction to Data Structure, Fall 2006 Slide- 32 California State University, Fresno Binary Search Tree – Next Node (6) Node For nodes without right subtree, if it is the right child of its parent, return the parent of its ancestor where the ancestor is the left child. If there is no such ancestor, return NULL.

Introduction to Data Structure, Fall 2006 Slide- 33 California State University, Fresno Binary Search Tree – Next Node (7) Node For nodes without right subtree, if it is the right child of its parent, return the parent of its ancestor where the ancestor is the left child. If there is no such ancestor, return NULL. NULL!

Introduction to Data Structure, Fall 2006 Slide- 34 California State University, Fresno Binary Search Trees - Verification int isBST(struct node* node) { if (node==NULL) return(true); if (node->left!=NULL && maxValue(node->left) > node->data) return(false); if (node->right!=NULL && minValue(node->right) data) return(false); if (!isBST(node->left) || !isBST(node->right)) return(false); return(true); }

Introduction to Data Structure, Fall 2006 Slide- 35 California State University, Fresno Using Binary Search Trees Application: Removing Duplicates