Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the input and output of AVL Tree data structure

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to realize the input and output of AVL tree data structure". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

AVL tree (balanced binary tree):

AVL tree is essentially a binary search tree, but it also has the following characteristics: it is an empty tree or the absolute value of the height difference between its left and right subtrees is not more than 1, and the left and right subtrees are both balanced binary trees. The maximum height difference between two subtrees of any node in an AVL tree is one, so it is also called a balanced binary tree. The following is an example of a comparison between balanced and unbalanced binary trees:

Balance factor (bf): the depth of the left subtree of the node minus the depth of the right subtree, then it is obvious that-1 height;} / single rotation right-handed AvlTree singleRotateWithRight (AvlTree T) {AvlTree L = T-> massipLeft; T-> m_pLeft = L-> massipRightL; L-> m_pRight = T; T-> height = Max (Height (T-> m_pLeft), Height (T-> m_pRight)) + 1 L-> height = Max (Height (L-> m_pLeft), Height (L-> m_pRight)) + 1; return L; / / at this time L becomes the root node (see the right-handed figure of the insertion of AVL)} / / single rotation left-handed AvlTree singleRotateWithLeft (AvlTree T) {AvlTree R = T-> AvlTree pRightT; T-> m_pRight = R-> mroompLeft; R-> m_pLeft = T T-> height = Max (Height (T-> m_pLeft), Height (T-> m_pRight)) + 1; R-> height = Max (Height (R-> m_pLeft), Height (R-> m_pRight)) + 1; return R / / at this time R becomes the root node (see the left-left case of AVL insertion)} / / A pair of rotation, first left then right AvlTree doubleRotateWithLeft (AvlTree T) / / first left then right {T-> m_pLeft = singleRotateWithLeft (T-> m_pLeft); return singleRotateWithRight (T);} / / A pair of rotation, first right then left AvlTree doubleRotateWithRight (AvlTree T) / / first right then left {T-> m_pRight = singleRotateWithRight (T-> m_pRight) Return singleRotateWithLeft (T);} AvlTree AvlTreeInsert (AvlTree T, DataType x) {if (T = = NULL) / / if the tree is empty {T = (AvlNode *) malloc (sizeof (struct AvlNode)); if (T) {T-> data = x; T-> m_pLeft = NULL; T-> m_pRight = NULL; T-> height = 0 } else {cout m_pLeft = AvlTreeInsert (T-> mroompLeftscox); / / insert and then rotate if (Height (T-> m_pLeft)-Height (T-> m_pRight) = = 2) / / only this {if (x) is possible.

< T->

Data) / / left and left case, only need to rotate right {T = singleRotateWithRight (T);} else / / left and right, double rotation, first left {T = doubleRotateWithLeft (T) } else if (x > T-> data) {T-> m_pRight = AvlTreeInsert (T-> massipRightMagnex) If (Height (T-> m_pRight)-Height (T-> m_pLeft) = = 2) {if (x > T-> singleRotateWithLeft-> data) / / right case, turn left {T = singleRotateWithLeft (T) } else / / left and right, first right {T = doubleRotateWithRight (T);} / / if this number already exists, then T-> height = Max (Height (T-> m_pLeft), Height (T-> m_pRight)) + 1 is not inserted. Return T;} / / Recursive implementation of traversing void inOrderVisitUseRecur (const AvlTree pCurrent) {if (pCurrent) {inOrderVisitUseRecur (pCurrent- > m_pLeft); cout data m_pLeft) cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report