find the th smallest
2013-09-26 01:34
197 查看
3-8. Design a data structure to support the following operations:
• insert(x,T) – Insert item x into the set T .
• delete(k,T) – Delete the kth smallest element from T.
• member(x,T) – Return true iff x ∈ T .
All operations must take O(\log n) time on an n-element set.
Solution: We use a basic binary search tree for this and add two counters which count the number of children nodes.
Here you can see how the counters in pink. Let’s look at 4. The pink 2 indicates that there are 2 nodes on the left, i.e.
smaller than 4. And the pink 3 indicates that there are 3 nodes on the right, i.e. bigger than 4. What happens if we insert an item?
We do the standard insert traversal of a binary search tree with the difference that in each node we add a one to our counter because there will be one more node if we added our new item.
You can see what happened when we added 1. Besides adding the node we increased the left counter of 2 by one and the left counter by 4 by 1 because we traveled this way. So there’s no problem on inserting new nodes with
complexity.
The next method is member which is basically search in a binary search tree which also works with a complexity of
The last one is delete(k, T) which removes the kth smallest item. Finally we can use our counters. The first counter indicates if our kth smallest item is on the left, the item or on the right.
Example 1: We want the 3th smallest item. We start at 4 and see that there are 3 items on the left, i.e. the 3th smallest item in its left children. Next we are at 2 and see that there are 1 on the left and 1 on the right, therefore we have to go right (1 left
+ item itself + 1 right item = 3). Now we have arrived 3 and there are no other child items therefore 3 is our 3th smallest item.
Example 2: We want the 4th smallest item. We start at 4 and see that there are 3 items to the left. Therefore the 4th smallest item is 4 itself.
• insert(x,T) – Insert item x into the set T .
• delete(k,T) – Delete the kth smallest element from T.
• member(x,T) – Return true iff x ∈ T .
All operations must take O(\log n) time on an n-element set.
Solution: We use a basic binary search tree for this and add two counters which count the number of children nodes.
Here you can see how the counters in pink. Let’s look at 4. The pink 2 indicates that there are 2 nodes on the left, i.e.
smaller than 4. And the pink 3 indicates that there are 3 nodes on the right, i.e. bigger than 4. What happens if we insert an item?
We do the standard insert traversal of a binary search tree with the difference that in each node we add a one to our counter because there will be one more node if we added our new item.
You can see what happened when we added 1. Besides adding the node we increased the left counter of 2 by one and the left counter by 4 by 1 because we traveled this way. So there’s no problem on inserting new nodes with
complexity.
The next method is member which is basically search in a binary search tree which also works with a complexity of
The last one is delete(k, T) which removes the kth smallest item. Finally we can use our counters. The first counter indicates if our kth smallest item is on the left, the item or on the right.
Example 1: We want the 3th smallest item. We start at 4 and see that there are 3 items on the left, i.e. the 3th smallest item in its left children. Next we are at 2 and see that there are 1 on the left and 1 on the right, therefore we have to go right (1 left
+ item itself + 1 right item = 3). Now we have arrived 3 and there are no other child items therefore 3 is our 3th smallest item.
Example 2: We want the 4th smallest item. We start at 4 and see that there are 3 items to the left. Therefore the 4th smallest item is 4 itself.
相关文章推荐
- Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- Find the k-th smallest element
- give two sorted array, find the k-th smallest element of union of A and B
- CareerCup Find the smallest range that includes at least one number from each of the k sorted lists.
- Data Structure Binary Search Tree: Find k-th smallest element in BST (Order Statistics in BST)
- Find the smallest number whose digits multiply to a given number n
- quick select to find the kth smallest element in array
- Find the k-th minimal element of an unsorted array
- Google Treasure Hunt 2008---Find the Smallest Prime Number
- leetcode :Find the contiguous subarray within an array (containing at least one number) which has th
- Google Treasure Hunt 2008---Find the Smallest Prime Number
- Find the smallest window of a certain sequence
- Find the smallest positive number missing from an unsorted array
- Find the Kth smallest element
- 在排序数组中找到第k个元素 find the k-th element in two sorted arrays
- SOJ 2868: find the smallest number
- 9.1-2 Find the second smallest of n elements.
- Find the kth smallest element in two sorted array
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.