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