Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of."— Presentation transcript:

1 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

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

3 Introduction to Data Structure, Fall 2006 Slide- 3 California State University, Fresno Binary Search Trees 65 30 37 10 25 15 Binary Search Tree 1

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

5 Introduction to Data Structure, Fall 2006 Slide- 5 California State University, Fresno Binary Search Tree Traversal – Inorder 33 32 2 52 6 66 41 94 1

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

7 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” 50 62 37 32 10 605335 25 55 30 15 Binary Search Tree – Search

8 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)); } 50 62 37 32 10 605335 25 55 30 15 Binary Search Tree – Search

9 Introduction to Data Structure, Fall 2006 Slide- 9 California State University, Fresno CurrentNodeAction Root = 50Compare item = 38 and 50 38 < 50, move to the left subtree Node = 30Compare item = 38 and 30 38 > 30, move to the right subtree Node = 35Compare item = 38 and 35 38 > 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! 50 62 37 32 10 605335 25 55 30 15 Binary Search Tree – Insertion 38

10 Introduction to Data Structure, Fall 2006 Slide- 10 California State University, Fresno Binary Search Tree Node – Insertion 50 62 37 32 10 605335 25 55 30 15 Insert 41 to the tree 41

11 Introduction to Data Structure, Fall 2006 Slide- 11 California State University, Fresno Binary Search Tree Node – Insertion 50 62 37 32 10 605335 25 55 30 15 Insert 31 to the tree 41 31 How to build a BST, given the list of values?

12 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”

13 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”

14 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

15 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? 30 65 10 37 15 Bad Solution: 30 is out of place

16 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? 65 37 10 30 15 Good Solution

17 Introduction to Data Structure, Fall 2006 Slide- 17 California State University, Fresno Binary Search Tree Traversal – Deletion 33 32 2 52 6 66 41 94 1 Challenge: How to maintain the order after deletion?

18 Introduction to Data Structure, Fall 2006 Slide- 18 California State University, Fresno Binary Search Tree Node – Deletion (1) 50 62 37 32 10 605335 25 55 30 15 Delete 15 from the tree For leaf nodes, simply delete it.

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

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

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

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

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

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

25 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

26 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

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

28 Introduction to Data Structure, Fall 2006 Slide- 28 California State University, Fresno Binary Search Tree – Next Node (2) 50 62 37 32 10 6035 25 55 30 15 Node 50 31 For internal nodes with right subtree, return the left- most leaf node of its right subtree.

29 Introduction to Data Structure, Fall 2006 Slide- 29 California State University, Fresno Binary Search Tree – Next Node (3) 50 62 37 32 10 6035 25 55 30 15 Node 25 31 For nodes without right subtree, if it is the left child of its parent, return its parent.

30 Introduction to Data Structure, Fall 2006 Slide- 30 California State University, Fresno Binary Search Tree – Next Node (4) 50 62 37 32 10 6035 25 55 30 15 Node 31 31 For nodes without right subtree, if it is the left child of its parent, return its parent.

31 Introduction to Data Structure, Fall 2006 Slide- 31 California State University, Fresno Binary Search Tree – Next Node (5) 50 62 32 10 6035 25 55 30 15 Node 35 31 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

32 Introduction to Data Structure, Fall 2006 Slide- 32 California State University, Fresno Binary Search Tree – Next Node (6) 50 62 32 10 6035 25 55 30 15 Node 35 31 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.

33 Introduction to Data Structure, Fall 2006 Slide- 33 California State University, Fresno Binary Search Tree – Next Node (7) 50 62 32 10 6035 25 55 30 15 Node 62 31 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!

34 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); }

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


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

Similar presentations


Ads by Google