Create a Complete Binary Tree using Linked Lists w/o comparing node values

后端 未结 3 2011
心在旅途
心在旅途 2021-01-07 07:39

I am trying to create a complete binary tree using a linked list, instead of arraylist, without comparing node values. What I mean is on inserting a new value, I do not wish

3条回答
  •  感情败类
    2021-01-07 08:15

    The entire basis of a binary tree is on comparing values and sending them left or right down the tree.

    Therefore, it is impossible to create a binary tree without comparisons.

    But you did say upon insertion, so you could add the item to the end of the list, and then sort it when you call for the tree. Like this:

    list.add(value); //Just add the item to the end
    
    call(); //This method would sort the list into a tree appropriately.
    

    This option also allows you to change the type of tree dynamically, since you could add a parameter to call() that specifies a tree type.

提交回复
热议问题