Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 4: Trees Part II - AVL Tree
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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.
CS 171: Introduction to Computer Science II
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
Trees and Red-Black Trees Gordon College Prof. Brinton.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Transforming Infix to Postfix
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Binary Trees Chapter 6.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
ICS 220 – Data Structures and Algorithms Week 7 Dr. Ken Cosh.
COSC2007 Data Structures II
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.
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 ),
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Compiled by: Dr. Mohammad Omar Alhawarat
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Starting at Binary Trees
Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
File Organization and Processing Week Tree Tree.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
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.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Internal and External Sorting External Searching
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
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 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Chapter 21: Binary Trees.
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
CSC 143 Binary Search Trees.
Chapter 20: Binary Trees.
1 Lecture 13 CS2013.
Presentation transcript:

Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1

Outline  2.6 Dealing with Non-Unique Keys  2.7 Queries for the Keys in an Interval  2.8 Building Optimal Search Trees  2.9 Converting Trees into Lists  2.10 Removing a Tree 2

 Complete Binary Tree : All level except leaves are completely filled. Max # of level i=2^i Height (h) with n nodes = log2 n  Perfect Binary Tree: All levels completely full Max # of nodes in tree with height h is = 2^0 + 2^2 +…. +2^h =2^(h+1) -1 = 2^(# of levels)-1 Height (h) with n nodes = log2 (n+1) -1 ( n = 2^(h+1) -1  2^(h+1) = (n+1)  h = log2 (n+1) -1 ) Time complexity = O(h)  O(log2 n) 3 Terminology:

 Balanced binary tree : Difference between height of left and right subtree for every node is not more than 1.  Height of a node : # of edges in longest path from the node to a leaf node>  Depth of a node : # of edges in path from root to the node 4 Terminology:

Traversal Orders  The three traversal orders are: A. Breadth-first B. Depth-first: 1- Preorder: Process the value in the current node first, then its left and right subtrees. 5

Traversal Orders 2- Inorder (symmetric): process the left subtree first, then the value in the current node, then its right subtree. 6

Traversal Orders 3- Postorder: process the left and right subtrees first, then the value in the current node. 7

Linked data structure  Linked list  Chains  Doubly linked 8

2.6 Dealing with Non-Unique Keys  Store many objects with the same key value.  Keep all elements of the same key in a linked list  The correct reaction is as follows: 1- Find : produces all elements of that list O(h + k) k is the number of elements that find returns. 2- Insert: inserts at the beginning of the list O(h). 9

2.6 Dealing with Non-Unique Keys 3- Delete: deletes all items of that key in time O(h). 1- Delete the references to the objects :  Additional node between the leaf and the linked list. ( contains pointers to the beginning and to the end of the list)  Transfer the entire list with O(1) operations to the free list. 2- Delete the objects  walking along this list, but not in O(1). (time independent of the # of objects) 10

2.7 Queries for the Keys in an Interval  We store all those branches on a stack.  We have two keys [a,b[ left and right.  Use left as a Binary search tree key.  Largest end point of subtree in right. 11

Insert Interval [a,b[ 12  Insert into BST, using a as the key.  Update max in each node on search path.

Search Interval [a,b[  If interval in node intersects query interval, return it. interval [a, b[ (lo,hi).  Else if left subtree is null, go right  Else if max endpoint in left subtree is less than a, go right  Else go left. 13

Search Interval [a,b[ 14

2.7 Queries for the Keys in an Interval We Have tow Methods: 1- Method One Change Tree Structure:  Need to organize the leaves into a doubly linked list  Moving between nodes cost O(1).  Need to change the insertion and deletion functions. (cost only O(1) additional time)  Query takes O(k) additional time if it lists, a total of k keys in the interval. 15

2.7 Queries for the Keys in an Interval Method Two Change Query Function:  Down the tree with the query interval instead of the query key.  Search for any one interval they intersects query, If interval in node intersects query interval, return it. interval [a, b[:  Else if left subtree is null, go right  Else if max endpoint in left subtree is less than a, go right  Else go left.  We have i + 1 leaves, when i interior nodes between paths,  total number of nodes visited is at most twice the # of nodes visited in a normal find operation plus O(k) for k leaves.

17

2.8 Building Optimal Search Trees We can construct an optimal search tree from a given set of (key, object) pairs. The search tree will be as static data structure: there are no inserts and deletes, In this case :  No problem for rebalancing the tree,  But if we build it, knowing the data in advance. The primary issue is the height  Because a search tree of height h has at most 2^h leaves, an optimal search tree for a set of n items has height (log n), where the log, as always, is taken to base 2. 18

How to construct search tree with optimal height? Bottom-up. Top-down. Assuming that the (key, object) pairs are given in sorted list (increased order). 19

Bottom-Up method The construction of Bottom_Up is easy.  One views the initial list as a list of one element trees.  Then, one goes repeatedly through the list, joining two consecutive trees. Until there is only one tree left. Assume here that:  the list items are themselves of type tree_node_t.  Left entry pointing to the object.  The key containing the object key.  The right entry pointing to the next item. Or NULL. Create a list, where all the nodes of the previous list are attached as leaves. Then maintain a list of trees, where the key value is the smallest key value in the tree below it. 20

O(n) 21

22

The Bottom_Up method is O(n) The disadvantage of this method is that:  the tree is unbalanced although it has optimal height. The first half of the algorithm, duplicating the list and converting all the original list nodes to leaves, takes obviously O(n) ; it is just one loop over the length of the list. The second half has a more complicated structure, but in each execution of the body of the innermost loop,  One of the n interior nodes created in the first half is removed from the current list and put into a finished subtree,  So the innermost part of the two nested loops is executed only n times. 23

Top-down method The top-down construction is easiest to describe recursively:  divide the data set in the middle,  create optimal trees for the lower and the upper halves,  And join them together. This division is very balanced; in each subtree the number of items left and right differs by at most one, and it also results in a tree of optimal height. But if we implement it this way, and the data is given as list, it takes ( n log n ) time,  because we get an overhead of ( n ) in each recursion step to find the middle of the list.  But there is a nice implementation with O(n) complexity using a stack. 24

How does it works First construct the tree “in the abstract,” without filling in any key values or pointers to objects. Then, find the middle of the list; by keep tracking the number of elements that should go into the left and right subtrees. Building abstract tree using a stack: 1. We initially put the root on the stack, labeled with the required tree size; 2. Then we continue, until the stack is empty, to take nodes from the stack, attach them to two newly created nodes labeled with half the size, 3. And put the new nodes again on the stack. If the size reaches one, we have a leaf, so node should not be put back on the stack but should be filled with the next key and object from the list. The problem is to fill in the keys of the interior nodes, which become available only when the leaf is reached.  Each item on the stack needs two pointers,  One to the node that still needs to be expanded and one to the node higher up in the tree, where the smallest key of leaves below that node should be inserted as comparison key.  Also, each stack item contains a number, the number of leaves that should be created below that node. 25

The Top_Down method is O(n) To analyze this algorithm:  In each step on the stack, we create either two new nodes, and there are only n − 1 new nodes created in total,or we attach a list item as leaf, and there are only n list items. So the total complexity is O(n). 26

27

2.9 Converting Trees into Lists  Using a trivial depth-first search (Stack).  Converts tree of n leaves into a list of n elements.  Converting time complexity is O(n)  Using array as a preferred method If height of tree is not too large.  Array size needs to be at least as large as the height of the tree 28

2.10 Removing a Tree  No longer need it  To avoid a memory leak  Time to Remove a tree structure related to the size.  Rotations in the root till the left-lower neighbor is a leaf;.  Returns that leaf.  Moves the root down to the right,  Returns the previous root. 29

Thanks 30