Non-Recursive Implementation of the AVL Tree Insertion routine C Language

Source: Internet
Author: User

Finally, the Non-Recursive Implementation of the AVL Tree Insertion routine is completed.

I started to access the AVL Tree the day before yesterday. Today I used a day to complete the non-recursive insertion routine. it should be 6 hours. even if the test is correct, you are willing to review and understand it in this form. I also want to display it, because in my opinion, it is amazing to write a non-recursive insertion routine on the third day of learning the AVL Tree. if you think that I am funny, you should not blame me. You should think that I am a miserable person who has spent three days to solve this problem and show off here; if you think I'm amazing, then I'm a genius.

Note: It took about three hours to complete the process today... well, what I said yesterday is so rash, but it's a wake-up when I stay there.

The main idea is: additem () is to execute this task function. This function accepts a tree type variable and an item type variable as the parameter and returns a tree type variable.

This function needs to create a stack to store the node variables that appear on the newly added Node path, that is, position data. previously, the position * type data is saved, that is, the & scan is saved. The result is that the content in the stack changes with the scan. the choice of stack is mainly focused on the characteristics of the stack after its advanced development and its scalability. at first I thought of using arrays, but the array must be specified in advance. I hope that every function of mine will adjust itself based on its actual parameters, instead of using some constants, even constants, I don't want to use constants that have no scientific basis.

Find the parent node of the added node, and add the node to the left or right pointer of the node.

After the stack is added, It loops through the condition that the stack is not empty and checks the current content at the top of the stack. the height difference between the left and right subtree of the current node is checked. the height difference is implemented using the height_calculrecognition () function, which recursively obtains the height of the current node. I intentionally used recursion as an exercise. if it is a loop, I simply think about it and it feels a little difficult, so the recursion that I was not familiar with before the simple exercise. later I noticed that this function returns 0 if the current node is null or the current node is a leaf node when processing height. due to this defect, I added the detection for the left and right subnodes of the current node in additem (). If it is null, the height value is assigned to-1. in this way, we can handle this situation.

After recursion, the new AVL Tree of the tree type is returned.

Tree additem (TREE tree, const item) <br/>{< br/> position scan = tree; <br/> stack; <br/> stack_item temp; <br/> int lenth_left, lenth_right; </P> <p> initialize_stack (& stack); <br/> If (avltreeisempty (tree )) <br/>{< br/> tree = make_node (item); <br/> return tree; <br/>}< br/> while (scan! = NULL) <br/>{< br/>/* press the current node address into the stack */<br/> push (& stack, scan ); <br/> If (left_above_and_beyond_right (item, scan-> item )) <br/> {<br/>/* scan indicates the parent node of the new node */<br/> If (null = scan-> right) <br/> break; <br/> else <br/> scan = scan-> right; <br/>}< br/> else if (left_is_less_than_right (item, scan-> item )) <br/> {<br/>/* scan indicates the parent node of the new node */<br/> If (null = scan-> left) <br/> break; <br/> else <br/> SC An = scan-> left; <br/>}< br/>/* otherwise, the repeat operation may occur */<br/> else <br/> return tree; <br/>}< br/>/* Add the node next */<br/> If (left_above_and_beyond_right (item, scan-> item )) <br/> scan-> right = make_node (item); <br/> else if (left_is_less_than_right (item, scan-> item )) <br/> scan-> left = make_node (item); <br/>/* if a new node fails to be created */<br/> If (null = scan) <br/> return tree; <br/> while (! Stack_is_empty (& stack) <br/>{< br/> temp = Top (& stack); <br/> If (null = temp-> left) <br/> lenth_left =-1; <br/> else <br/> lenth_left = height_calculate (temp-> left ); <br/> If (null = temp-> right) <br/> lenth_right =-1; <br/> else <br/> lenth_right = height_calculate (temp-> right); <br/> If (2 = lenth_left-lenth_right) <br/>{< br/> If (left_above_and_beyond_right (item, temp-> left-> item) <br/>{< br/> If (tree = temp) <br/> tree = new_double_rotate_with_left (temp); <br/> else <br/> temp = new_double_rotate_with_left (temp ); <br/>}< br/> else <br/> {<br/> If (tree = temp) <br/> tree = single_rotate_with_left (temp ); <br/> else <br/> temp = single_rotate_with_left (temp); <br/>}< br/> If (2 = lenth_right-lenth_left) <br/>{< br/> If (left_above_and_beyond_right (item, temp-> right-> item) <br/>{< br/> If (tree = temp) <br/> tree = single_rotate_with_right (temp); <br/> else <br/> temp = single_rotate_with_right (temp ); <br/>}< br/> else <br/> {<br/> If (tree = temp) <br/> tree = new_double_rotate_with_right (temp ); <br/> else <br/> temp = new_double_rotate_with_right (temp); <br/>}< br/> POP (& stack ); <br/>}< br/> empty_the_stack (& stack); </P> <p> return tree; <br/>}

Next, describe the function used to calculate the height of the current node.

Static int height_calculate (position) <br/>{< br/> int count_left, count_right; </P> <p> count_left = count_right = 0; </P> <p> If (position! = NULL) <br/>{< br/> count_left = height_calculate (position-> left); <br/> count_right = height_calculate (position-> right ); <br/> If (position-> left! = NULL) <br/> count_left ++; <br/> If (position-> right! = NULL) <br/> count_right ++; <br/>}</P> <p> return count_left> = count_right? Count_left: count_right; <br/>}

Next is the single rotation and double rotation functions.

Static position single_rotate_with_left (position K2) <br/>{< br/> position K1; </P> <p> k1 = k2-> left; <br/> k2-> left = K1-> right; <br/> K1-> right = k2; </P> <p> k2-> Height = max (height_count (K2-> left), height_count (K2-> right) + 1; <br/> K1-> Height = max (height_count (K1-> left), K2-> height) + 1; </P> <p> return K1; <br/>}</P> <p> static position single_rotate_with_right (position K2) <br/>{< br/> position K1; </P> <p> k1 = k2-> right; <br/> k2-> right = K1-> left; <br/> K1-> left = k2; </P> <p> k2-> Height = max (height_count (K2-> left), height_count (K2-> right) + 1; <br/> K1-> Height = max (height_count (K1-> right), K2-> height) + 1; </P> <p> return K1; <br/>}</P> <p> static position double_rotate_with_left (position K3) <br/>{< br/> K3-> left = single_rotate_with_right (K3-> left); </P> <p> return single_rotate_with_left (K3 ); <br/>}</P> <p> static position double_rotate_with_right (position K3) <br/>{< br/> K3-> right = single_rotate_with_left (K3-> right); </P> <p> return single_rotate_with_right (K3); <br/>}

After that, I wrote two new double rotation functions to solve the problem of double rotation in principle. In principle, I drew a picture on my paper.

Static position new_double_rotate_with_left (position K3) <br/>{< br/> position K2, K1; </P> <p> k2 = K3-> left; <br/> k1 = k2-> right; </P> <p> k2-> right = K1-> left; <br/> K1-> left = k2; <br/> K3-> left = K1-> right; <br/> K1-> right = K3; </P> <p> return K1; <br/>}</P> <p> static position new_double_rotate_with_right (position K3) <br/>{< br/> position K2, K1; </P> <p> k2 = K3-> right; <br/> k1 = k2-> left; </P> <p> k2-> left = K1-> right; <br/> K1-> right = k2; <br/> K3-> right = K1-> left; <br/> K1-> left = K3; </P> <p> return K1; <br/>}

Finally, it is finished. I feel that I lack basic computer knowledge and working principles. that is to say, in the registers, memory between the basic commands. it is still confusing to use the function return value and pass the variable address to other functions to change the variables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.