1 / 18

Binary and AVL Trees in C

Binary and AVL Trees in C. Yuanming Yu CSCI2100B Data Structures Tutorial 5. Overview. Binary tree Degree of tree is 2. struct node_s { Datatype element; struct node_s *leftChild; struct node_s *rightChild; }; typedef struct node_s node;. Trees – traversal ( Recursion Method ).

shae
Download Presentation

Binary and AVL Trees in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary and AVL Trees in C Yuanming Yu CSCI2100B Data Structures Tutorial 5

  2. Overview • Binary tree • Degree of tree is 2 struct node_s { Datatype element; struct node_s *leftChild; struct node_s *rightChild; }; typedef struct node_s node;

  3. Trees – traversal (Recursion Method) • Preorder void preorder(node *t) { if (t != NULL) { printf(“%d ”, t->element); /* V */ preorder(t->leftChild); /* L */ preorder(t->rightChild); /* R */ } }

  4. Trees – traversal (Recursion Method) • Inorder void inorder(node *t) { if (t != NULL) { inorder(t->leftChild); /* L */ printf(“%d ”, t->element); /* V */ inorder(t->rightChild); /* R */ } }

  5. Trees – traversal (Recursion Method) • Postorder void postorder(node *t) { if (t != NULL) { postorder(t->leftChild); /* L */ postorder(t->rightChild); /* R */ printf(“%d ”, t->element); /* V */ } }

  6. D H G C B E F I A Trees - traversal • Preorder A B D G H E C F I • Inorder G D H B E A F I C • Postorder G H D E B I F C A

  7. AVL tree For each node, the difference of height between left and right are no more than 1. struct AVLnode_s { Datatype element; struct AVLnode *left; struct AVLnode *right; }; typedef struct AVLnode_s AVLnode;

  8. Four Models • There are four models about the operation of AVL Tree: LL RR LR RL

  9. k1 k2 k2 k1 AVL tree • Single rotation: left-left (LL) Z X X Y Y Z W W

  10. k1 k2 k1 k2 AVL tree • Single rotation: right-right (RR) X Z Y Z X Y W W

  11. k2 k1 k3 k1 k3 k2 AVL tree • Double rotation: left-right (LR) D A A C B D B C

  12. k2 k3 k1 k3 k1 k2 AVL tree • Double rotation: right-left (RL) A D A C B D B C

  13. Balancing an AVL tree after an insertion • Begin at the node containing the item which was just inserted and move back along the access path toward the root.{ • For each node determine its height and check the balance condition. { • If the tree is AVL balanced and no further nodes need be considered. • else If the node has become unbalanced, a rotation is needed to balance it. } }we proceed to the next node on the access path.

  14. AVLnode *insert(Datatype x, AVLnode *t) { if (t == NULL) { /* CreateNewNode */ } else if (x < t->element) { t->left = insert(x, t->left); /* DoLeft */ } else if (x > t->element) { t->right = insert(x, t->right); /* DoRight */ } }

  15. AVL tree • CreateNewNode t = malloc(sizeof(struct AVLnode); t->element = x; t->left = NULL; t->right = NULL;

  16. AVL tree • DoLeft if (height(t->left) - height(t->right) == 2) if (x < t->left->element) t = singleRotateWithLeft(t); // LL else t = doubleRotateWithLeft(t); // LR

  17. AVL tree • DoRight if (height(t->right) - height(t->left) == 2) if (x > t->right->element) t = singleRotateWithRight(t); // RR else t = doubleRotateWithRight(t); // RL

  18. Kindly reminder of Midterm • Programming environment would be Linux • We will provide main stream editors: Vi/Vim, Emacs, Gedit, Nano. Please get familiar with at least one of them within 30 days. (There is NO Visual Studio under Linux) • You also need to learn how to compile program by command line (We would like to help in any cases) • e.g.: gcc myprog.c –o myprog • And run it by command: ./myprog

More Related