I2206 Data Structures TS 6: Binary Trees - Binary Search Trees
|
|
- Sharleen Tucker
- 5 years ago
- Views:
Transcription
1 Lebanese University BS - Computer Science Faculty of Science Section I I2206 Data Structures TS 6: Binary Trees - Binary Search Trees Exercise 1 What value does the following C function return? i n t mystery ( Btree x ) i f ( x == NULL) r eturn 0 ; e l s e r eturn max( mystery ( x >l e f t ), mystery ( x >r i g h t ) ) ; } Exercise 2 Write a function that returns the maximum value of all the nodes in a binary tree. Assume all values are nonnegative; return -1 if the tree is empty. Exercise 3 Write a function that prints all the nodes less than a given value v in a binary tree of integers. Exercise 4 Write a function that returns the sum of all the nodes in a binary tree. Exercise 5 Write a function that counts the number of items in a binary tree. Exercise 6 Write an iterative function that tests whether an element belongs to a BST. Exercise 7 Write a recursive function that tests whether an element belongs to a BST. Exercise 8 Write a recursive function that inserts an element into a BST. Exercise 9 Write an iterative function that inserts an element into a BST. Exercise 10 Write a function that checks whether two BSTs are equal (contains the same elements). Exercise 11 Write a function that deletes an element from a BST. Exercise 12 A binary tree is said to be balanced if both of its subtrees are balanced and the height of its left subtree differs from the height of its right subtree by at most 1. Write a C function to determine whether a given binary tree is balanced. Exercise 13 Write a function that returns the depth of a binary tree. Test your function. Exercise 14 Write a function that counts the number of leaf nodes in a binary tree. Test your function. 1
2 Exercise 15 Write a function that counts the number of non-leaf nodes in a binary tree. Test your function. Exercise 16 Write a function that computes the maximum width of a dynamically implemented BT. We define the maximum width of a BT as the maximum number of nodes located at the same level. Exercise 17 A comb left is a locally complete binary tree (each node has 0 or 2 children) in which each right child of a node is a leaf. Write a function isleftcomb that checks if a binary tree is a left comb. Exercise 18 Write a function that computes the maximum depth of a statically implemented BT. Exercise 19 ( Exam) A historian wants to classify a set of events. Each event is characterized by a title, a location and a period of time (as most of the events are spread over several years). Title and location are strings, while the period is a closed interval [start, end], where start and end is a pair of integers such that < start end < +. In the following, we use the following declarations: typedef struct int start, end;} interval; typedef struct char *title, *location; interval period;} Event; Here are some examples of events: Title Location Period World War II world [ ] First Atomic Bomb Hiroshima [ ] JFK president U SA [ ] Independence Lebanon [ ] Note that certain events are spread over several years, while others begin and end the same year. In order to classify these events, the historian builds a binary tree of events (BtreeE) whose data are events such as: If EvL is in the left subtree of Ev, then EvL.start Ev.start If EvR is in the right subtree of Ev, then EvR.start > Ev.start Thus, an event tree is a binary search tree for the event starting year in which repetitions can occur since EvL.start Ev.start (see figure against, where T i denotes a title and L i a location). We therefore have the following declaration: typedef struct node Event data; struct node *Left, *Right;} BtreeE; 2
3 1. Write a recursive function int contains(btreee B, int year) that returns the number of events of B such as year interval of these events. 2. Write an iterative function that returns the minimum interval that contains all the intervals of events of a given BtreeE. In the previous example, the result will be [1210, 1985]. 3. Write a function int isbtreee(btreee B) that determines whether a given event tree B satisfies the properties of a BtreeE. 4. Write a function int LongestInterval(BtreeE B, Event E) that returns the time in years from the largest inersection event between E and those of B. Example: the intersection of the event T8, L8, [ ] with the previous tree is 31; it comes from the intersection with T2, L2, [ ]. 5. Write a function int maxlong(btreee B) that returns the longest common period (intersection) of two events in a given BtreeE. In the previous tree, the events, T2,L2,[ ] and T5,L5,[ ] have an intersection of 24 years (between 1930 and 1954). Exercise 20 In a binary tree (BT), a path between the root and a leaf is the number of arcs joining the root to the leaf. The BT height is the length of the longest path between the root and each of the leaves. 1. Write a function int height(bt T) that returns the height of T. 2. The search algorithm in a BST is proportional to its height. In order to improve the search performance, we consider the class of balanced BST. A BST T is balanced if T is empty or if for each node, the difference between the heights of the left and right subtrees is < 2. Write a function int dif LR(BT T) that returns the difference between left and right subtree height of a given BT T. 3. Write a function int balanced(bt T) that returns 1 if T is balanced and 0 otherwise. 4. In order to ensure the balance of a BST, we suggest to transform it using the following rotations: Left Rotation: a left rotation around the node B consists in getting down B and getting up its right child D without invalidating elements order (see figure below). Right Rotation: the reverse of the left rotation. Left-Right Rotation on T: it consists in a left rotation on T left subtree followed by a right rotation on T. Right-Left Rotation on T: it consists in a right rotation on T right subtree followed by a left rotation on T. 3
4 Write a function left rotation that rotates left a given BST. You must only manipulate pointers; no memory allocation or release is allowed. 5. Write a function left right rotation that rotates left-right a given BST. 6. The insertion of an element into a balanced BT is as follows: the element is added as when inserting into a BST then: If the left balance is lost then a left-right rotation is done if the left child lean to the right, otherwise a simple right rotation is done. If the right balance is lost then a right-left rotation is done if the right child lean to the left, otherwise a simple left rotation is done. Assuming that a binary tree T is said to be left-leaning if dif LR(T)>0, and suppose having the functions: lean L, lean R, left rotation, left right rotation, right rotation, and right left rotation, write a function to insert an element into a balanced BST that implements the described insertion procedure. Exercise 21 ( Exam) 1. (10 pts) Write a recursive function int max rec(btree B, int root index) that returns the maximum element in a binary tree statically implemented rooted at index root index. Example: Consider the following Binary tree and its representation using an array: Index Root Left Right 0 Rnd Rnd Rnd Rnd -1 Rnd where Rnd designates a random number. max rec(b, 1) returns 45; max rec(b, 2) returns 4 since the maximum element of the tree rooted at element of value 4 (root index=2) is equal to 4; 4
5 max rec(b, 5) returns (2 pts) Without performing calculation, give the complexity of the above function. 3. (11 pts) Write a recursive function int isbst(btree tree) that checks whether a statically implemented Binary tree is a Binary Search Tree. Your function should return 0 for the tree above. 4. (10 pts) Write an iterative function that calculates the width of a statically implemented Binary tree. Recall that the width of a tree is equal to the maximum of widths of all levels. Your function should return 3 for the tree above. Exercise 22 ( Exam) We define the maximum width of a BT as the maximum number of nodes located at the same level, and the maximum depth as the longest path from the root to a leaf. 1. Write a function that computes the maximum width of a dynamically implemented BT. 2. Write a function that computes the maximum depth of a statically implemented BT. Exercise 23 ( Exam) A binary tree is called complete if all its non-leaf nodes have two children. Write a function that tests whether a binary tree statically implemented is complete. Exercise 24 ( Exam) Write a function deepest(a, E) that returns the deepest level of the element E in the binary tree A or 0 if E / A. We assume that the root of the tree is at level 1, and that the tree is dynamically implemented. Running example: let A be the following BT; deepest(a,2)=3, deepest(a,3)= Exercise 25 ( Exam) We define the width of a level of a BT as the number of nodes located at this level. A BT is a fairly well developed if the width of each level of the BT is > than the one of the previous level. Write a function that determines whether a BT is fairly well developed Exercise 26 ( Exam) Consider the following declaration of a BST in which the field nbd denotes the number of descendants of a tree node: typedef s t r u c t node element data ; 5
6 } BST; i n t nbd ; s t r u c t node Left, Right ; 1. Write a function that inserts an element to a BST; 2. Write a function that tests that the field nbd of each BST node is correct. Exercise 27 ( Exam) A Boolean expression is represented as a binary tree of strings (BT) in which internal nodes contain the logical connectors AND, OR and NOT, and leaves contain strings representing Boolean variables. Ex: the expression a 1 AND NOT (bool 2 OR bx 30 ) is represented by the following BT: An interpretation of an expression is represented by a set of pairs <variable, value> that associates to each variable of the expression a Boolean value. Ex: the set (<a1,1>,<bool2,0>,<bx30,1>) associate the value 1 (True) to the Boolean variables a1 and bx30 and the value 0 (False) to bool Assume that we use BT dynamic implementation; give the declaration of the types Interpretation and Expression. 2. Write a function that returns the Boolean value of a variable in an interpretation if it exists. 3. Write a function int Evaluate(Interpretation I, Expression E, int *Value) that computes the value of the expression E according to the interpretation I. 4. Now, we want to test whether a given expression is valid (true for all interpretations). Assume the existence of an array containing all interpretations of a given expression (all possible variables combinations), write a function int Valid(Interpretation arrayinter[], int NBInter, Expression E) that checks the validity of the expression. Exercise 28 Write a method and the corresponding recursive function to insert an Entry, passed as a parameter, into a binary tree. If the root is empty, the new entry should be inserted into the root, otherwise it should be inserted into the shorter of the two subtrees of the root (or into the left subtree if both subtrees have the same height). Exercise Suppose that Entry is the type char. Write a function that will print all the entries from a binary tree in the bracketed form (data: LT, RT) where data is the Entry in the root, LT denotes the left subtree of the root printed in bracketed form, and RT denotes the right subtree in bracketed form. For example, the first tree in Figure 10.3 will be printed as (. : (a: (:, ), (:, )), (b: (:, ), (:, ))) 2. Modify the function so that it prints nothing instead of (:, ) for an empty subtree, and x instead of (x:, ) for a subtree consisting of only one node with the Entry x. Hence the preceding tree will now print as (. : a, b). 6
7 Exercise 30 Write a function that will interchange all left and right subtrees in a linked binary tree. Exercise 31 Consider a BST of integers. 1. Write a function that returns the number of common entries in two BST. 2. Write a function that merges 2 BST into a new BST. Exercise We consider a variant of BST where left(n) n < right(n) and this for any node n in the tree. Write a function that calculates the number of distinct values in this kind of binary tree. Give the corresponding declaration. 2. Given 2 BST A and B, write a function that deletes from B all the common elements with A. Exercise 33 A Binary Search Tree (BST) of whole integers can be represented by an array where the root is located at 0 and the contents of the remaining nodes correspond to the course in width of the tree. i.e. every node has two locations for its left and right children. In the case where one (or both) of the two children doesn t exist, the corresponding location will contain the value -1. Ex : The array corresponds to the tree 1. Write the corresponding declaration. 2. Write a function that inserts a natural integer in a BST. 3. Write a function that calculates the number of nodes having only one child in a BST. 4. Write a function that tests if a BT is a Binary Search Tree. Exercise 34 Consider a binary tree T. We define the path from the root to a leaf as the number of edges from the root to that leaf. The minimum path in a tree T is the shortest path among all paths between the root and all leaves. 1. Write a function int min(t) that returns the minimum path in the tree T. 2. Write a function int diff(t) that returns the difference between the minimum and maximum paths in the tree T. 3. A binary tree T is said to be leftist if, for each node N in T, diff(left(n)) diff(right(n)). Write a function int leftest(t) that determines if T is leftist or not. 7
8 Exercise Write a function that returns the value of the father of a given element in a binary tree of integers without repetition. 2. Write a function that returns an array containing the values of all ancestors of a given element in a binary tree of integers without repetition ordered from the nearest to the farthest. Example: the sorted ancestors of 14 in the following tree are: 9, 17, Write a function that returns the value of the nearest common ancestor of two given elements in a binary tree of integers without repetition. Example: the nearest common ancestor of 8 and 5 is 17. Exercise 36 Write a function that displays the elements of a binary tree located at odd left to right and top to bottom. The root of the tree is assumed to be at level 0. Example: Expected result: B C H I J K Exercise 37 A comb left is a locally complete binary tree (each node has 0 or 2 children) in which each right child of a node is a leaf. Write a function isleftcomb that checks if a binary tree is a left comb. Exercise 38 Consider a binary tree of integers dynamically implemented; write a function update that changes node values such that each node is equal to the sum of its children. It is obvious that leaves remain 8
9 unchanged. Example: Exercise 39 A complete binary tree is a binary tree in which all level are completely filled: each non-leaf node has exactly 2 children and all leaves are located in the same level. Thus a tree of depth n has 2n-1 nodes ( ). In this exercise, we represent a complete binary tree statically by an array that corresponds to the tree in width traversal (the root is at the index 0). In this exercise, we suppose that all tree values are distinct. Ex: the below array corresponds to the following BT: We define the ancestors of a leaf L as all nodes that are on the path from L to the root (including L). Thus, in this example, the ancestors of 18 are 18, 5, 4 and Write a function that takes a leaf L in parameter and computes the sum of L ancestors. 2. Write a function that takes in parameters 2 leaves and computes the sum of their ancestors without repetition. Ex: for the leaves 7 and 18, the sum of ancestors is: (4 and 6 are taken into account only once). Exercise 40 ( Exam) A Trees Traversal Consider the following tree:
10 1. (2 pts) Give the inorder traversal of the above tree. 2. (2 pts) Write a recursive function that prints the inorder traversal of a tree dynamically implemented. 3. (10 pts) Write an iterative function that prints the inorder traversal of a tree dynamically implemented. B BST The following function checks whether a given binary tree dynamically implemented is a BST or not. i n t isbst ( Btree t r e e ) i f ( t r e e == NULL) return 1 ; i f ( tree >l e f t!= NULL && FindMax ( tree >l e f t ) > tree >data ) return 0 ; i f ( tree >r i g h t!= NULL && FindMin ( tree >r i g h t ) < tree >data ) return 0 i f (! isbst ( tree >l e f t )! isbst ( tree >r i g h t ) ) return 0 ; r eturn 1 ; } FindMax and FindMin are 2 auxiliary functions that returns the maximum (resp. minimum) element of a BST. element FindMin ( Btree t r e e ) i f ( t r e e==null) return INT MAX; r eturn min ( tree >data, min ( FindMin ( tree >l e f t ), FindMin ( tree >r i g h t ) ) ) ; } element FindMax ( Btree t r e e ) i f ( t r e e==null) r eturn INT MIN ; r eturn max( tree >data, max( FindMax ( tree >l e f t ), FindMax ( tree >r i g h t ) ) ) ; } 1. (1 pt) Without performing calculation, give the worst-case time complexity of the isbst function. Justify your answer. 2. (6 pts) Note that the inorder traversal of BSTs produces sorted lists. While traversing the BST in inorder, and at each node, check the condition that its key value should be greater then the key value of its previous visited node. Write a recursive function that checks whether a given binary tree is a BST or not given the above approach. 3. (1 pt) Without performing calculation, give the worst-case time complexity of your function written in part B-2. Justify your answer. C Max-Heap We are interested in this part to implement a stack of integers using a Max-Heap. Let H be a Max-Heap allowing the following operations: Max-Heap CreateHeap(); This operation creates a Max-Heap. The capacity of the heap is fixed and cannot increase. int insert(max-heap *H, element e); This operation inserts an element e in the Max-Heap if there s room left. The element e could be a complex type and percolate up could be performed on one of its field members. int deletemax(max-heap *H); This operation deletes the maximum element in a Max-Heap if it exists. 10
11 int getmax(max-heap H, element *e); This operation returns the Max-Heap maximum element in e if it exists. int isemptyheap(max-heap H); This operation returns true if the Max-Heap is empty, false otherwise. int isfullheap(max-heap H); This operation returns true if the Max-Heap is full, false otherwise. 1. (2 pts) Give the declaration of types element and stack; 2. (8 pts) Implement a stack using a Max-Heap. D Generic Trees A Generic Tree can be represented as follows: Element First Child Next Sibling A NULL NULL B F L NULL... D NULL NULL The tree node declaration for general trees can be given as: typedef... element ; typedef s t r u c t node element data ; s t r u c t node f i r s t C h i l d ; s t r u c t node n e x t S i b l i n g ; } GenericTree ; 1. (3 pts) Write a function that returns the number of nodes in a Generic Tree. 11
Trees. (Trees) Data Structures and Programming Spring / 28
Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r
More informationBinary Trees, Binary Search Trees
Binary Trees, Binary Search 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, insert, delete)
More informationLec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)
Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)
More informationUses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010
Uses for About Binary January 31, 2010 Uses for About Binary Uses for Uses for About Basic Idea Implementing Binary Example: Expression Binary Search Uses for Uses for About Binary Uses for Storage Binary
More informationCSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes
CSE 250 Final Exam Fall 2013 Time: 3 hours. Dec 11, 2013 Total points: 100 14 pages Please use the space provided for each question, and the back of the page if you need to. Please do not use any extra
More information! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge
Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fall 2018 Jill Seaman!1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every
More informationTrees, Part 1: Unbalanced Trees
Trees, Part 1: Unbalanced Trees The first part of this chapter takes a look at trees in general and unbalanced binary trees. The second part looks at various schemes to balance trees and/or make them more
More informationTree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.
Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except
More informationOutline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications
Trees 1 Outline Preliminaries What is Tree? Implementation of Trees using C++ Tree traversals and applications Binary Trees Binary Search Trees Structure and operations Analysis 2 What is a Tree? A tree
More informationCMSC 341 Lecture 15 Leftist Heaps
Based on slides from previous iterations of this course CMSC 341 Lecture 15 Leftist Heaps Prof. John Park Review of Heaps Min Binary Heap A min binary heap is a Complete binary tree Neither child is smaller
More informationSolution to CSE 250 Final Exam
Solution to CSE 250 Final Exam Fall 2013 Time: 3 hours. December 13, 2013 Total points: 100 14 pages Please use the space provided for each question, and the back of the page if you need to. Please do
More informationCMSC 341 Lecture 15 Leftist Heaps
Based on slides from previous iterations of this course CMSC 341 Lecture 15 Leftist Heaps Prof. John Park Review of Heaps Min Binary Heap A min binary heap is a Complete binary tree Neither child is smaller
More informationCOSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)
COSC 2007 Data Structures II Final Exam Thursday, April 13 th, 2006 This is a closed book and closed notes exam. There are total 3 parts. Please answer the questions in the provided space and use back
More information! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge
Trees (& Heaps) Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Spring 2015 Jill Seaman 1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root -
More informationAlgorithms. Deleting from Red-Black Trees B-Trees
Algorithms Deleting from Red-Black Trees B-Trees Recall the rules for BST deletion 1. If vertex to be deleted is a leaf, just delete it. 2. If vertex to be deleted has just one child, replace it with that
More informationCMSC 341 Leftist Heaps
CMSC 341 Leftist Heaps Based on slides from previous iterations of this course Today s Topics Review of Min Heaps Introduction of Left-ist Heaps Merge Operation Heap Operations Review of Heaps Min Binary
More informationTrees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech
Trees Dr. Ronaldo Menezes Hugo Serrano (hbarbosafilh2011@my.fit.edu) Introduction to Trees Trees are very common in computer science They come in different variations They are used as data representation
More informationCOMP : Trees. COMP20012 Trees 219
COMP20012 3: Trees COMP20012 Trees 219 Trees Seen lots of examples. Parse Trees Decision Trees Search Trees Family Trees Hierarchical Structures Management Directories COMP20012 Trees 220 Trees have natural
More informationTrees. A tree is a directed graph with the property
2: Trees Trees A tree is a directed graph with the property There is one node (the root) from which all other nodes can be reached by exactly one path. Seen lots of examples. Parse Trees Decision Trees
More informationData Structures in Java
Data Structures in Java Lecture 9: Binary Search Trees. 10/7/015 Daniel Bauer 1 Contents 1. Binary Search Trees. Implementing Maps with BSTs Map ADT A map is collection of (key, value) pairs. Keys are
More informationTrees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.
Trees Q: Why study trees? : Many advance DTs are implemented using tree-based data structures. Recursive Definition of (Rooted) Tree: Let T be a set with n 0 elements. (i) If n = 0, T is an empty tree,
More informationINF2220: algorithms and data structures Series 1
Universitetet i Oslo Institutt for Informatikk A. Maus, R.K. Runde, I. Yu INF2220: algorithms and data structures Series 1 Topic Trees & estimation of running time (Exercises with hints for solution) Issued:
More informationAdvanced Set Representation Methods
Advanced Set Representation Methods AVL trees. 2-3(-4) Trees. Union-Find Set ADT DSA - lecture 4 - T.U.Cluj-Napoca - M. Joldos 1 Advanced Set Representation. AVL Trees Problem with BSTs: worst case operation
More informationCSCI2100B Data Structures Trees
CSCI2100B Data Structures Trees Irwin King king@cse.cuhk.edu.hk http://www.cse.cuhk.edu.hk/~king Department of Computer Science & Engineering The Chinese University of Hong Kong Introduction General Tree
More informationEE 368. Weeks 5 (Notes)
EE 368 Weeks 5 (Notes) 1 Chapter 5: Trees Skip pages 273-281, Section 5.6 - If A is the root of a tree and B is the root of a subtree of that tree, then A is B s parent (or father or mother) and B is A
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure
More informationDATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap
DATA STRUCTURES AND ALGORITHMS Hierarchical data structures: AVL tree, Bayer tree, Heap Summary of the previous lecture TREE is hierarchical (non linear) data structure Binary trees Definitions Full tree,
More informationCS102 Binary Search Trees
CS102 Binary Search Trees Prof Tejada 1 To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) Binary Search Trees Binary Search Trees have one
More informationDiscussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:
Discussion 2C Notes (Week 8, February 25) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Trees Definitions Yet another data structure -- trees. Just like a linked
More informationData Structures and Algorithms
Data Structures and Algorithms CS245-2008S-19 B-Trees David Galles Department of Computer Science University of San Francisco 19-0: Indexing Operations: Add an element Remove an element Find an element,
More informationBinary Trees
Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what
More informationBinary Trees and Binary Search Trees
Binary Trees and Binary Search Trees Learning Goals After this unit, you should be able to... Determine if a given tree is an instance of a particular type (e.g. binary, and later heap, etc.) Describe
More informationNET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50
1 NET/JRF-COMPUTER SCIENCE & APPLICATIONS UNIT TEST : DATA STRUCTURE Time: 01 : 00 Hour Date : 02-06-2017 M.M. : 50 INSTRUCTION: Attempt all the 25 questions. Each question carry TWO marks. 1. Consider
More informationAlgorithms. AVL Tree
Algorithms AVL Tree Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other
More informationCS-301 Data Structure. Tariq Hanif
1. The tree data structure is a Linear data structure Non-linear data structure Graphical data structure Data structure like queue FINALTERM EXAMINATION Spring 2012 CS301- Data Structure 25-07-2012 2.
More informationCE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, 4.1 4.2 Izmir University of Economics 1 Preliminaries - I (Recursive) Definition: A tree is a collection of nodes. The
More informationOperations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.
Priority Queue, Heap and Heap Sort In this time, we will study Priority queue, heap and heap sort. Heap is a data structure, which permits one to insert elements into a set and also to find the largest
More informationTREES. Trees - Introduction
TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)
More informationTrees, Binary Trees, and Binary Search Trees
COMP171 Trees, Binary Trees, and Binary Search Trees 2 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,
More information1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1
Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 1. O(logn) 2. O(n) 3. O(nlogn) 4. O(n 2 ) 5. O(2 n ) 2. [1 pt] What is the solution
More informationMulti-way Search Trees! M-Way Search! M-Way Search Trees Representation!
Lecture 10: Multi-way Search Trees: intro to B-trees 2-3 trees 2-3-4 trees Multi-way Search Trees A node on an M-way search tree with M 1 distinct and ordered keys: k 1 < k 2 < k 3
More informationA set of nodes (or vertices) with a single starting point
Binary Search Trees Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search trees Implement the TreeSort algorithm 2 A set of
More informationComputational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs
Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in
More informationChapter 20: Binary Trees
Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other
More informationBinary Search Trees. See Section 11.1 of the text.
Binary Search Trees See Section 11.1 of the text. Consider the following Binary Search Tree 17 This tree has a nice property: for every node, all of the nodes in its left subtree have values less than
More informationMidterm solutions. n f 3 (n) = 3
Introduction to Computer Science 1, SE361 DGIST April 20, 2016 Professors Min-Soo Kim and Taesup Moon Midterm solutions Midterm solutions The midterm is a 1.5 hour exam (4:30pm 6:00pm). This is a closed
More informationWhy Do We Need Trees?
CSE 373 Lecture 6: Trees Today s agenda: Trees: Definition and terminology Traversing trees Binary search trees Inserting into and deleting from trees Covered in Chapter 4 of the text Why Do We Need Trees?
More informationCS350: Data Structures Binary Search Trees
Binary Search Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Introduction to Binary Search Trees A binary search tree is a binary tree that
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques () Lecture 6 January 24, 2018 Binary Search Trees (Lecture notes Chapter 7) Announcements Homework 2: Computing Human Evolution due Tuesday, September 19 th Reading:
More information1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1
Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 2. O(n) 2. [1 pt] What is the solution to the recurrence T(n) = T(n/2) + n, T(1)
More informationCpt S 122 Data Structures. Course Review Midterm Exam # 1
Cpt S 122 Data Structures Course Review Midterm Exam # 1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 1 When: Friday (09/28) 12:10-1pm Where:
More informationTrees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.
Chapter 6 Trees In a hash table, the items are not stored in any particular order in the table. This is fine for implementing Sets and Maps, since for those abstract data types, the only thing that matters
More information9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology
Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive
More informationCourse Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU
Course Review for Midterm Exam 1 Cpt S 223 Fall 2011 1 Midterm Exam 1 When: Friday (10/14) 1:10-2pm Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & in-class
More informationModule 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.
The Lecture Contains: Index structure Binary search tree (BST) B-tree B+-tree Order file:///c /Documents%20and%20Settings/iitkrana1/My%20Documents/Google%20Talk%20Received%20Files/ist_data/lecture13/13_1.htm[6/14/2012
More informationUnit III - Tree TREES
TREES Unit III - Tree Consider a scenario where you are required to represent the directory structure of your operating system. The directory structure contains various folders and files. A folder may
More informationl Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are:
DDS-Heaps 1 Heaps - basics l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are: l insert an object, find the object of minimum key (find
More informationV Advanced Data Structures
V Advanced Data Structures B-Trees Fibonacci Heaps 18 B-Trees B-trees are similar to RBTs, but they are better at minimizing disk I/O operations Many database systems use B-trees, or variants of them,
More informationMulti-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25
Multi-way Search Trees (Multi-way Search Trees) Data Structures and Programming Spring 2017 1 / 25 Multi-way Search Trees Each internal node of a multi-way search tree T: has at least two children contains
More informationData Structure. IBPS SO (IT- Officer) Exam 2017
Data Structure IBPS SO (IT- Officer) Exam 2017 Data Structure: In computer science, a data structure is a way of storing and organizing data in a computer s memory so that it can be used efficiently. Data
More informationB-Trees. Based on materials by D. Frey and T. Anastasio
B-Trees Based on materials by D. Frey and T. Anastasio 1 Large Trees n Tailored toward applications where tree doesn t fit in memory q operations much faster than disk accesses q want to limit levels of
More informationl So unlike the search trees, there are neither arbitrary find operations nor arbitrary delete operations possible.
DDS-Heaps 1 Heaps - basics l Heaps an abstract structure where each object has a key value (the priority), and the operations are: insert an object, find the object of minimum key (find min), and delete
More informationTrees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology
Trees : Part Section. () (2) Preorder, Postorder and Levelorder Traversals Definition: A tree is a connected graph with no cycles Consequences: Between any two vertices, there is exactly one unique path
More informationCS 350 : Data Structures Binary Search Trees
CS 350 : Data Structures Binary Search Trees David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania James Moscola Introduction to Binary Search Trees A binary
More informationCMSC 341 Lecture 10 Binary Search Trees
CMSC 341 Lecture 10 Binary Search Trees John Park Based on slides from previous iterations of this course Review: Tree Traversals 2 Traversal Preorder, Inorder, Postorder H X M A K B E N Y L G W UMBC CMSC
More information[ DATA STRUCTURES ] Fig. (1) : A Tree
[ DATA STRUCTURES ] Chapter - 07 : Trees A Tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst several
More informationTrees. CSE 373 Data Structures
Trees CSE 373 Data Structures Readings Reading Chapter 7 Trees 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical relationships File directories
More informationCMSC 341 Lecture 14: Priority Queues, Heaps
CMSC 341 Lecture 14: Priority Queues, Heaps Prof. John Park Based on slides from previous iterations of this course Today s Topics Priority Queues Abstract Data Type Implementations of Priority Queues:
More informationV Advanced Data Structures
V Advanced Data Structures B-Trees Fibonacci Heaps 18 B-Trees B-trees are similar to RBTs, but they are better at minimizing disk I/O operations Many database systems use B-trees, or variants of them,
More informationLecture 13: AVL Trees and Binary Heaps
Data Structures Brett Bernstein Lecture 13: AVL Trees and Binary Heaps Review Exercises 1. ( ) Interview question: Given an array show how to shue it randomly so that any possible reordering is equally
More informationExam Data structures DAT036/DAT037/DIT960
Exam Data structures DAT036/DAT037/DIT960 Time Thursday 18 th August 2016, 08:30 12:30 Place Maskinhuset / SB Multisal Course responsible Nick Smallbone, tel. 0707 183062 The exam consists of six questions.
More informationData Structures and Algorithms
Data Structures and Algorithms CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco 06-0: Ordered List ADT Operations: Insert an element in the list
More informationCMSC 341. Binary Search Trees CMSC 341 BST
CMSC 341 Binary Search Trees CMSC 341 BST Announcements Homework #3 dues Thursday (10/5/2017) Exam #1 next Thursday (10/12/2017) CMSC 341 BST A Generic Tree CMSC 341 BST Binary Tree CMSC 341 BST The Binary
More informationIntroduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree
Chapter 4 Trees 2 Introduction for large input, even access time may be prohibitive we need data structures that exhibit running times closer to O(log N) binary search tree 3 Terminology recursive definition
More informationCSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.
Name: Class: Date: CSCI-401 Examlet #5 True/False Indicate whether the sentence or statement is true or false. 1. The root node of the standard binary tree can be drawn anywhere in the tree diagram. 2.
More informationComputer Science 210 Data Structures Siena College Fall Topic Notes: Trees
Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,
More informationMID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.
MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions
More informationTree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest
Tree Structures Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest o A tree is a connected digraph with these properties: There is exactly one node (Root)
More information8. Binary Search Tree
8 Binary Search Tree Searching Basic Search Sequential Search : Unordered Lists Binary Search : Ordered Lists Tree Search Binary Search Tree Balanced Search Trees (Skipped) Sequential Search int Seq-Search
More informationA6-R3: DATA STRUCTURE THROUGH C LANGUAGE
A6-R3: DATA STRUCTURE THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be answered in the TEAR-OFF
More informationBinary Trees. Height 1
Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3.
More informationCMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course
CMSC 341 Priority Queues & Heaps Based on slides from previous iterations of this course Today s Topics Priority Queues Abstract Data Type Implementations of Priority Queues: Lists BSTs Heaps Heaps Properties
More informationMIDTERM EXAMINATION Spring 2010 CS301- Data Structures
MIDTERM EXAMINATION Spring 2010 CS301- Data Structures Question No: 1 Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the
More informationCISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees Outline General Trees Terminology, Representation, Properties Binary Trees Representations, Properties, Traversals Recursive Algorithms
More informationSection 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents
Section 5.5 Binary Tree A binary tree is a rooted tree in which each vertex has at most two children and each child is designated as being a left child or a right child. Thus, in a binary tree, each vertex
More informationBinary heaps (chapters ) Leftist heaps
Binary heaps (chapters 20.3 20.5) Leftist heaps Binary heaps are arrays! A binary heap is really implemented using an array! 8 18 29 20 28 39 66 Possible because of completeness property 37 26 76 32 74
More informationCS F-11 B-Trees 1
CS673-2016F-11 B-Trees 1 11-0: Binary Search Trees Binary Tree data structure All values in left subtree< value stored in root All values in the right subtree>value stored in root 11-1: Generalizing BSTs
More informationBalanced Binary Search Trees. Victor Gao
Balanced Binary Search Trees Victor Gao OUTLINE Binary Heap Revisited BST Revisited Balanced Binary Search Trees Rotation Treap Splay Tree BINARY HEAP: REVIEW A binary heap is a complete binary tree such
More informationCSCI Trees. Mark Redekopp David Kempe
CSCI 104 2-3 Trees Mark Redekopp David Kempe Trees & Maps/Sets C++ STL "maps" and "sets" use binary search trees internally to store their keys (and values) that can grow or contract as needed This allows
More informationExercise 1 : B-Trees [ =17pts]
CS - Fall 003 Assignment Due : Thu November 7 (written part), Tue Dec 0 (programming part) Exercise : B-Trees [+++3+=7pts] 3 0 3 3 3 0 Figure : B-Tree. Consider the B-Tree of figure.. What are the values
More informationFINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard
FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures
More informationCS301 - Data Structures Glossary By
CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm
More informationData Structures Question Bank Multiple Choice
Section 1. Fundamentals: Complexity, Algorthm Analysis 1. An algorithm solves A single problem or function Multiple problems or functions Has a single programming language implementation 2. A solution
More informationBinary Trees: Practice Problems
Binary Trees: Practice Problems College of Computing & Information Technology King Abdulaziz University CPCS-204 Data Structures I Warmup Problem 1: Searching for a node public boolean recursivesearch(int
More informationQuestions from the material presented in this lecture
Advanced Data Structures Questions from the material presented in this lecture January 8, 2015 This material illustrates the kind of exercises and questions you may get at the final colloqium. L1. Introduction.
More informationAlgorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs
Algorithms in Systems Engineering ISE 172 Lecture 16 Dr. Ted Ralphs ISE 172 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms
More informationChapter 4: Trees. 4.2 For node B :
Chapter : Trees. (a) A. (b) G, H, I, L, M, and K.. For node B : (a) A. (b) D and E. (c) C. (d). (e).... There are N nodes. Each node has two pointers, so there are N pointers. Each node but the root has
More informationBasic Data Structures (Version 7) Name:
Prerequisite Concepts for Analysis of Algorithms Basic Data Structures (Version 7) Name: Email: Concept: mathematics notation 1. log 2 n is: Code: 21481 (A) o(log 10 n) (B) ω(log 10 n) (C) Θ(log 10 n)
More informationTrees. Truong Tuan Anh CSE-HCMUT
Trees Truong Tuan Anh CSE-HCMUT Outline Basic concepts Trees Trees A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called branches, that connect the nodes
More informationCSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees. Linda Shapiro Spring 2016
CSE373: Data Structures & lgorithms Lecture 6: Binary Search Trees Linda Shapiro Spring 2016 nnouncements HW2 due start of class Wednesday pril 13 on paper. Spring 2016 CSE373: Data Structures & lgorithms
More information