### Binary Tree

I am trying to learn about binay trees And I want to make the program able to balance the tee when user wants. For doing that it counts the number of nodes and creates an array to save current tree. And sort it then starts creating a new tree from middle of the array. But it doesnt save nodes into array just the first node.

If you want I can send the full code.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` `````` int func_count_node( node* p_tree) { int sum = 0; if( p_tree->p_left != NULL) { sum += func_count_node( p_tree->p_left ); } else if( p_tree->p_right != NULL) { sum += func_count_node( p_tree->p_right ); } ++sum; return sum; } node* func_balance_tree( node* p_tree, node* p_array[], int num) { if( p_tree->p_left != NULL) { func_balance_tree( p_tree->p_left, p_array, ++num ); } if( p_tree->p_left != NULL) { func_balance_tree( p_tree->p_right, p_array, ++num ); } p_array[num] = p_tree; } void sort (node* array[], int size) { for ( int i = 0; i < size; i++ ) { int index = findSmallestRemainingElement( array, size, i ); swap( array, i, index ); } } int findSmallestRemainingElement (node* array[], int size, int index) { int index_of_smallest_value = index; for (int i = index + 1; i < size; i++) { if ( array[ i ]->number < array[ index_of_smallest_value ]->number ) { index_of_smallest_value = i; } } return index_of_smallest_value; } void swap (node* array[], int first_index, int second_index) { node* temp = array[ first_index ]; array[ first_index ] = array[ second_index ]; array[ second_index ] = temp; } ``````

 ``1234567891011121314151617181920212223242526272829`` `````` if( x == 5) { sum = func_count_node(p_tree); if(sum>0) { node* p_array[sum]; p_array[sum] = new node; p_tree = func_balance_tree(p_tree, p_array, 0); sort( p_array, sum); delete p_tree; p_tree = new node; mid1 = (sum%2)-1; mid2 = mid1+1; while(mid1>=0) { p_tree = func_add_node( p_tree, p_array[mid1]->number, p_array[mid1]->name); --mid1; } while(mid2number, p_array[mid2]->name); ++mid2; } } }``````
Having to count the nodes of the tree to determine if it needs balancing is not a very efficient method because this will take time as the trees grows ( ~O(n) time ) to find all lengths. But with a well implemented binary search tree, you are able to know the height of the tree at any point by keeping track of a single variable. Balancing is then achieved by something known as rotation (avl tree) or zig, zig-zig, zig-zag(splay tree), etc for different balance trees

Wikipedia gives some information on self-balancing trees:
http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree#Implementations

I have recently completed an implementation of an avl search tree. If you are interested in taking a look at that: http://www.cplusplus.com/forum/beginner/108093/#msg588108

http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_avl.aspx
http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree
It's just as hard for us to find logical errors in your code as it is for you.

People here are most helpful when you don't know/understand something about C++, and they can give a simple answer.

Posting your code saying "it doesn't work, I don't know why" is unlikely to be fruitful unless the code is very simple, and yours isn't.

Tree balancing is a bit complicated. Do you already have an algorithm for balancing that you can translate to C++? If not, maybe read these:

http://www.mec.ac.in/resources/notes/notes/ds/bal.htm
http://courses.cs.vt.edu/~cs2604/fall05/mcpherson/note/BalancingTrees.ppt
http://www.stoimen.com/blog/2012/07/03/computer-algorithms-balancing-a-binary-search-tree/

Good luck.