Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.

Slides:



Advertisements
Similar presentations
Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Trees Types and Operations
Advanced Data Structures
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
B + -Trees (Part 1) Lecture 20 COMP171 Fall 2006.
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
B + -Trees (Part 1) COMP171. Slide 2 Main and secondary memories  Secondary storage device is much, much slower than the main RAM  Pages and blocks.
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
CSE 326: Data Structures Lecture #13 Extendible Hashing and Splay Trees Alon Halevy Spring Quarter 2001.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
B-Trees and B+-Trees Disk Storage What is a multiway tree?
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
B + -Trees COMP171 Fall AVL Trees / Slide 2 Dictionary for Secondary storage * The AVL tree is an excellent dictionary structure when the entire.
Splay Trees Splay trees are binary search trees (BSTs) that:
Splay Trees and B-Trees
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
COSC2007 Data Structures II
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CSIT 402 Data Structures II
B + -Trees. Motivation An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations.
Starting at Binary Trees
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Data Structures AVL Trees.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 CSE 326: Data Structures Trees. 2 Today: Splay Trees Fast both in worst-case amortized analysis and in practice Are used in the kernel of NT for keep.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
SPLAY TREE The basic idea of the splay tree is that every time a node is accessed, it is pushed to the root by a series of tree rotations. This series.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
BCA-II Data Structure Using C
Multiway Search Trees Data may not fit into main memory
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Trees, Binary Search Trees
Trees.
AVL-Trees (Part 1).
CMSC 341 Splay Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6

TREES Trees are special subset of graphs. A tree is a collection of nodes. It consists of a) a distinguished node called root b) zero or more non empty subtrees T1,T2………. Tk, each of these nodes connected by a directed edge from “r” c)recursive definition root T1 T2 Tk

Tree is a directed graph without a cycle 1.each node of a tree has only one parent node except root 2.there is only one “path” from root to each node, no cycle Path Path: a sequence of nodes n 1, n 2 ….. n k such that n i is the parent of n i+1 Length of path: the number of edges Depth of n i : length of the path from root to n i Height of n i : length of longest path from n i to leaf node Leaf node: which has no child also called as terminal node

Linked list representation of a tree

Implementation: using linked lists

Tree Search (tree traverse) 1) Depth first search(DFS)……………… use stacks 2) Breadth first search(BFS) …………… Use Queues

Binary Search Tree each son of a vertex is distinguished either as a left son or as right son no vertex has more than one left son nor more than one right son a tree in which no node can have more than two children Operations: 1.Find(Search) 2.Traversal 3.Insert 4.Delete 5.Build

Find Operation Time Complexity – O(Height of the Binary Search Tree) That is O(N) in worst case Example, A B C D E Height of the tree = N Thus, Order of growth = O(N)

Find (Worst Case Example) Order of growth will be O(N), no matter how the tree is structured. A D E B C A B C D E

Find Max and Find Min For Find Max and Find Min operations, the worst case will have the time complexity order of O(N). L is the smallest value in this BST. I is the largest value in this BST. For sorting in ascending order use inorder (LVR) method. For sorting in descending order use inorder (RVL) method. It will have the order of O(N). A BC D F G J L M E HI K O N

Traversal & Median Value Inorder can be used to find the median value in the BST. It will have the order of O(N). We can use the balanced binary search tree in order to change the order O(N) to O(logN). Traversal in a BST will have the order O(N). Recursion can be used for traversal operation.

Insert Operation Always follow the BST rules while inserting a new node in the tree. Case 1) New node will always be a terminal node. Example ( 2 is the new node) Case 2) In order to find the location to insert the node in some cases when following the BST protocols. The complexity will be in the order O(N). Example ( 6 is the new node) Time Complexity worst case – O(Height of the BST)

Delete Operation Case 1) The deleting node has no child ≡ terminal node (leaf) => just delete it ! Example Case 2) The deleting node has only one child => reconnect the child to its parent node. Example

Delete Operation Case 3) The deleting node has two children (two sub trees). A) Find the smallest node from its right sub tree and replace the deleting node with it and delete the replaced node. B) Find the largest node from its left sub tree and replace the deleting node with it and delete the replaced node. Time Complexity worst case – O(Height of the BST) Example (A)

Build Operation For the following N elements, the BST is build based on the input from left to right. Example 5,10,21,32,7 Example 10,7,5,21,

Average Case Analysis

Balanced Binary Search Tree ( AVL Tree ) The difference of depth between any terminal node in a binary tree should be at most 1. AVL Tree is a BST which satisfies the balance condition. 1.Must be easy to maintain. 2.Depth of tree is O(log N). How to implement it ? Reshape the AVL tree if the depth difference is more than 1. Rotation Single Rotation Double Rotation Height of empty AVL tree = -1 (Purely Mathematical assumption) Definition of Balance Condition For “every” node in AVL tree, height of left and right sub tree can differ by at most 1.

Ex:

To maintain balance condition we require additional operations after inserting or deleting a node. Note: Height information is kept for each node. After each insert operation, update the height of all the nodes from new inserted node to root node. Note: The min no. of nodes in an AVL tree of height “h” is S(h). S(h) = S(h-1)+S(h-2)+1 Ex: S(0)=1; S(1)=2; S(2)=4; S(4)=12; S(5)=20 All the tree operations = O(log n) To maintain AVL tree condition after each of insert or delete operations we need to perform Rotations.

Insertion Operation: If you want to insert 6 into above AVL tree the newly inserted node will be leaf node.

The above tree is not an AVL tree as node 8 violates the condition and height is not balanced. So, we need to rotate. Note: After one insertion only nodes that are on the path from the insertion point to the root might have their balance altered. Inserted node to root : update the balancing info. α : node that must be rebalanced Case 1: Insertion into the left sub tree of the left child of α Case 2: Insertion into the right sub tree of left child of α Case 3: Insertion into the left sub tree of right child of α Case 4: Insertion into the right sub tree of right child of α

Types of rotation: Single Rotation: Case 1 and Case 4 Double rotation: Case 2 and Case 3. Case 1:

K2 is alpha. So perform single rotation.

Case 4:

After single rotation (counter-clock wise)

Case 3 – Double rotation

Case 2 – Double rotation

Upon the 1 st rotation we get the following tree.

To understand the double rotation consider for example tree where we insert 5.But upon such action height violation occur at the node 8.

But this tree is also not balanced we get a violation at node 4.So single rotation does not work here and hence we have to use double rotation for the tree. After double rotation we get tree as below.

SPLAY TREE It is a special case of Binary Search Tree and splay tree ≠ balanced Binary Search Tree. * Relatively Simple. * Guarantee that any M Consecutive tree operations starting from an empty BST take at most O(MlogN). * It does not mean O(logN) for single operation. * This just means O(logN) “amortized” cost per operation. Idea: After a node is accessed it is pushed to the root by a series of AVL tree rotations. Why?: Likely to be accessed again in the future ~ Locality. Does not require the maintenance or height/balance information.

After 1 st rotation

After 2 nd rotation

After 3rd rotation

After 4 th rotation

Problem: Another node might be as deep as tree height. Ω (M.N) time for sequence of M operations. Method 2: Perform a series of special rotations. Zig-Zag. Zig-Zig. Let X be a (non-root) node on the access path at which we are rotating. Rule: If the parent of X is the root of the tree, merely rotate X and the root OTHERWISE X has both a parent and parent which are (P) and (G) respectively.

Case 1: Zig Zag

Case 2: Zig Zig

Delete Operation 1)Accessing the node to be deleted – push up the node to root 2)Delete it (root) – two subtrees Tl and Tr. 3)Find largest element in Tl – It will be a root with right child of Tl. 4)Tr will be the right subtree of the root.

B-Tree (M and L) = Reduce the number of disc access time M-ary search Tree M-way branches M-1 keys Maintain M-ary search tree is balanced

Properties of B-Tree 1. Data items are stored at leaves. 2. Non-leaf nodes store up to (M-1) keys Key i represents the smallest key in subtree (i + 1) 3. Root is either a leaf or has between two & M children 4. All non-leaf nodes(except root) rhave between “[M/2] & M” children 5. All leaves are at the same depth & have between [L/2] & L children for same L

eg.

eg. deletion

eg. Assume that one block = 8192bytes = 8K each key = 32bytes link = 4bytes M-ary B-Tree M-1 keys M links 32(M-1)+4M <= 8192 M=228 L value? 8192/record size(256byte) = 32records/leaf

10 million records 1.Each leaf has between 16 & 32 records 2.Each internal node(except root) at least 114 branches  10,000,000 / 16 = 625,000 leaves (worst case)  Worst case, depth of B-Tree?  Log ,000,000