Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Tree Chapter 10.

Similar presentations


Presentation on theme: "Binary Search Tree Chapter 10."— Presentation transcript:

1 Binary Search Tree Chapter 10

2 What is BST? binary search tree is a binary tree T such that each internal node v of T stores an entry (k,x) such that: • Keys stored at nodes in the left subtree of v are less than or equal to k. • Keys stored at nodes in the right subtree of v are greater than or equal to k.

3 Searching Given a search key k and a node v of T. TreeSearch, returns a node (position) w of the subtree T(v) of T rooted at v, such that one of the following occurs: • w is an internal node and w's entry has key equal to k. • w is an external node representing k's proper place in an inorder traversal of T(v), but k is not a key contained in T(v).

4 (a) A binary search tree T representing a dictionary D with integer keys;

5 (b) nodes of T visited when executing operations find(76) (successful) and find(25) (unsuccessful) on D

6 O(h) – height of tree

7 Analysis of BST

8 Update Operation Insertion
By the BST ordering property, a new node is always inserted as a leaf node. 5 6 8 2 4 7 9 3 6 8 2 4 7 9 3 6 8 2 4 7 9 3 6 8 2 4 7 9 3 5 5 1 1 1 1 5

9 Insertion of an entry with key 78 into the search tree
Insertion of an entry with key 78 into the search tree. Finding the position to insert is shown in (a),

10 The resulting tree is shown in (b).

11 Removal There are three cases: Example: Delete 5 in the tree below:
The node to be deleted is a leaf node. The node to be deleted has one non-empty child. The node to be deleted has two non-empty children. CASE 1: DELETING A LEAF NODE Example: Delete 5 in the tree below: 7 7 15 2 4 1 8 40 6 3 9 5 Delete 5 2 4 1 15 8 40 9 3 6

12 Example: (a) The right subtree of the node x to be deleted is empty.
CASE 2: THE NODE TO BE DELETED HAS ONE NON-EMPTY CHILD (a) The right subtree of the node x to be deleted is empty. Example: target 20 35 10 8 5 22 40 3 25 6 target Delete 10 20 temp 5 35 3 8 22 40 6 25

13 (b) The left subtree of the node x to be deleted is empty.
7 15 2 4 1 8 40 6 3 12 5 target temp 14 9 7 15 2 4 1 12 40 6 3 5 target 14 9 Delete 8

14 CASE 3: DELETING A NODE THAT HAS TWO NON-EMPTY CHILDREN
DELETION BY COPYING: METHOD#1 Copy the minimum key in the right subtree of x to the node x, then delete the one-child or leaf- node with this minimum key. 7 15 2 4 1 8 40 6 3 9 5 Delete 7

15 Case 3: DELETING A NODE THAT HAS TWO NON-EMPTY CHILDREN
DELETION BY COPYING: METHOD#2 Copy the maximum key in the left subtree of x to the node x, then delete the one-child or leaf-node with this maximum key. Example: 7 15 2 4 1 8 40 6 3 9 5 Delete 7 6 15 2 4 1 8 40 5 3 9

16 Removal from the binary search tree where the entry to remove (with key 32) is stored at a node (w) with an external child: (a) before the removal; (b) after the removal.

17 Removal from the binary search tree of where the entry to remove (with key 65) is stored at a node (w) whose children are both internal: (a) before the removal; (b) after the removal.

18 BST Performance? Deletion Insertion Retrieval Data Structure O(log n)
BSTs provide good logarithmic time performance in the best and average cases. Average case complexities of using linear data structures compared to BSTs: Deletion Insertion Retrieval Data Structure O(log n) FAST BST O(n) SLOW FAST* Sorted Array Sorted Linked List *using binary search


Download ppt "Binary Search Tree Chapter 10."

Similar presentations


Ads by Google