简体   繁体   中英

Creating a doubleTree() of a Binary Search Tree?

I came across a problem stating

 For each node in a binary search tree, 
 create a new duplicate node, and insert 
 the duplicate as the left child of the original node. 
 The resulting tree should still be a binary search tree.

http://cslibrary.stanford.edu/110/BinaryTrees.html

There is a solution there,but my solution is different.

void doubleTree(struct node* node) { 
  struct node* tempNode;

  if (node->left == NULL) 
  {
   node->left = new Node(node->data);   
  }
  else{
   tempNode = new Node(node->data);
tempNode->left = node->left;
node->left = tempNode; 
  }
}

Is this approach correct? Thanks.

The approach is correct, however, your doubleTree() doesn't take care of the word each in the problem statement. It doubles only one node.

Not is not correct. You code adds one node only to the tree. Therefore it does not double the nodes in a tree, unless that tree just happens to have only one node.

BTW I would look to solve the printing the tree problem before doing any more. How can you check your solutions to other problems are correct if you cannot print a tree?

void createDoubleTree(TreeNode t) {
        if (t == null) {
            return;
        }
        createDoubleTree(t.left);
        TreeNode temp=t.left;
        t.left=new TreeNode(t.k);
        t.left.left=temp;
        createDoubleTree(t.right);
    }
void doubleTree() // public member function of BST class
{
    doubleTree(root);
}
void doubleTree(BSTNode* s) // private member function of BST class
{
    if(s)
    {
        doubleTree(s->left);
        doubleTree(s->right);
        BSTNode *t = new BSTNode;
        t->data = s->data;
        t->left = s->left;
        t->right = 0;
        s->left = t;
    }
}

GDB Scenario: Suppose you are hired as a software engineer by a leading software company and assigned a task to store employees' records alphabetically based on employees names. Your application should allow records to be inserted, deleted, and searched by name. The company's main concern is to perform efficient searching on employees' record to check for employees' performance. For this, Binary Search is a good option. But there is a problem in BST that it does not deal with duplicate values as its formal definition says “If an element is greater than its root, it will go on its right side; and if it is less than its root, it will go on its left side”. This definition does not deal with duplicate values. But, in given scenario, as more than one employee may have the same name, so your BST should be able to deal with duplicate values too. Now consider that, you have the following options to change BST definition to accommodate duplicate values too: If an element is greater than root, it will go on its right side, and if it is less than root, it will go on its left side; otherwise, it may go on either right or left side of root. Use an array with each node to store duplicate entries.

GDB Question: Your task is to analyze costs and benefits of using above proposed alterations in BST definition with respect to time and memory for the given scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM