Trees.

Slides:



Advertisements
Similar presentations
1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
Advertisements

CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
Trees Types and Operations
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
AVL Trees And the pain you must suffer to learn them.
Data Structures AVL Trees.
Binary Search Trees (BST)
AVL Trees CSE, POSTECH.
COMP 53 – Week Fourteen Trees.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Balanced Binary Search Trees
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Lecture Efficient Binary Trees Chapter 10 of textbook
Red-Black Tree Neil Tang 02/04/2010
Week 7 - Friday CS221.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
UNIT III TREES.
Binary Search Trees (Continued)
CSIT 402 Data Structures II
Problems with Linked List (as we’ve seen so far…)
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Introduction Applications Balance Factor Rotations Deletion Example
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
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
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
Lecture 22 Binary Search Trees Chapter 10 of textbook
Cinda Heeren / Geoffrey Tien
MA/CSSE 473 Day 21 AVL Tree Maximum height 2-3 Trees
ITEC 2620M Introduction to Data Structures
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
i206: Lecture 13: Recursion, continued Trees
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Search Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Monday, April 16, 2018 Announcements… For Today…
AVL Trees CENG 213 Data Structures.
Instructor: Lilian de Greef Quarter: Summer 2017
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
Search Sorted Array: Binary Search Linked List: Linear Search
Data Structures and Algorithms
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Data Structures & Algorithms
Tree Rotations and AVL Trees
AVL Search Tree put(9)
CS202 - Fundamental Structures of Computer Science II
short illustrative repetition
AVL Tree By Rajanikanth B.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
AVL Tree Chapter 6 (cont’).
AVL Trees (Adelson – Velskii – Landis)
INSERT THE TITLE OF YOUR PRESENTATION HERE AVL TREE.
Lecture 9: Intro to Trees
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Trees

Recursion Review Base Case- how we exit and don’t go into infinite recursion Some work Division of work Recursive call (s) For many of you, recursion is intimidating. Eventually it will become second nature just as loops and file operations have.

Recursive Process Decide if you want to use recursion Decide inputs and outputs Decide how to break down the work Tentatively decide on a base case Make recursive calls Bring back together the results Return the proper values Finalize your base case Trace the recursion Practice sum numbers from 1 to n

Practice - Sum the numbers from 1 to n int sum(int n){ if(n==1){ return n; } int curSum=n+sum(n-1); return curSum;

Practice - Sum array int sum( int* arr, int pos, int size){ if(pos==size-1) return arr[pos]; return arr[pos]+sum(arr, pos+1, size); }

Practice - Linked List class Node { public: int value; Node *next; Node( int val, Node *n = NULL ){ value=val; next=n; } };

Practice - Linked List Print Void print(Node* cur){ if( cur==NULL) return; cout<<cur->value; print(cur->next); }

Binary Search Tree What is a tree? What is a Binary tree? What is a Binary search tree? Binary tree, node and two pointers to children Binary search tree is a binary tree But it is ordered to facilitate searching Anything to the left is a smaller value, anything to the right is a greater value Children are binary search trees

Binary Search Tree Is a binary tree (each node has two pointers to children) Ordered to facilitate searching Anything to the left is a smaller value Anything to the right is a greater value Children are binary search trees Cannot have duplicate values because it violates the search property

Example 1

At your seats Build a tree from these nodes B R L M T C A N P D

Deletion We need to preserve the binary search properties Deleting a leaf is easy, we can just delete it, and set its parent’s pointer to null The easiest way to delete a node is to find its successor, copy the value over, then delete that leaf node

Code - Node class Node { public: int value; Node *left; Node *right; Node( int val, Node *l = NULL, Node *r = NULL ){ value=val; left=l; right=r; } };

Code - Search Node* search( int value, Node* cur){ if(cur==NULL) return NULL; else if( value == cur->value) return cur; else if( value < cur->value) return search(value, cur->left); else return search(value, cur->right); }

Code - Delete void delete( int value, Node* cur){ }

Complexity Search Insert Delete Search – Ο(h) , where h is the height of the tree. In the best case (balanced), h is log n . In the worst case, h is n (the number of nodes in the tree). Insert – Ο(h) , where h is the height of the tree. Delete – Ο(h) , where h is the height of the tree.

Disadvantages What is my worst case? When will it occur? O(n) Will happen if things are in order

Balanced Balanced means the heights of the left and right sub-trees differ by at most one This needs to be true for every node in the tree If a tree is balanced, what is the worst case?

Balanced Examples

Alternatives AVL and Splay trees Self balancing

AVL - Adelson, Velskii, Landis Binary tree Balanced Determining if it is an AVL tree Check each node for Right sub-tree is an AVL tree Left sub-tree is an AVL tree Heights of right and left differ by at most one AVL search Tree An AVL tree, but also a binary tree

AVL practice Is it AVL? Is it AVL search?

AVL We usually only look at AVL search When I say AVL, I mean AVL search If not search, I will specify by saying non-search AVL

Properties Height is logn Can do it for any n Search is logn Insertion is logn Deletion is logn

AVL - Search How do we search? Same as binary search

AVL - Insert How is it done? Same way as binary search What if it is thrown off balance? Re- balance Rotations

AVL - Rotations RR LL RL LR

AVL - Single Left Rotation Fixes a RR inbalance

AVL - Double Left Rotation Fixes an RL inbalance

AVL Insert 3, 2, 1, 4, 5, 6, 7, 16, 15, 14

AVL Delete How do we delete?

AVL Delete How do we delete? Same way as in a binary search tree But the tree can be thrown off balance Re- balance the tree using rotations May need to do multiple rotations.

AVL Delete Rebalancing Starting where we deleted, we follow the recursion back up the tree At every node we visit, we need to check that it is still balanced. If it is not balanced, we use a rotation to fix it What is the worst case for the number of rotations I have to do?

AVL Delete Delete 60

More AVL Delete Delete 70

Example - Delete

Example - Delete

Example - Delete

Delete – Code? int Delete (){ }

Participation Quiz – 5 points Do you like the way I use power points to guide the lecture? Suggestions? Do you feel I do a good job answering in class questions? Suggestions? Do you find the examples, and asking for class guidance through them helpful? Suggestions? Write a short snipet of code that has O(n) complexity. It can be simple, and does not need to do anything useful. Which trees drawn on the board are AVL? (remember I mean AVL search) I want this to be the best learning experience possible for you, so please include any other comments or suggestions you have for improving the course.