Binary and AVL Trees in C

Slides:



Advertisements
Similar presentations
AVL Trees binary tree for every node x, define its balance factor
Advertisements

CS 261 – Data Structures AVL Trees. Binary Search Tree: Balance Complexity of BST operations: proportional to the length of the path from the root to.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
Trees CS-212 Dick Steflik. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called the root.
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Data Structures Using C++1 Chapter 11 Binary Trees.
1 8/16/2015 MATH 224 – Discrete Mathematics A rooted tree is a tree that has a distinguished node called the root. Just as with all trees a rooted tree.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Lecture 17 Non-Linear data structures Richard Gesick.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
Introduction to C Programming CE Lecture 23 Binary Trees.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Queue and Tree in C Yang Zhengwei CSCI2100B Data Structures Tutorial 5 1.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BST Trees
Lecture 5 Trees.
AVL Trees binary tree for every node x, define its balance factor
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Introduction Applications Balance Factor Rotations Deletion Example
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
CS 201 Data Structures and Algorithms
Chapter 29 AVL Trees.
Additional Tree Traversal Example #1
CS 261 – Recitation 7 Fall 2010 CS 261 – Data Structures
Trees Another Abstract Data Type (ADT)
Tree.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Section 8.1 Trees.
Data Structures & Algorithm Design
עצים root הגדרת עץ חופשי: גרף לא מכוון ללא מעגלים
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
AVL Trees CENG 213 Data Structures.
Trees.
Binary Tree Traversal Methods
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
AVL Trees: AVL Trees: Balanced binary search tree
Trees Another Abstract Data Type (ADT)
Binary Tree Traversal Methods
Trees Another Abstract Data Type (ADT)
CSE 373: Data Structures and Algorithms
Binary Tree Traversal Methods
AVL Search Tree put(9)
Dynamic Dictionaries Primary Operations: Additional operations:
CE 221 Data Structures and Algorithms
AVL Tree By Rajanikanth B.
Binary Tree Properties & Representation
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
AVL Tree Chapter 6 (cont’).
Binary Trees.
Binary and AVL Trees in C Hao Ma
INSERT THE TITLE OF YOUR PRESENTATION HERE AVL TREE.
Tree traversals BST properties Search Insertion
AVL-Trees (Part 2).
Yuanming Yu CSCI2100B Data Structures Tutorial 9
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

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) Preorder void preorder(node *t) { if (t != NULL) { printf(“%d ”, t->element); /* V */ preorder(t->leftChild); /* L */ preorder(t->rightChild); /* R */ }

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 Method) Postorder void postorder(node *t) { if (t != NULL) { postorder(t->leftChild); /* L */ postorder(t->rightChild); /* R */ printf(“%d ”, t->element); /* V */ }

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 B C D E F I G H

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 There are four models about the operation of AVL Tree: LL RR LR RL

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

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

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

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

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.

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 tree CreateNewNode t = malloc(sizeof(struct AVLnode); t->element = x; t->left = NULL; t->right = NULL;

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