How to create a binary tree in C
binary trees in C are a good way to organize data in a dynamic way for easy searching. However, requiring a lot of work to keep .
Create the binary tree
its binary tree structure. Each will have a binary tree structure, even if you have only one variable. Choose the name, then use typedef to create it:
typedef struct student_data STUDENT_DATA;
To define the structure. It includes two links to the same structure:
student_data struct (int student_ID; int student_grade; STUDENT_DATA * left, right ;};
* Assign a pointer to this data structure, to initialize NULL, if the tree head:
* STUDENT_DATA students = NULL;
Add to allocate temporary binary tree
two pointer to the data structure:
STUDENT_DATA * new_student, cur_student *
The use malloc () to create a new element, always checking an error:
if ((new_student = malloc (sizeof (STUDENT_DATA))) == NULL) (abort ();)
fill the new element fields. Set your left and the right to the fields NULL:
new_student-> = Change the student_ID; new_student-> = student_size NewSize; new_student-> Left = NULL; new_student-> right = NULL;
Consider the possibility of the head variable. If the chief variable is NULL, this is the first element added to the tree in order to establish the variable from head to point to it, and that’s it:
if (! Students) = (New_student students; return;)
Start at the top of the tree:
= cur_student students, while (cur_student) (
will take care of the duplicate entry if the new value and actual value are equal:
if (Change cur_student-==> student_ID) (abort ();)
Deal with unequal values. If the new value is lower than the current value, the new element is to the left. ADD immediately if there is nothing to the left. Otherwise, the left and walk loop:
if (Change
Do the same on the right, otherwise:
) else (if (cur_student-> right == NULL) (cur_student-> right = newstudent; return 1) cur_student = cur_student-> right;))
Look in the binary tree
Create a temporary variable pointing to the data structure:
STUDENT_DATA * cur_student;
Set your temporary variable to the head variable:
cur_student = students_head;
loop through the elements, to verify the desired value:
, whereas (cur_student) (if (cur_student-> student_ID == 15) (return cur_student-> student_grade;)
Branch of the left or right, and the loop, if it is found:
if (cur_student-> student_ID <15) (cur_student = cur_student-> right;) cur_student else (= cur_student-> left;)
See, if the loop ends. Doing so means that you never found the theme:
) return 0;
Clean
the DEALLOCATE binary tree when the program ends, since not all operating systems will be responsible for this automatically. This is better to use a recursive function:
deallocate_binary_tree zero (the tree STUDENT_DATA *) (
Note: If no no tree, there’s nothing to do:
if (! Tree) return;
DEALLOCATE left and right sub-recursive:
deallocate_binary_tree (tree-> left); deallocate_binary_tree (tree-> right);
Release the element, and that’s it:
free (tree);)
Featured Links:
Proven Money Maker At $9,547 A Day.
Burn The Fat - Top Selling Fat Loss Ebook Since 2003.
Have You Ever Stayed Awake at Night Stressing About Whether or Not Your Marriage Will Last ... And What You Can Possibly Do to Save It?
Scan your computer for hidden AdWare and Spyware, Remove them permanently.
Join the Internet revolution and start downloading free movies and more!
Internet Marketer Gets $87 Million in Google Pay-Per-Click Ads FREE! ... And Makes Over $314 Million as a Result! ...And Now He's Going to Give You This Same Secret for Next to Nothing!








Leave a comment
You must be logged in to post a comment.