Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction...
-
date post
19-Dec-2015 -
Category
Documents
-
view
219 -
download
0
Transcript of Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction...
Introduction to Data Structure, Fall 2006 Slide- 1
California State University, Fresno
Introduction to Data Structure
Chapter 10
Ming LiDepartment 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
6530
3710
25
15
Binary Search Tree 1
Introduction to Data Structure, Fall 2006 Slide- 4
California State University, Fresno
Binary Search Trees
B in ary Search T ree 2
5 3
3 0
5 9
6 2
5 0
B in ary Search T ree 3
Introduction to Data Structure, Fall 2006 Slide- 5
California State University, Fresno
Binary Search Tree Traversal – Inorder
33
322
526
6641
941
Introduction to Data Structure, Fall 2006 Slide- 6
California State University, Fresno
Current Node ActionRoot = 50 Compare item = 37 and 50
37 < 50, move to the left subtreeNode = 30 Compare item = 37 and 30
37 > 30, move to the right subtreeNode = 35 Compare item = 37 and 35
37 > 35, move to the right subtreeNode = 37 Compare item = 37 and 37. Item found.
50
62373210
60533525
5530
15
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 < r->data)search in the left subtree with root “r->left”;
Elsesearch in the right subtree with root “r->right”
50
62373210
60533525
5530
15
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 < r->data)
return(BST_Search(root->left, target));else
return(BST_Search(root->right, target));}
50
62373210
60533525
5530
15
Binary Search Tree – Search
Introduction to Data Structure, Fall 2006 Slide- 9
California State University, Fresno
Current Node ActionRoot = 50 Compare item = 38 and 50
38 < 50, move to the left subtreeNode = 30 Compare item = 38 and 30
38 > 30, move to the right subtreeNode = 35 Compare item = 38 and 35
38 > 35, move to the right subtreeNode = 37 Compare item = 38 and 37
Got to right subtree. However, it is empty, so insert as right child!
50
62373210
60533525
5530
15
Binary Search Tree – Insertion
38
Introduction to Data Structure, Fall 2006 Slide- 10
California State University, Fresno
Binary Search Tree Node – Insertion
50
62373210
60533525
5530
15
Insert 41 to the tree
41
Introduction to Data Structure, Fall 2006 Slide- 11
California State University, Fresno
Binary Search Tree Node – Insertion
50
62373210
60533525
5530
15
Insert 31 to the tree
4131
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 < r->data)
if (r->left == NULL)insert as left child of “r”;
elseinsert 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 < r->data)
insert in the left subtree with root “r->left”;
Elseinsert 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 <= node->value)InsertNode(&node->left, newNode);
elseInsertNode(&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?
6 53 0
3 71 0
2 5
1 5
\\ //
D elet e n o d e 2 5
30
6510
37
15
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?
6 53 0
3 71 0
2 5
1 5
\\ //
D elet e n o d e 2 5
65
3710
30
15
Good Solution
Introduction to Data Structure, Fall 2006 Slide- 17
California State University, Fresno
Binary Search Tree Traversal – Deletion
33
322
526
6641
941
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)
50
62373210
60533525
5530
15
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)
50
62373210
60533525
5530
15
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)
50
62373210
60533525
5530
15
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)
50
62373210
60533525
5530
15
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)
50
623210
60533525
5530
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.
375053
Introduction to Data Structure, Fall 2006 Slide- 23
California State University, Fresno
Binary Search Tree Node – Deletion (6)
50
62373210
60533525
5530
15
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)
50
62373210
603525
5530
15
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)
50
62373210
603525
5530
15
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)
50
62373210
603525
5530
15
Node 50
31
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)
50
62373210
603525
5530
15
Node 25
31
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)
50
62373210
603525
5530
15
Node 31
31
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)
50
623210
603525
5530
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
Introduction to Data Structure, Fall 2006 Slide- 32
California State University, Fresno
Binary Search Tree – Next Node (6)
50
623210
603525
5530
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.
Introduction to Data Structure, Fall 2006 Slide- 33
California State University, Fresno
Binary Search Tree – Next Node (7)
50
623210
603525
5530
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!
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) <= node->data) return(false);
if (!isBST(node->left) || !isBST(node->right)) return(false); return(true); }