binary and avl trees in c n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Binary and AVL Trees in C PowerPoint Presentation
Download Presentation
Binary and AVL Trees in C

Loading in 2 Seconds...

play fullscreen
1 / 18

Binary and AVL Trees in C - PowerPoint PPT Presentation


  • 115 Views
  • Uploaded on

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 ).

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Binary and AVL Trees in C' - shae


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
binary and avl trees in c

Binary and AVL Trees in C

Yuanming Yu

CSCI2100B Data Structures Tutorial 5

overview
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
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 */

}

}

trees traversal recursion method1
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 */

}

}

trees traversal recursion method2
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 */

}

}

trees traversal

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

avl tree
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;

four models
Four Models
  • There are four models about the operation of AVL Tree:

LL RR LR RL

avl tree1

k1

k2

k2

k1

AVL tree
  • Single rotation: left-left (LL)

Z

X

X

Y

Y

Z

W

W

avl tree2

k1

k2

k1

k2

AVL tree
  • Single rotation: right-right (RR)

X

Z

Y

Z

X

Y

W

W

avl tree3

k2

k1

k3

k1

k3

k2

AVL tree
  • Double rotation: left-right (LR)

D

A

A

C

B

D

B

C

avl tree4

k2

k3

k1

k3

k1

k2

AVL tree
  • Double rotation: right-left (RL)

A

D

A

C

B

D

B

C

balancing an avl tree after an insertion
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.

slide14

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 */

}

}

avl tree5
AVL tree
  • CreateNewNode

t = malloc(sizeof(struct AVLnode);

t->element = x;

t->left = NULL;

t->right = NULL;

avl tree6
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

avl tree7
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

kindly reminder of midterm
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