What is the best way to create a binary tree in C language?

Asked

Viewed 286 times

6

Being a binary tree a set of records that meets certain conditions. I would like to know how best to implement a binary search tree in language C would be using a structure with head or without head? Not taking into consideration its scanning mode e-r-d , r-e-d ... I know that the address of a binary tree is the address of its root and so how should I proceed?

  • And your question is whether the use of this structure is viable or not?

  • Daviaragao What would be the best way to use this structure, with or without head. I would like to know the difference in performance between both

  • The best way in the case you presented is to create a pointer to root, which will only serve to point anyway, so it will be easy to balance the tree in case of later inserts. It is good that you know how to work with pointers, since you will move with pointer. Answered?

  • Cristopher Vidal Machado ,the part of handling pointers I already understand!! But in this case I should create a "head" for the tree or work directly with the structure without a head? Thanks guy, thank you!

1 answer

2


There are some factors that may interfere in the performance of both cases. The form of implementation and what is the content of this "head" can interfere in the analysis of the performance of the two methods of creating a binary tree. Every tree must have its root pointed so that it is not lost in memory, but not necessarily by a "head". Taking into account the same implementation for both cases, the tree that has no "head" will be accessed in a shorter time, since it has one node less in the middle of the path to access. The difference in access performance will be the access time to a node. This difference, although there will almost not be noticed, access is very fast.

  • The contents of this head could be a variable indicating the number of elements of the binary tree ! Thanks Daviaragao

  • But note that in this case a variable, other than the head of the tree would be more advantageous, because there is no need to have to navigate through this head every time just to increase this variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.