AVL Tree related Operations

Source: Internet
Author: User

#include <iostream>using namespacestd;//the node of the AVL treeTemplate<typename t>classtreenode{ Public: TreeNode (): Lson (null), Rson (null), Freq (1), HGT (0) {} T data;//value    intHgt//The height of the tree with this node as its root    intFreq//the frequency of the same point I don't knowtreenode* Lson, *rson;//address of the left and right son};template<typename t>classavltree{//class properties and method declarations for AvltreePrivate: TreeNode<T>*root;//root node    voidInsertpri (Treenode<t>*&node, T x);//Inserttreenode<t>* Findpri (treenode<t>* node, T x);//Find    voidTraversalpri (treenode<t>* node);//Traverse    voidDelpri (treenode<t>* &node, T x);//Delete    intHeight (treenode<t>* node);//Auxiliary operation: Height    voidSingleft (treenode<t>* &k2);//left left Operation    voidSingright (treenode<t>* &k2);//Right -right operation    voidDoubleleft (treenode<t>* &k3);//left and right operation    voidDoubleright (treenode<t>* &k3);//right-left operation    intMaxintCmpaintCMPB); Public: Avltree (): root (NULL) {}; voidInsert (T x);//Insert Interface    voidDel (T x);//Remove Interfacetreenode<t>* Find (T x);//Find Interfaces    voidTraversal ();//Traverse Interface};//Auxiliary operation: HeightTemplate<typename t>intAvltree<t>::height (treenode<t>*node) {    if(node!=NULL)returnNode->hgt; return-1;}//Auxiliary operation: Compare heightTemplate<typename t>intAvltree<t>::max (intCmpaintCMPB) {    returnCmpa > CMPB?CMPA:CMPB;} Template<typename t>voidAvltree<t>::singleft (treenode<t>* &K2) {//Left-to-left case rotationtreenode<t>*K1; K1= k2->Lson; K2->lson = k1->Rson; K1->rson =K2; K2=K1; K2-&GT;HGT = max (height (k2->lson), height (K2->rson)) +1; K1-&GT;HGT = max (height (k1->lson), height (K1->rson)) +1;} Template<typename t>voidAvltree<t>::singright (treenode<t>* &K2) {//Left-to-left case rotationtreenode<t>*K1; K1= k2->Rson; K2->rson = k1->Lson; K1->lson =K2; K2=K1; K2-&GT;HGT = max (height (k2->lson), height (K2->rson)) +1; K1-&GT;HGT = max (height (k1->lson), height (K1->rson)) +1;} Template<typename t>voidAvltree<t>::d oubleleft (treenode<t>* &K3) {//about the situationSingright (k3->Lson); Singleft (K3);}//right-left caseTemplate<typename t>voidAvltree<t>::d oubleright (treenode<t>* &K3) {Singleft (K3-Rson); Singright (K3);}//InsertTemplate<typename t>voidAvltree<t>::insertpri (treenode<t>* &node, T x) {    if(node = =NULL) {Node=NewTreenode<t>(); Node->data =x; return; }    if(node->data>x) {INSERTPRI (node->lson, x);//Recursive insertion//self-adjusting after inserting        if(2= = Height (node->lson)-Height (node->Rson)) if(Node->lson->data <x) doubleright (node); Elsesingleft (node); }    Else if(Node->data <x) {INSERTPRI (node-Rson, X); if(2= = Height (node->rson)-Height (node->Lson)) if(Node->rson->data <x) singright (node); Elsedoubleright (node); }    Else+ + (node->freq); //The new height value, the next +1 is important, the author has forgotten to addNODE-&GT;HGT = max (height (node->lson), height (Node->rson)) +1;}//Insert InterfaceTemplate<typename t>voidAvltree<t>:: Insert (T x) {Insertpri (root, x);}//FindTemplate<typename t>TreeNode<t>* Avltree<t>::findpri (treenode<t>*node, T x) {    if(node = =NULL)returnNULL; if(Node->data >x)returnFindpri (node->Lson, X); Else if(Node->data <x)returnFindpri (node->Rson, X); Else returnnode;}//FindTemplate<typename t>TreeNode<t>* avltree<t>:: Find (T x) {Findpri (root, x);}//DeleteTemplate<typename t>voidAvltree<t>::d elpri (treenode<t>* &node,t x) {    if(node = =NULL)return; if(X < node->data) {DELPRI (node-Lson, X); //post-Deletion adjustments        if(2= = Height (node->rson)-Height (node->Lson)) if(Node->rson->lson&&height (Node->rson->lson) > Height (node->rson->Rson))        Doubleright (node); Elsesingright (node); }    Else if(X > Node->data) {DELPRI (node-Rson, X); if(2= = Height (node->lson)-Height (node->Rson)) if(Node->lson->rson&&height (Node->lson->rson) > Height (node->lson->Lson))        Doubleleft (node); Elsesingleft (node); }    Else//after the action is found{//first, there were two sons.        if(node->lson&&node->Rson) {TreeNode<t>* T = node->Lson;  for(; t->rson; t = t->Rson); Node->data = t->data; Node->freq = t->freq; //The case of recursion to a son or no sonDelpri (Node->lson, t->data); if(2= = Height (node->rson)-Height (node->Lson)) {//the following if themselves will not write                if(Node->rson->lson&&height (Node->rson->lson) > Height (node->rson->Rson))                Doubleright (node); Elsesingright (node); }        }        Else{TreeNode<t>* T =node; if(Node->lson = =NULL) Node= node->Rson; Else if(Node->rson = =NULL) Node= node->Lson;            Delete (t); T=NULL; }    }    if(node = = NULL)return;//indicates that there is only the root node, and after the deletion, there is noNODE-&GT;HGT = max (height (node->lson), Height (node->Rson)); return;} Template<typename t>voidAvltree<t>::d El (T x) {Delpri (root, x);}//Middle sequence traversal functiontemplate<classT>voidAvltree<t>::traversalpri (treenode<t>*node) {    if(node = = NULL)return; TRAVERSALPRI (Node->lson);//Walk the left subtree firstcout << Node->data <<" ";//Output root nodeTraversalpri (Node->rson);//and then traverse the right subtree .}//Middle Sequence Traversal interfacetemplate<classT>voidAvltree<t>:: Traversal () {traversalpri (root);}intMain () {Avltree<int>T; T.insert (3); T.insert (2); T.insert (1); T.insert (4); T.insert (5); T.insert (0); T.del (2); T.insert (2); T.traversal ();}

AVL Tree related Operations

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.