Click here to Skip to main content
15,886,780 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I am trying to make a complete tree recursive but It randomly adds element in order. How do I convert this binary search tree to a complete tree. Thanks in advance.
Java
@Override
public boolean offer(E item) {

    if(root == null){
        root = new Node<E>(item);
        root.left = null;
        root.left = null;
    }
    Node<E> newNode = new Node<E>(item);
    insert(root, item);

    return false;
}

public void insert(Node<E> root, E value) {
    if (compare(value, root.data) > 0) {
      if (root.left != null) {
        insert(root.left, value);
      } else {
        System.out.println("Inserted " + value + " to left of "
            + root.data);
        root.left = new Node(value);
      }
    } else if (compare(value, root.data) < 0) {
      if (root.right != null) {
        insert(root.right, value);
      } else {
        System.out.println("  Inserted " + value + " to right of "
            + root.data);
        root.right = new Node(value);
      }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 3-May-14 7:06am    
I have no idea what it may possibly mean, "make a complete tree recursive". "Recursive" is the property of some algorithm, not a data structure. And what does it mean "convert a tree"? Do you mean changing your code?
—SA
Coder_Jack 3-May-14 16:41pm    
In offer method I call insert method and add a node to the tree but the problem is I couldnt find a correct algorithm to implement it as complete binary tree. It is just like binary tree not complete binary Mr. Sergey. Thank you

Please see my comment to the question. The request of converting a binary tree into a "complete tree" (you can only talk about complete binary tree) is the absurd.

There are two meanings of this term "complete binary tree", one of them is also used instead of more certain term "perfect binary tree": http://en.wikipedia.org/wiki/Binary_tree[^].

In both cases, a tree remains a binary tree; and the code is the same. Its "perfectness" or "completeness" depends in input data used to populate the tree. The algorithm remains the same. Likewise, your notion of "making a complete tree recursive" is just a misnomer. "Recursive" is a characteristic of an algorithm, not a data structure:
http://en.wikipedia.org/wiki/Recursive[^],
http://en.wikipedia.org/wiki/Recursion_%28computer_science%29[^].

Even though many data structures are themselves organically related to recursion (such as fractals, or, in some trivial way, all the tree structures, not only binary trees), saying to "make a tree recursive" is just a kind of word salad (http://en.wikipedia.org/wiki/Word_salad[^]).

—SA
 
Share this answer
 
Comments
Maciej Los 3-May-14 7:45am    
5ed!
Please, see my answer ;)
Sergey Alexandrovich Kryukov 3-May-14 19:52pm    
Thank you, Maciej.
—SA
Coder_Jack wrote:
How do I convert this binary search tree to a complete tree?


Please, correct me if i'm wrong: the code you posted doesn't contains any "search tree" algorithm.

I'd suggest to start here: An Extensive Examination of Data Structures Using C# 2.0 [^]. It might help to understand how to write algorithm to search binary tree.
 
Share this answer
 
Comments
Maciej Los 3-May-14 17:27pm    
Thank you for unknown down-voter!
Sergey Alexandrovich Kryukov 3-May-14 19:52pm    
The link is useful, the note is correct; voted 5.
—SA
Maciej Los 4-May-14 7:10am    
Thank you, Sergey ;)
BTW: Someone downvoted both answers ;(
Sergey Alexandrovich Kryukov 4-May-14 13:23pm    
You're welcome.
Well, yes, I can see 4 of -16 (down-vote by a highest reputation member) in a row in a short period of time, something I call "emotional" down-vote. It already happened before on a regular basis... Interesting approach. :-)
—SA
Maciej Los 4-May-14 14:54pm    
'"emotional" down-vote' - i need to remember it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900