Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

Size: px
Start display at page:

Download "Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues"

Transcription

1 Copyright 1998 by Addison-Wesley Publishing Company 147 Chapter 15 Stacks and Queues

2 Copyright 1998 by Addison-Wesley Publishing Company 148 tos (-1) B tos (1) A tos (0) A A tos (0) How the stack routines work: empty stack, push(a), push(b), pop

3 Copyright 1998 by Addison-Wesley Publishing Company 149 makeempty( ) size = 0 enqueue(a) size = 1 back front back A front enqueue(b) size = 2 A front back B dequeue( ) size = 1 back B front dequeue( ) size = 0 back front Basic array implementation of the queue

4 Copyright 1998 by Addison-Wesley Publishing Company 150 After 3 enqueues size = 3 enqueue(f) size = 4 back F back C D E front C D E front back dequeue( ) F D E size = 3 front back dequeue( ) F E size = 2 front back dequeue( ) F size = 1 front Array implementation of the queue with wraparound

5 Copyright 1998 by Addison-Wesley Publishing Company 151 topofstack D C B A Linked list implementation of the stack

6 Copyright 1998 by Addison-Wesley Publishing Company 152 front back A B C D Linked list implementation of the queue

7 Copyright 1998 by Addison-Wesley Publishing Company 153 back Before... back After... X enqueue operation for linked-list-based implementation

8 Copyright 1998 by Addison-Wesley Publishing Company 154 Chapter 16 Linked Lists

9 Copyright 1998 by Addison-Wesley Publishing Company 155 frontoflist A B C D Basic linked list

10 Copyright 1998 by Addison-Wesley Publishing Company 156 A B current tmp x Insertion into a linked list: create new node (tmp), copy in x, set tmp s next reference, set current s next reference

11 Copyright 1998 by Addison-Wesley Publishing Company 157 A X B current Deletion from a linked list

12 Copyright 1998 by Addison-Wesley Publishing Company 158 A B C header Using a header node for the linked list

13 Copyright 1998 by Addison-Wesley Publishing Company 159 header Empty list when header node is used

14 Copyright 1998 by Addison-Wesley Publishing Company 160 A B head tail Doubly linked list

15 Copyright 1998 by Addison-Wesley Publishing Company 161 head tail Empty doubly linked list

16 Copyright 1998 by Addison-Wesley Publishing Company A B... c a X d b Insertion into a doubly linked list by getting new node and then changing references in order indicated

17 Copyright 1998 by Addison-Wesley Publishing Company 163 A B C D first Circular doubly linked list

18 Copyright 1998 by Addison-Wesley Publishing Company 164 Chapter 17 Trees

19 Copyright 1998 by Addison-Wesley Publishing Company 165 A B C D E F G H I J K A tree

20 Copyright 1998 by Addison-Wesley Publishing Company 166 root... T 1 T 2 T 3 T k Tree viewed recursively

21 Copyright 1998 by Addison-Wesley Publishing Company 167 A B C D E F G H I J K First child/next sibling representation of tree in Figure 17.1

22 Copyright 1998 by Addison-Wesley Publishing Company 168 mark* books* courses*.login dsaa* ecp* ipps* cop3223* cop3530* ch1 ch2 ch1 ch2 ch1 ch2 syl syl Unix directory

23 Copyright 1998 by Addison-Wesley Publishing Company 169 mark books courses.login dsaa ecp ipps cop3223 cop3530 ch1 ch2 ch1 ch2 ch1 ch2 syl syl The directory listing for tree in Figure 17.4

24 Copyright 1998 by Addison-Wesley Publishing Company 170 mark* (1) books* (1) courses* (1).login (2) dsaa* (1) ecp* (1) ipps* (1) cop3223* (1) cop3530* (1) ch1 (9) ch2 (7) ch1 (4) ch2 (6) ch1 (3) ch2 (8) syl (2) syl (3) Unix directory with file sizes

25 Copyright 1998 by Addison-Wesley Publishing Company 171 ch1 9 ch2 7 dsaa 17 ch1 4 ch2 6 ecp 11 ch1 3 ch2 8 ipps 12 books 41 syl 2 cop syl 3 cop courses 8.login 2 mark 52 Trace of the size method

26 Copyright 1998 by Addison-Wesley Publishing Company a * a - d d b c b c Uses of binary trees: left is an expression tree and right is a Huffman coding tree

27 Copyright 1998 by Addison-Wesley Publishing Company 173 root X t1.root t2.root Result of a naive merge operation

28 Copyright 1998 by Addison-Wesley Publishing Company 174 root T1.root oldroot oldt1.root X T2.root Aliasing problems in the merge operation; T1 is also the current object

29 Copyright 1998 by Addison-Wesley Publishing Company 175 S L S R Recursive view used to calculate the size of a tree: S T = S L + S R +1

30 Copyright 1998 by Addison-Wesley Publishing Company 176 H L +1 H L H R H R +1 Recursive view of node height calculation: H T =max(h L +1, H R +1 )

31 Copyright 1998 by Addison-Wesley Publishing Company Preorder, postorder, and inorder visitation routes

32 Copyright 1998 by Addison-Wesley Publishing Company 178 a b c d e a 0 b 0 a 1 b 1 a 1 d 0 b 2 a 1 d 1 b 2 a 1 d 2 b 2 a 1 b 2 a 1 a 1 d b c 0 a 2 e 0 c 1 a 2 e 1 c 1 a 2 e 2 c 1 a 2 c 1 a 2 c 2 a 2 a 2 e c a Stack states during postorder traversal

33 Copyright 1998 by Addison-Wesley Publishing Company 179 Chapter 18 Binary Search Trees

34 Copyright 1998 by Addison-Wesley Publishing Company Two binary trees (only the left tree is a search tree)

35 Copyright 1998 by Addison-Wesley Publishing Company Binary search trees before and after inserting 6

36 Copyright 1998 by Addison-Wesley Publishing Company Deletion of node 5 with one child, before and after

37 Copyright 1998 by Addison-Wesley Publishing Company Deletion of node 2 with two children, before and after

38 Copyright 1998 by Addison-Wesley Publishing Company 184 X X X S L S R S L S R S L S R K < S L + 1 K == S L + 1 K > S L + 1 Using the size data field to implement findkth

39 Copyright 1998 by Addison-Wesley Publishing Company 185 Balanced tree on the left has a depth of log N; unbalanced tree on the right has a depth of N 1

40 Copyright 1998 by Addison-Wesley Publishing Company Binary search trees that can result from inserting a permutation 1, 2, and 3; the balanced tree in the middle is twice as likely as any other

41 Copyright 1998 by Addison-Wesley Publishing Company Two binary search trees: the left tree is an AVL tree, but the right tree is not (unbalanced nodes are darkened)

42 Copyright 1998 by Addison-Wesley Publishing Company 188 H H 1 S H 1 S H 2 H 2 Minimum tree of height H

43 Copyright 1998 by Addison-Wesley Publishing Company 189 k 2 k 1 k 1 k 2 A B C A B C Single rotation to fix case 1

44 Copyright 1998 by Addison-Wesley Publishing Company 190 k 1 k C A B 2 6 k A k B C Single rotation fixes AVL tree after insertion of 1

45 Copyright 1998 by Addison-Wesley Publishing Company 191 k 2 k 1 k 1 k 2 A B C A B C Symmetric single rotation to fix case 4

46 Copyright 1998 by Addison-Wesley Publishing Company 192 k 2 k 1 k 1 k 2 P Q R P Q R Single rotation does not fix case 2

47 Copyright 1998 by Addison-Wesley Publishing Company 193 k 3 k 2 k 1 k 1 k 3 k2 A B C D A B C D Left-right double rotation to fix case 2

48 Copyright 1998 by Addison-Wesley Publishing Company 194 k 1 k 3 A D k 2 B C 5 k 1 k k A B C D Double rotation fixes AVL tree after insertion of 5

49 Copyright 1998 by Addison-Wesley Publishing Company 195 k 1 k 2 k 3 k 1 k 3 k 2 A B C D A B C D Left-right double rotation to fix case 3

50 Copyright 1998 by Addison-Wesley Publishing Company 196 A red-black tree is a binary search tree with the following ordering properties: 1. Every node is colored either red or black. 2. The root is black. 3. If a node is red, its children must be black. 4. Every path from a node to a null reference must contain the same number of black nodes. Red-black tree properties

51 Copyright 1998 by Addison-Wesley Publishing Company Example of a red-black tree; insertion sequence is 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55)

52 Copyright 1998 by Addison-Wesley Publishing Company 198 G P P S X G X C D E A B C S A B D E If S is black, then a single rotation between the parent and grandparent, with appropriate color changes, restores property 3 if X is an outside grandchild

53 Copyright 1998 by Addison-Wesley Publishing Company 199 G X P S P G A X D E A B C S B C D E If S is black, then a double rotation involving X, the parent, and the grandparent, with appropriate color changes, restores property 3 if X is an inside grandchild

54 Copyright 1998 by Addison-Wesley Publishing Company 200 G P P S X G X C D E A B C S A B D E If S is red, then a single rotation between the parent and grandparent, with appropriate color changes, restores property 3 between X and P

55 Copyright 1998 by Addison-Wesley Publishing Company 201 X X C1 C2 C1 C2 Color flip; only if X s parent is red do we continue with a rotation

56 Copyright 1998 by Addison-Wesley Publishing Company Color flip at 50 induces a violation; because it is outside, a single rotation fixes it

57 Copyright 1998 by Addison-Wesley Publishing Company Result of single rotation that fixes violation at node 50

58 Copyright 1998 by Addison-Wesley Publishing Company Insertion of 45 as a red node

59 Copyright 1998 by Addison-Wesley Publishing Company 205 P P X T X T Deletion: X has two black children, and both of its sibling s children are black; do a color flip

60 Copyright 1998 by Addison-Wesley Publishing Company 206 P T X T P R R X Deletion: X has two black children, and the outer child of its sibling is red; do a single rotation

61 Copyright 1998 by Addison-Wesley Publishing Company 207 P R X T P T R X Deletion: X has two black children, and the inner child of its sibling is red; do a double rotation

62 Copyright 1998 by Addison-Wesley Publishing Company 208 X P T T X' P C B C B C X' B X is black and at least one child is red; if we fall through to next level and land on a red child, everything is good; if not, we rotate a sibling and parent

63 Copyright 1998 by Addison-Wesley Publishing Company 209 The level of a node is One if the node is a leaf The level of its parent, if the node is red One less than the level of its parent, if the node is black 1. Horizontal links are right pointers (because only right children may be red). 2. There may not be two consecutive horizontal links (because there cannot be consecutive red nodes). 3. Nodes at level 2 or higher must have two children. 4. If a node does not have a right horizontal link, then its two children are at the same level. AA-tree properties

64 Copyright 1998 by Addison-Wesley Publishing Company AA-tree resulting from insertion of 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55, 35

65 Copyright 1998 by Addison-Wesley Publishing Company 211 P X P X A B C A B C skew is a simple rotation between X and P

66 Copyright 1998 by Addison-Wesley Publishing Company 212 R X R G X G A B A B split is a simple rotation between X and R; note that R s level increases

67 Copyright 1998 by Addison-Wesley Publishing Company After inserting 45 into sample tree; consecutive horizontal links are introduced starting at After split at 35; introduces a left horizontal link at After skew at 50; introduces consecutive horizontal nodes starting at 40

68 Copyright 1998 by Addison-Wesley Publishing Company After split at 40; 50 is now on the same level as 70, thus inducing an illegal left horizontal link After skew at 70; this introduces consecutive horizontal links at After split at 30; insertion is complete

69 Copyright 1998 by Addison-Wesley Publishing Company When 1 is deleted, all nodes become level 1, introducing horizontal left links

70 Copyright 1998 by Addison-Wesley Publishing Company 216 Five-ary tree of 31 nodes has only three levels

71 Copyright 1998 by Addison-Wesley Publishing Company B-tree of order 5

72 Copyright 1998 by Addison-Wesley Publishing Company 218 A B-tree of order M is an M-ary tree with the following properties: 1. The data items are stored at leaves. 2. The nonleaf nodes store up to M 1 keys to guide the searching; key i represents the smallest key in subtree i The root is either a leaf or has between 2 and M children. 4. All nonleaf nodes (except the root) have between M 2 and M children. 5. All leaves are at the same depth and have between L 2 and L children, for some L. B-tree properties

73 Copyright 1998 by Addison-Wesley Publishing Company B-tree after insertion of 57 into tree in Figure 18.70

74 Copyright 1998 by Addison-Wesley Publishing Company Insertion of 55 in B-tree in Figure causes a split into two leaves

75 Copyright 1998 by Addison-Wesley Publishing Company Insertion of 40 in B-tree in Figure causes a split into two leaves and then a split of the parent node

76 Copyright 1998 by Addison-Wesley Publishing Company B-tree after deletion of 99 from Figure 18.73

List of Transparencies

List of Transparencies List of Transparencies Chapter 1 Primitive Java 1 A simple first program 2 The eight primitve types in Java 3 Program that illustrates operators 4 Result of logical operators 5 Examples of conditional

More information

Trees. (Trees) Data Structures and Programming Spring / 28

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 information

CSCI2100B Data Structures Trees

CSCI2100B 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 information

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

9/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 information

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Trees. 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 information

CS350: Data Structures Red-Black Trees

CS350: Data Structures Red-Black Trees Red-Black Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Red-Black Tree An alternative to AVL trees Insertion can be done in a bottom-up or

More information

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department Abstract Data Structures IB Computer Science Content developed by Dartford Grammar School Computer Science Department HL Topics 1-7, D1-4 1: System design 2: Computer Organisation 3: Networks 4: Computational

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Introduction. 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 information

Binary Trees

Binary 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 information

Algorithms. Deleting from Red-Black Trees B-Trees

Algorithms. 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 information

Trees. Eric McCreath

Trees. Eric McCreath Trees Eric McCreath 2 Overview In this lecture we will explore: general trees, binary trees, binary search trees, and AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for:

More information

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

ECE 242 Data Structures and Algorithms.  Trees IV. Lecture 21. Prof. ECE 22 Data Structures and Algorithms http://www.ecs.umass.edu/~polizzi/teaching/ece22/ Trees IV Lecture 2 Prof. Eric Polizzi Summary previous lectures Implementations BST 5 5 7 null 8 null null 7 null

More information

Multi-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 / 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 information

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Module 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 information

CISC 235: Topic 4. Balanced Binary Search Trees

CISC 235: Topic 4. Balanced Binary Search Trees CISC 235: Topic 4 Balanced Binary Search Trees Outline Rationale and definitions Rotations AVL Trees, Red-Black, and AA-Trees Algorithms for searching, insertion, and deletion Analysis of complexity CISC

More information

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Course 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 information

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F]. Question Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F]. a) Draw the linked node structure of L, including

More information

CE 221 Data Structures and Algorithms

CE 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 information

Implementations II 1

Implementations II 1 Implementations II 1 Agenda Trees Terminology!Binary trees Tree traversal! Binary search trees The basic binary search tree Balanced binary search trees AVL-trees Red-black trees AA-trees B-trees 2 Trees

More information

CS301 - Data Structures Glossary By

CS301 - 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 information

Why Do We Need Trees?

Why 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 information

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang) Bioinformatics Programming EE, NCKU Tien-Hao Chang (Darby Chang) 1 Tree 2 A Tree Structure A tree structure means that the data are organized so that items of information are related by branches 3 Definition

More information

Balanced Binary Search Trees

Balanced Binary Search Trees Balanced Binary Search Trees Why is our balance assumption so important? Lets look at what happens if we insert the following numbers in order without rebalancing the tree: 3 5 9 12 18 20 1-45 2010 Pearson

More information

Binary Trees, Binary Search Trees

Binary 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 information

Binary Search Trees. Analysis of Algorithms

Binary Search Trees. Analysis of Algorithms Binary Search Trees Analysis of Algorithms Binary Search Trees A BST is a binary tree in symmetric order 31 Each node has a key and every node s key is: 19 23 25 35 38 40 larger than all keys in its left

More information

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Trees : 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 information

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL MINUTIAE Feedback for P1p1 should have gone out before class Grades on canvas tonight Emails went to the student who submitted the assignment If you did not receive

More information

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved. Data Structures Outline Introduction Linked Lists Stacks Queues Trees Introduction dynamic data structures - grow and shrink during execution Linked lists - insertions and removals made anywhere Stacks

More information

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

DATA 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 information

Data Structure Advanced

Data Structure Advanced Data Structure Advanced 1. Is it possible to find a loop in a Linked list? a. Possilbe at O(n) b. Not possible c. Possible at O(n^2) only d. Depends on the position of loop Solution: a. Possible at O(n)

More information

CS350: Data Structures B-Trees

CS350: Data Structures B-Trees B-Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Introduction All of the data structures that we ve looked at thus far have been memory-based

More information

Data Structures Question Bank Multiple Choice

Data 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 information

Programming II (CS300)

Programming 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 information

Advanced Tree Data Structures

Advanced Tree Data Structures Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park Binary trees Traversal order Balance Rotation Multi-way trees Search Insert Overview

More information

Trees. CSE 373 Data Structures

Trees. 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 information

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

2-3 Tree. Outline B-TREE. catch(...){ printf( Assignment::SolveProblem() AAAA!); } ADD SLIDES ON DISJOINT SETS Outline catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } Balanced Search Trees 2-3 Trees 2-3-4 Trees Slide 4 Why care about advanced implementations? Same entries, different insertion sequence:

More information

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 2004 Goodrich, Tamassia (2,4) Trees 1 Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two children and stores d -1 key-element

More information

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time 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 finish within O(log N) time The theoretical conclusion

More information

Self-Balancing Search Trees. Chapter 11

Self-Balancing Search Trees. Chapter 11 Self-Balancing Search Trees Chapter 11 Chapter Objectives To understand the impact that balance has on the performance of binary search trees To learn about the AVL tree for storing and maintaining a binary

More information

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

CSCI-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 information

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College! Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College! ADTʼs Weʼve Studied! Position-oriented ADT! List! Stack! Queue! Value-oriented ADT! Sorted list! All of these are linear! One previous item;

More information

CS24 Week 8 Lecture 1

CS24 Week 8 Lecture 1 CS24 Week 8 Lecture 1 Kyle Dewey Overview Tree terminology Tree traversals Implementation (if time) Terminology Node The most basic component of a tree - the squares Edge The connections between nodes

More information

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology Chapter 11 Trees Definition of a general tree A general tree T is a set of one or more nodes such that T is partitioned into disjoint subsets: A single node r, the root Sets that are general trees, called

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Algorithms. AVL Tree

Algorithms. 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 information

Chapter 20: Binary Trees

Chapter 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 information

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli 2-3 and 2-3-4 Trees COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli Multi-Way Trees A binary search tree: One value in each node At most 2 children An M-way search tree: Between 1 to (M-1) values

More information

Trees. R. J. Renka 10/14/2011. Department of Computer Science & Engineering University of North Texas. R. J. Renka Trees

Trees. R. J. Renka 10/14/2011. Department of Computer Science & Engineering University of North Texas. R. J. Renka Trees Trees R. J. Renka Department of Computer Science & Engineering University of North Texas 10/14/2011 4.1 Preliminaries Defn: A (rooted) tree is a directed graph (set of nodes and edges) with a particular

More information

Lecture 6: Analysis of Algorithms (CS )

Lecture 6: Analysis of Algorithms (CS ) Lecture 6: Analysis of Algorithms (CS583-002) Amarda Shehu October 08, 2014 1 Outline of Today s Class 2 Traversals Querying Insertion and Deletion Sorting with BSTs 3 Red-black Trees Height of a Red-black

More information

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-12-01 09:30:53 1/54 Chapter-11.pdf (#13) Terminology Definition of a general tree! A general tree T is a set of one or

More information

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-03-25 21:47:41 1/53 Chapter-11.pdf (#4) Terminology Definition of a general tree! A general tree T is a set of one or more

More information

March 20/2003 Jayakanth Srinivasan,

March 20/2003 Jayakanth Srinivasan, Definition : A simple graph G = (V, E) consists of V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements of V called edges. Definition : In a multigraph G = (V, E) two or

More information

CS 350 : Data Structures B-Trees

CS 350 : Data Structures B-Trees CS 350 : Data Structures B-Trees David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania James Moscola Introduction All of the data structures that we ve

More information

CS350: Data Structures AA Trees

CS350: Data Structures AA Trees AA Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Introduction to AA Trees A type of balanced binary search tree Developed as a simpler alternative

More information

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1 Trees Introduction & Terminology Cinda Heeren / Geoffrey Tien 1 Review: linked lists Linked lists are constructed out of nodes, consisting of a data element a pointer to another node Lists are constructed

More information

COMP : Trees. COMP20012 Trees 219

COMP : 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 information

CP2 Revision. theme: dynamic datatypes & data structures

CP2 Revision. theme: dynamic datatypes & data structures CP2 Revision theme: dynamic datatypes & data structures structs can hold any combination of datatypes handled as single entity struct { }; ;

More information

(2,4) Trees. 2/22/2006 (2,4) Trees 1

(2,4) Trees. 2/22/2006 (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 2/22/2006 (2,4) Trees 1 Outline and Reading Multi-way search tree ( 10.4.1) Definition Search (2,4) tree ( 10.4.2) Definition Search Insertion Deletion Comparison of dictionary

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 10: Search and Heaps MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Search and Heaps 2 Linear Search Binary Search Introduction to trees Priority Queues Heaps Linear Search

More information

Trees. A tree is a directed graph with the property

Trees. 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 information

Trees Algorhyme by Radia Perlman

Trees Algorhyme by Radia Perlman Algorhyme by Radia Perlman I think that I shall never see A graph more lovely than a tree. A tree whose crucial property Is loop-free connectivity. A tree which must be sure to span. So packets can reach

More information

3. Fundamental Data Structures

3. Fundamental Data Structures 3. Fundamental Data Structures CH08-320201: Algorithms and Data Structures 233 Data Structures Definition (recall): A data structure is a way to store and organize data in order to facilitate access and

More information

Cpt S 122 Data Structures. Data Structures Trees

Cpt S 122 Data Structures. Data Structures Trees Cpt S 122 Data Structures Data Structures Trees Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Motivation Trees are one of the most important and extensively

More information

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

MIDTERM 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 information

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University Trees Reading: Weiss, Chapter 4 1 Generic Rooted Trees 2 Terms Node, Edge Internal node Root Leaf Child Sibling Descendant Ancestor 3 Tree Representations n-ary trees Each internal node can have at most

More information

Implementations II 1

Implementations II 1 Implementations II 1 Agenda Trees Terminology!Binary trees Tree traversal! Binary search trees The basic binary search tree Balanced binary search trees AVL-trees Red-black trees AA-trees B-trees 2 Trees

More information

Red-black trees (19.5), B-trees (19.8), trees

Red-black trees (19.5), B-trees (19.8), trees Red-black trees (19.5), B-trees (19.8), 2-3-4 trees Red-black trees A red-black tree is a balanced BST It has a more complicated invariant than an AVL tree: Each node is coloured red or black A red node

More information

TREES. Trees - Introduction

TREES. 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 information

Lecture: Analysis of Algorithms (CS )

Lecture: Analysis of Algorithms (CS ) Lecture: Analysis of Algorithms (CS583-002) Amarda Shehu Fall 2017 1 Binary Search Trees Traversals, Querying, Insertion, and Deletion Sorting with BSTs 2 Example: Red-black Trees Height of a Red-black

More information

Section 4 SOLUTION: AVL Trees & B-Trees

Section 4 SOLUTION: AVL Trees & B-Trees Section 4 SOLUTION: AVL Trees & B-Trees 1. What 3 properties must an AVL tree have? a. Be a binary tree b. Have Binary Search Tree ordering property (left children < parent, right children > parent) c.

More information

Search Trees. COMPSCI 355 Fall 2016

Search Trees. COMPSCI 355 Fall 2016 Search Trees COMPSCI 355 Fall 2016 2-4 Trees Search Trees AVL trees Red-Black trees Splay trees Multiway Search Trees (2, 4) Trees External Search Trees (optimized for reading and writing large blocks)

More information

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees 4. Trees 4.1 Preliminaries 4.2 Binary trees 4.3 Binary search trees 4.4 AVL trees 4.5 Splay trees 4.6 B-trees Malek Mouhoub, CS340 Fall 2002 1 4.1 Preliminaries A Root B C D E F G Height=3 Leaves H I J

More information

Analysis of Algorithms

Analysis of Algorithms Analysis of Algorithms Trees-I Prof. Muhammad Saeed Tree Representation.. Analysis Of Algorithms 2 .. Tree Representation Analysis Of Algorithms 3 Nomenclature Nodes (13) Size (13) Degree of a node Depth

More information

Trees. Truong Tuan Anh CSE-HCMUT

Trees. 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 information

Search Trees - 1 Venkatanatha Sarma Y

Search Trees - 1 Venkatanatha Sarma Y Search Trees - 1 Lecture delivered by: Venkatanatha Sarma Y Assistant Professor MSRSAS-Bangalore 11 Objectives To introduce, discuss and analyse the different ways to realise balanced Binary Search Trees

More information

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

Introduction to Trees. D. Thiebaut CSC212 Fall 2014 Introduction to Trees D. Thiebaut CSC212 Fall 2014 A bit of History & Data Visualization: The Book of Trees. (Link) We Concentrate on Binary-Trees, Specifically, Binary-Search Trees (BST) How Will Java

More information

Algorithms. 演算法 Data Structures (focus on Trees)

Algorithms. 演算法 Data Structures (focus on Trees) 演算法 Data Structures (focus on Trees) Professor Chien-Mo James Li 李建模 Graduate Institute of Electronics Engineering National Taiwan University 1 Dynamic Set Dynamic set is a set of elements that can grow

More information

Garbage Collection: recycling unused memory

Garbage Collection: recycling unused memory Outline backtracking garbage collection trees binary search trees tree traversal binary search tree algorithms: add, remove, traverse binary node class 1 Backtracking finding a path through a maze is an

More information

12 July, Red-Black Trees. Red-Black Trees

12 July, Red-Black Trees. Red-Black Trees 1 BST work well if the data is inserted into the tree in random order. They work much slower if the data is inserted in already sorted order. When the values to be inserted are already ordered, a binary

More information

Unit III - Tree TREES

Unit 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 information

Binary search trees (chapters )

Binary search trees (chapters ) Binary search trees (chapters 18.1 18.3) Binary search trees In a binary search tree (BST), every node is greater than all its left descendants, and less than all its right descendants (recall that this

More information

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am Announcements Midterm exam 2, Thursday, May 18 Closed book/notes but one sheet of paper allowed Covers up to stacks and queues Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps

More information

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Lec 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 information

Chapter 12 Advanced Data Structures

Chapter 12 Advanced Data Structures Chapter 12 Advanced Data Structures 2 Red-Black Trees add the attribute of (red or black) to links/nodes red-black trees used in C++ Standard Template Library (STL) Java to implement maps (or, as in Python)

More information

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop. Tree A tree consists of a set of nodes and a set of edges connecting pairs of nodes. A tree has the property that there is exactly one path (no more, no less) between any pair of nodes. A path is a connected

More information

Course Review. Cpt S 223 Fall 2009

Course Review. Cpt S 223 Fall 2009 Course Review Cpt S 223 Fall 2009 1 Final Exam When: Tuesday (12/15) 8-10am Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes Homeworks & program

More information

FINALTERM 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 ( 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 information

Stacks, Queues and Hierarchical Collections

Stacks, Queues and Hierarchical Collections Programming III Stacks, Queues and Hierarchical Collections 2501ICT Nathan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Copyright 2002- by

More information

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Tree: 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 information

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

CSE 373 APRIL 17 TH TREE BALANCE AND AVL CSE 373 APRIL 17 TH TREE BALANCE AND AVL ASSORTED MINUTIAE HW3 due Wednesday Double check submissions Use binary search for SADict Midterm text Friday Review in Class on Wednesday Testing Advice Empty

More information

MID 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. 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 information

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1 Queues ADT description Implementations Cinda Heeren / Geoffrey Tien 1 Queues Assume that we want to store data for a print queue for a student printer Student ID Time File name The printer is to be assigned

More information

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Hierarchical data structures. Announcements. Motivation for trees. Tree overview Announcements Midterm exam 2, Thursday, May 18 Closed book/notes but one sheet of paper allowed Covers up to stacks and queues Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps

More information

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC) SET - 1 II B. Tech I Semester Supplementary Examinations, May/June - 2016 PART A 1. a) Write a procedure for the Tower of Hanoi problem? b) What you mean by enqueue and dequeue operations in a queue? c)

More information

CSE 530A. B+ Trees. Washington University Fall 2013

CSE 530A. B+ Trees. Washington University Fall 2013 CSE 530A B+ Trees Washington University Fall 2013 B Trees A B tree is an ordered (non-binary) tree where the internal nodes can have a varying number of child nodes (within some range) B Trees When a key

More information

Name CPTR246 Spring '17 (100 total points) Exam 3

Name CPTR246 Spring '17 (100 total points) Exam 3 Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in

More information

Table of Contents. Chapter 1: Introduction to Data Structures... 1

Table of Contents. Chapter 1: Introduction to Data Structures... 1 Table of Contents Chapter 1: Introduction to Data Structures... 1 1.1 Data Types in C++... 2 Integer Types... 2 Character Types... 3 Floating-point Types... 3 Variables Names... 4 1.2 Arrays... 4 Extraction

More information

CSE 230 Intermediate Programming in C and C++ Binary Tree

CSE 230 Intermediate Programming in C and C++ Binary Tree CSE 230 Intermediate Programming in C and C++ Binary Tree Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu Introduction to Tree Tree is a non-linear data structure

More information

Binary search trees (BST) Binary search trees (BST)

Binary search trees (BST) Binary search trees (BST) Tree A tree is a structure that represents a parent-child relation on a set of object. An element of a tree is called a node or vertex. The root of a tree is the unique node that does not have a parent

More information