AVL tree implementation

Source: Internet
Author: User
// Avl_tree.h # include <stack> Using STD: Stack; Template <typename T> class avl_tree {public: avl_tree () {nil = new avl_node (0,-1, null, null); tree_root = nil;} bool find (const T & Val) const; void insert (const T & Val); void del (const T & Val ); // Delete typedef struct tagavl_node {bool exist; t data; int height; struct tagavl_node * left, * right; tagavl_node (const T & Val, int WG, tagavl_node * Cl, tagavl_node * Cr) {data = Val; Height = WG; left = Cl; Right = CR; exist = 1 ;}} avl_node; private: avl_node * tree_root, * nil; avl_node * own_insert (avl_node * tree, const T & Val); avl_node * rolatewithleft (avl_node * P); avl_node * rolatewithright (avl_node * P);}; Template <typename T> bool avl_tree <t> :: find (const T & Val) const {avl_node * P = tree_root; while (P! = Nil & P-> data! = Val) {If (p-> data <Val) P = p-> right; else if (p-> DATA> Val) P = p-> left ;} return p! = Nil & P-> DATA = Val & P-> exist;} template <typename T> typename avl_tree <t >:: avl_node * avl_tree <t> :: rolatewithleft (avl_node * P) {avl_node * H = p-> left; P-> left = H-> right; H-> right = P; p-> Height = p-> left-> height> P-> right-> height? P-> left-> height + 1: p-> right-> height + 1; h-> Height = H-> left-> height> H-> right-> height? H-> left-> height + 1: H-> right-> height + 1; return h;} template <typename T> typename avl_tree <t> :: avl_node * avl_tree <t>: rolatewithright (avl_node * P) {avl_node * H = p-> right; P-> right = H-> left; h-> left = P; P-> Height = p-> left-> height> P-> right-> height? P-> left-> height + 1: p-> right-> height + 1; h-> Height = H-> left-> height> H-> right-> height? H-> left-> height + 1: H-> right-> height + 1; return h;} template <typename T> void avl_tree <t> :: insert (const T & Val) {avl_node * P = own_insert (tree_root, Val); If (tree_root = nil) {tree_root = P ;} else if (p-> left = tree_root | p-> right = tree_root) tree_root = P;} template <typename T> typename avl_tree <t> :: avl_node * avl_tree <t>: own_insert (avl_node * tree, const T & Val) {If (nil = tree) tree = new AVL _ Node (Val, 0, nil, nil); else if (Val> tree-> data) {tree-> right = own_insert (tree-> right, Val ); if (2 = tree-> right-> height-tree-> left-> height) {If (Val> tree-> right-> data) tree = rolatewithright (tree); else {tree-> right = rolatewithleft (tree-> right); tree = rolatewithright (tree );}}} else if (Val <tree-> data) {tree-> left = own_insert (tree-> left, Val ); if (2 = tree-> left-> height-tree-> right-> height ){ If (Val <tree-> right-> data) tree = rolatewithleft (tree); else {tree-> left = rolatewithright (tree-> left ); tree = rolatewithleft (tree) ;}} else // val = tree-> data {tree-> exist = 1;} If (tree! = Nil) tree-> Height = (tree-> right-> height> tree-> left-> height? Tree-> right-> Height: Tree-> left-> height) + 1; return tree;} template <typename T> void avl_tree <t> :: del (const T & Val) {avl_node * P = tree_root; while (P! = Nil & P-> data! = Val) {If (p-> data <Val) P = p-> right; else if (p-> DATA> Val) P = p-> left ;} if (P! = Nil & P-> DATA = Val) P-> exist = 0 ;}

AVL tree implementation

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.