DAST 2005 Tirgul 9 Binary Search Trees sample questions.

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

Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Department of Computer Science University of Maryland, College Park
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
DAST 2005 Tirgul 10 AVL Trees Graphs DFS sample questions.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Trees and Red-Black Trees Gordon College Prof. Brinton.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
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.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
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.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
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.
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.
H EAPS. T WO KINDS OF HEAPS : MAX AND MIN Max: Every child is smaller than its parent Meaning the max is the root of the tree 10 / \ 9 7 / \ 6 8 / \ 2.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
DAST Tirgul 6. What is a Binary Search Tree? The keys in a binary search tree (BST) are always stored in such a way as to satisfy the search property:
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.
DAST Tirgul 7.
Instructor: Lilian de Greef Quarter: Summer 2017
Recursive Objects (Part 4)
MCS680: Foundations Of Computer Science
Cinda Heeren / Geoffrey Tien
Week 6 - Wednesday CS221.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
Data Structures & Algorithm Design
ITEC 2620M Introduction to Data Structures
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search 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.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Lecture 7 Algorithm Analysis
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
CSC 143 Binary Search Trees.
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

DAST 2005 Tirgul 9 Binary Search Trees sample questions

DAST 2005 Q.Write a pseudo code for computing the height of a binary search tree (BST), what is its time complexity? A.The height of a binary tree is the maximum between the height of its children + 1. We can write a simple recursive algorithm for calculating the height: int height(node) { if (node == null) return -1; return (1 + max(height(node.left), height(node.right))); } The algorithm visits all of the tree nodes  its time complexity is O (n)

DAST 2005 Q.You are provided with a nearly BST, a binary tree in which for every node, either the key of its left child is smaller than its key and the key of its right child is bigger, or vice versa. Write a pseudo code for fixing the tree, what is its complexity? A.The solution is very simple, we should check each node and swap its children if necessary, pay attention that fixing (or not fixing) a node doesn’t say we shouldn’t fix its children. void fix(node) { // should check for nulls if (node.left.key > node.right.key) swap(node.left,node.right) fix(node.left); fix(node.right); } The algorithm visits all of the tree nodes  its time complexity is O (n)

DAST 2005 Q.Your colleague tells you he has found a remarkable BST property: Suppose that the search for key k in a binary search tree ends up in a leaf. Consider three sets: A - the keys to the left of the search path; B - the keys on the search path; and C - the keys to the right of the search path. He claims that any three keys must satisfy a < b < c. Is the claim true? A.No:

DAST 2005 Q.Write an algorithm for building a sorted circular doubly linked list containing the elements of a BST, what is its complexity? A.We should iterate through all the tree nodes, starting with the minimum and moving to the successor and add them to the list (remembering to link the last node to the first one) --- no pseudo code --- Traversing through all the nodes takes O (n) (previous tirgul). Adding n elements to the list takes O (n) as well  the total time complexity is therefore O (n) One drawback: We need an extra O (n) space complexity (for the list) can we avoid the extra space?

DAST 2005 Q.Write an algorithm for the same purpose without using the extra space (at the same time complexity) A.We exploit the fact that the keys of all the members to the left of a node are smaller than that of the node while all those to its right are larger. Intuitively, we would like to convert the left subtree into a list, convert the right subtree into a list and concatenate the lists [LEFT, node, RIGHT]. The algorithm is recursive and does exactly so It uses the tree nodes pointers (NO EXTRA SPACE!). In the result list, left means previous while right means next The algorithm performs a constant amount of work for each node and its time complexity is therefore linear

DAST 2005 List TreeToList(root) { if (root == null) return null; List LEFT = TreeToList(root.left); List RIGHT = TreeToList(root.right); List ROOT = makeList(root); return concatenate(LEFT, ROOT, RIGHT); } makeList - takes a single tree node and builds a circular doubly linked list (containing one element) of it.

DAST 2005 concatenate - takes three circular doubly linked lists and concatenates them into one list (assume it can handle null lists) List concatenate(LEFT, ROOT, RIGHT) { // should handle null lists LEFT.left.right = ROOT; ROOT.left = LEFT.left; ROOT.right = RIGHT; RIGHT.left.right = LEFT; LEFT.left = RIGHT.left; RIGHT.left = ROOT; return LEFT; }

DAST 2005 Concatenate example LEFT ROOTRIGHT LEFT ROOTRIGHT LEFT.left.right = ROOT;

DAST 2005 LEFT ROOTRIGHT ROOT.left = LEFT.left; LEFT ROOTRIGHT ROOT.right = RIGHT;

DAST 2005 LEFT ROOTRIGHT RIGHT.left.right = LEFT; LEFT ROOTRIGHT LEFT.left = RIGHT.left;

DAST 2005 LEFT ROOTRIGHT RIGHT.left = ROOT;