Binary search trees with equal keys Equal keys pose a problem for the implementation of binary search trees. a. What is the asymptotic performance of TREE-INSERT when used to insert n items with identical keys into an initially empty binary search tree? We propose to improve TREE-INSERT by testing before line 5 to determine whether :key D x:key and by testing before line 11 to determine whether :key D y:key. 304 Chapter 12 Binary Search Trees If equality holds, we implement one of the following strategies. For each strategy, find the asymptotic performance of inserting n items with identical keys into an initially empty binary search tree. (The strategies are described for line 5, in which we compare the keys of and x. Substitute y for x to arrive at the strategies for line 11.) b. Keep a boolean flag x:b at node x, and set x to either x:left or x:right based on the value of x:b, which alternates between FALSE and TRUE each time we visit x while inserting a node with the same key as x. c. Keep a list of nodes with equal keys at x, and insert into the list. d. Randomly set x to either x:left or x:right. (Give the worst-case performance and informally derive the expected running time.)
Read moreTable of Contents
Textbook Solutions for Introduction to Algorithms
Question
Radix trees Given two strings a D a0a1 :::ap and b D b0b1 :::bq, where each ai and each bj is in some ordered set of characters, we say that string a is lexicographically less than string b if either 1. there exists an integer j , where 0 j min.p; q/, such that ai D bi for all i D 0; 1; : : : ; j 1 and aj < bj , or 2. p< 10110 by rule 1 (letting j D 3) and 10100 < 101000 by rule 2. This ordering is similar to that used in English-language dictionaries. The radix tree data structure shown in Figure 12.5 stores the bit strings 1011, 10, 011, 100, and 0. When searching for a key a D a0a1 :::ap, we go left at a node of depth i if ai D 0 and right if ai D 1. Let S be a set of distinct bit strings whose lengths sum to n. Show how to use a radix tree to sort S lexicographically in .n/ time. For the example in Figure 12.5, the output of the sort should be the sequence 0, 011, 10, 100, 1011.
Solution
The first step in solving 12 problem number 2 trying to solve the problem we have to refer to the textbook question: Radix trees Given two strings a D a0a1 :::ap and b D b0b1 :::bq, where each ai and each bj is in some ordered set of characters, we say that string a is lexicographically less than string b if either 1. there exists an integer j , where 0 j min.p; q/, such that ai D bi for all i D 0; 1; : : : ; j 1 and aj < bj , or 2. p< 10110 by rule 1 (letting j D 3) and 10100 < 101000 by rule 2. This ordering is similar to that used in English-language dictionaries. The radix tree data structure shown in Figure 12.5 stores the bit strings 1011, 10, 011, 100, and 0. When searching for a key a D a0a1 :::ap, we go left at a node of depth i if ai D 0 and right if ai D 1. Let S be a set of distinct bit strings whose lengths sum to n. Show how to use a radix tree to sort S lexicographically in .n/ time. For the example in Figure 12.5, the output of the sort should be the sequence 0, 011, 10, 100, 1011.
From the textbook chapter Binary Search Trees you will find a few key concepts needed to solve this.
Visible to paid subscribers only
Step 3 of 7)Visible to paid subscribers only
full solution