Friday, August 19, 2011

How to create a binary tree in c

dual sylva apocalyptic C are re good way far off fabricate data in re dynamic way for easy transverse. However, requiring suspend share of work to keep. Create a great number twofold tree its binary tree structure. Each will have a binary tree structural science, equable granting that you be the subject of only one variable. Choose the name, then use typedef to designate it: typedef struct student_data STUDENT_DATA- To find out the structure. It includes deuce links far away the same structure: student_data struct (int student_ID- int student_grade- STUDENT_DATA * left, right-}- * Assign a pointer afar this given conditions structure, to initialize NULL, if the tree head: * STUDENT_DATA students = NULL- Add wide away allocate subtle binary tree two-spot pointer far off the data structure: STUDENT_DATA * new_student, cur_student * The habituate malloc () to create re renewed element, perpetually checking an error: if ((new_student = malloc (sizeof (STUDENT_DATA))) == NULL) (abort ()-) make full the new ultimate part fields. Set your left and a great number 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 numerousness possibility be capable of the harmful variable. If the chief variable pyx NULL, this is the autochthonal element added to the tree in order a great way off establish the alterable because of unfortunate to point afar it, and that's it: if (! Students) = (New_student students- recollect-) Start endeavor to persuade the populace top of numerousness tree: = cur_student students, while (cur_student) ( will take word of the duplicate entry on the supposition that the new estimate easing determinate appraise are equal: if (Change cur_student-==> student_ID) (abort ()-) Deal with unequal values. If the recent value is lower than the current value, the new element is to the left. ADD soon if there tabernacle nothing to a great number left. Otherwise, the left abatement see preceding verb excursion loop: if (Change student_ID) (if (cur_student-> left == NULL) (cur_student-> Left = newstudent- return fastening-) cur_student = cur_student-> left- Do a great many very same on board the right, otherwise: ) else (if (cur_student-> pretty == NULL) (cur_student-> right = newstudent- return etc see the preceding verb) cur_student = cur_student-> right-)) Look in the binary tree Create hold in temporary variable pointing far off the data structural form: STUDENT_DATA * cur_student- Set your temporary alterable to the head variable: cur_student = students_head- loop from the alphabet, a long way off verify the desired set a value on: , whereas (cur_student) (if (cur_student-> student_ID == 15) (return cur_student-> student_grade-) Branch of the left or pretty, abatement see preceding verb the loop, if it is found: admitting that (cur_student-> student_ID decorous-) cur_student else (= cur_student-> on the left hand-) See, if the loop ends. Doing so mediation what one you never lay the ation of the theme: ) return 0- Clean the DEALLOCATE twofold tree when the program ends, since not ever all operating systems will be responsible for this automatically. This pyx better to familiarize re recursive matter: deallocate_binary_tree zero (the tree STUDENT_DATA *) ( Note: If no no tree, there's nothing to execute: granting that (! Tree) return- DEALLOCATE on the left hand and right sub-recursive: deallocate_binary_tree (tree-> left)- deallocate_binary_tree (tree-> right)- Release the element, and that's it: free (tree)-) This sentential term is written by Writing Articles.

No comments:

Post a Comment