top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to delete first half or second half of the tree?

+2 votes
591 views

Your program should make N node tree into N/2 node.

Input
Ex: Tree with 15 nodes.

Output
Ex: Tree should have only 7 nodes w/o any memory leak.

Option : You can delete either first half or second half of tree.

posted Oct 23, 2013 by sivanraj

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button
first half means left part or upper part of tree.
Please give example .
ok. Anyway you have to traverse the tree with any of this in/post/pre order right such that you will be able to find middle node then delete the entire tree after middle [this can be a clue].  Is it clear now ?
So my two lines ??
and you got the answer :)
You asked So you should clear :)
I did not get you vikas.
In case of preorder , inorder and post order middle node will be different.
So in all case result will be different.
then what is the means of "first half or second half" ??
If you know the answer why did you asked the question ???

I asked the example.
Based on the traversal the middle node will be different, so i sakd to choose any one. fine. Let  take this sample

http://mathworld.wolfram.com/CompleteBinaryTree.html

delete the node from 13 o 25 as level based.
First you give  suggestion for pre/in/post order traversal.
Now you want to delete second half using level order traversal.
Please be clear yourself before asking the question.
I clearly mentioned in my first post itself that we can choose our own way/traversal as it will be a difficulty program.
Lets make it very easy.
delete any n/2 nodes from tree having n nodes.
now we end up with question. let we try for answer

Similar Questions
+1 vote

Suppose you are given a tree and asked to find out the first node at nth level. How can it be done? C code would be helpful?

+3 votes

Given a binary tree print it in inward spiral order i.e first print level 1, then level n, then level 2, then n-1 and so on.

For Ex -

         1 
   2           3 
 4    5       6     7 
8 9  10 11  12 13  14 15 

Print- 1 15 14 13 12 11 10 9 8 2 3 7 6 5 4

...