Search Structures. Kyungran Kang

Size: px
Start display at page:

Download "Search Structures. Kyungran Kang"

Transcription

1 Search Structures Kyungran Kang Ellis Horowitz, Sartaj Sahni and Susan Anderson-Freed, Fundamentals of Data Structures in C, 2nd Edition, Silicon Press, 2007.

2 Contents Binary Search Trees AVL Trees Red-Black Trees 2-3 Trees Trees B-Trees Tries -2-

3 Binary Search Tree A possible binary search tree with items (if, do, while) if do while A possible binary search tree with items (if, do, while, void, for) for for do while do void if void Are they optimal??? if while -3-

4 Evaluation of Binary Search Tree (1) Extended binary search tree Placing a special square node at every null link External node: not part of original tree Internal node: part of original tree do if while Cost of binary search tree Assume the binary search tree contains the identifiers a 1, a 2,, a n with a 1 < a 2 < < a n When only successful search is made, the cost of binary search tree is i= 1 level( where p i is the probability of searching for a i n p i a i ) -4-

5 Evaluation of Binary Search Tree (2) To take the unsuccessful searches into consideration, We consider every external node as failure node We may partition the identifiers not in the binary search tree into n+1 classes E i, 0 i n E 0 contains all identifiers x < a 1 E i contains all identifiers x such that a i < x < a i+1 Total cost of binary search tree n i= 1 p i level ( a i= 0 Optimal binary search tree i ) + n q i ( level ( failure node 1) where q i is the probability of searching for identifier in E i Minimizes the cost over all possible binary search trees for a given set of identifiers n i= 1 i) n p i + q i= 0 i = 1-5-

6 Performance of Binary Search Tree Time complexity of binary search tree average case: O(log 2 n) worst case: O(n) If we maintain the binary search tree as a complete binary tree Minimize the average and maximum search time Average and worst case: O(log 2 n) Significant increase in the time required to add new element because of reconstruction of the tree Method of growing balanced binary trees Balanced binary trees Average and worst case: O(log 2 n) -6-

7 AVL Trees (1) An AVL tree is a kind of balanced binary search tree, named after its inventors, Adelson-Velskii and Landis in 1962 Height balanced binary tree An empty binary tree is height balanced If T is a nonempty binary tree with T L and T R as its left and right subtrees, T is height balanced iff T L and T R are height balanced, and h L - h R 1 where h L and h R are height of T L and T R, respectively Balance factor, BF(T), of node T in a binary tree (h L h R ), where h L and h R are heights of left and right subtree of T For any node T in an AVL tree BF(T) = -1, 0, or 1-7-

8 AVL Trees (2) bf=+2 bf=+2 Yes, it s AVL tree No, it s not AVL tree -8-

9 AVL Trees (3) A rotation is a local operation in a search tree that preserves inorder traversal key ordering Rebalancing using four kinds of rotations Y: new inserted node, A: the nearest ancestor of Y, whose balance factor becomes ±2 LL : Y is inserted in the left subtree of left subtree of A LR : Y is inserted in the right subtree of the left subtree of A RR : Y is inserted in the right subtree of the right subtree of A RL : Y is inserted in the left subtree of the right subtree of A Height of the subtrees which are not involved in the rotation remain unchanged Insertion into an AVL tree Time to insert a new identifier: O(h), where h is the height of the tree before insertion -9-

10 AVL Tree Rebalancing bf=+2 H I J LL rotation H bf=-2 I I I H J H J J RR rotation bf=+2 H J I H I J H I bf=-2 J H I J LR rotation RL rotation -10-

11 Rebalancing in AVL Tree LL Rotation B h-1 A A R h-2 h Insert a node at B L bf=+2 B h A A R h+1 h-2 B L B R B L B R B LL Rotation h A B L B R -11- A R

12 Rebalancing in AVL Tree LR Rotation (1) B A h Insert a node at C L bf=+2 B A h-1 C h-2 h C h-2 B L C L C R A R B L C L C R A R B C A LR Rotation h B L C L C R A R -12-

13 Rebalancing in AVL Tree LR Rotation (2) B A h Insert a node at C R bf=+2 B A h-1 B L C C L C R A R h-2 h B L C h-2 C L C R A R C B A LR Rotation B L C L C R A R -13-

14 Rebalancing in AVL Tree RR Rotation h-2 A L B L B R A B h-1 h Insert a node at B R h-2 bf=-2 A L B L B R A B h B RR Rotation A A L B L -14- B R

15 Rebalancing in AVL Tree RL Rotation (1) A C Insert a node at B L bf=-2 A C h-2 B h h-2 B h A L h-1 A L B L B R C R B L B R C R A B C RL Rotation A L B L B R C R -15-

16 Rebalancing in AVL Tree RL Rotation (2) A C h Insert a node at B R bf=-2 A C h-2 B h-2 B h A L h-1 A L B L B R C R B L B R C R B A C RL Rotation h A L B L B R C R -16-

17 AVL Tree Implementation in C (1) #define struct { int key; } element; typedef struct treenode *treepointer; struct treenode { treepointer leftchild; element data; short int bf; treepointer rightchild; }; int unbalanced = FALSE; /* the tree is balanced */ treepointer root=null; -17-

18 AVL Tree Implementation in C (2) void avlinsert(treepointer *parent, element x, int *unbalanced) { if (!*parent) {/* insert element into the tree */ *unbalanced = TRUE; *parent= (treepointer)malloc(sizeof(treenode)); (*parent)->leftchild =(*parent)->rightchild = NULL; (*parent)->; (*parent)->data=x; } else if(x.key < (*parent)->data.key) { avlinsert(&(*parent)->leftchild, x, unbalanced); if(*unbalanced) /* left branch has grown higher */ switch((*parent)->bf) { case -1: (*parent)->bf = 0; *unbalanced = FALSE; break; case 0: (*parent)->bf = 1; break; case 1: leftrotation(parent, unbalanced); } p.503 Program 10.3 } -18-

19 AVL Tree Implementation in C (3) else if(x.key > (*parent)->data.key) { avlinsert(*(*parent)->rightchild, x, unbalanced); if(*unbalanced) /*right branch has grown higher */ switch((*parent)->bf) { case 1: (*parent)->bf = 0; *unbalanced = FALSE; break; case 0: (*parent)->bf = -1; break; case -1: rightrotation(parent, unbalanced); } } else { *unbalanced = 0; printf ( the key is already in the tree\n ); } } /* end of avlinsert */ -19-

20 Example of AVL Tree Insertion (1) (a) insert ch (b) insert (c) insert ember bf=-2 RR rotation -20-

21 Example of AVL Tree Insertion (2) (d) insert August Aug (e) insert il Aug bf=+2 LL rotation Aug -21-

22 Example of AVL Tree Insertion (3) (f) insert January Aug bf=+2 LR rotation Aug Jan Jan -22-

23 Example of AVL Tree Insertion (4) (g) insert December Aug Dec Jan (h) insert July Aug Dec Jan Jul -23-

24 Example of AVL Tree Insertion (5) (i) insert February bf=-2 Aug Jan RL rotation Aug Dec Jan Dec Feb Jul Feb Jul -24-

25 Example of AVL Tree Insertion (6) (j) insert June Aug Dec Feb bf=+2 Jan Jul Jun LR rotation Aug Jan Dec Feb Jul Jun -25-

26 Example of AVL Tree Insertion (7) (k) insert October Jan Dec Aug Jan Dec Feb Jul Aug Feb RR rotation Jul bf=-2 Jun Oct Jun Oct -26-

27 Example of AVL Tree Insertion (8) (l) insert September Aug Dec Feb Jan Jul Jun Oct Sep -27-

28 Deletion from AVL Tree (1) Delete a node x as in ordinary binary search tree. Note that the last node deleted is a leaf. Then trace the path from the new leaf towards the root. For each node x encountered, check if bf <2 If yes, proceed to parent(x) If not, perform an appropriate rotation at x For deletion, after we perform a rotation at x, we may have to perform a rotation at some ancestor of x. Thus, we must continue to trace the path until we reach the root -28-

29 Deletion from AVL Tree Replacement? Aug? Dec Jan Jul Dec Jul Jan? Jul? Jan Jul Dec?? Jan -29-

30 Deletion from AVL Tree Example (1) Delete Jan Dec Aug Feb Jul Jun Jan Oct -2 RR case in insertion Dec Sep Aug Feb Jul Oct Jun Sep -30-

31 Deletion from AVL Tree Example (2) Delete Feb Jan bf=1 LL case in 2 insertion Aug Dec Feb Jul -2 Aug Jan Jun Oct Dec Jul Sep Jun Oct RR case in insertion Sep -31-

32 Deletion from AVL Tree Example (3) bf=-2 Jan Aug Jan Dec Jul Aug Jul Oct Jun Oct Dec Jun Sep Sep -32-

33 Comparison of Various Structures Operation Sequential list (sorted) Linked list AVL tree Search for x O(log n) O(n) O(log n) Search for kth item O(1) O(k) O(log n) Delete x O(n) O(1) (doubly linked or position is known) O(log n) Delete kth item O(n-k) O(k) O(log n) Insert x O(n) O(1) (if position is known) O(log n) -33-

34 Red-Black Trees (1) A red-black tree is a binary search tree with one extra attribute for each node: the color, which is either red or black Red-Black Trees are one of the preferred methods of maintaining binary search trees The Red-Black Tree was invented by Rudolf Bayer in 1972 The trees were originally called Symmetric Binary B- Trees They were renamed "Red-Black Trees" by Leonidas J. Guibas and Robert Sedgewick in

35 Red-Black Trees (2) A binary search tree is a red-black tree if: The root and all leave nodes (terminal nodes) are colored black A node is either red or black On any path from the root to a leaf, red nodes must not be adjacent (color invariant) Every simple path from a given node to any of its descendant leaves contains the same number of black nodes (height invariant) -35-

36 Red-Black Trees (3) Black-height of a node x, bh(x), is the number of black nodes on any path from x to a leaf, not counting x A red-black tree with n internal nodes has height at most 2 log 2 (n+1) Approximate balancing is performed during insertion and deletion operations The cost is O(log n) instead of O(n) for a full rebalance after insertion -36-

37 Red-Black Trees - Example h = 4 26 bh = 2 h = 1 bh = 1 17 h = 2 41 bh = 1 NIL NIL NIL h = 3 bh = 2 30 h = 1 47 bh = NIL h = 2 bh = 1 h = 1 bh = 1 NIL NIL NIL NIL -37-

38 Red-Black Tree Implementation in C typedef enum {red, black} color; typedef struct redblack *redblackptr; Typedef struct redblack { element data; redblackptr leftchild; redblackptr rightchild; color Color; } -38-

39 Red-Black Tree Insertion (1) Insert node as usual in BST Color the node Red Check what Red-Black property is violated Every node is Red or Black? NULLs are Black? If node is Red, both children must be Black? Every path from node to leaf nodes must contain the same number of Blacks? -39-

40 Red-Black Tree Insertion (2) Imbalances LLb : pu is LEFT CHILD of gu, u is LEFT CHILD of pu, and uncle is black LLr : pu is LEFT CHILD of gu, u is LEFT CHILD of pu, and uncle is red LRb : pu is LEFT CHILD of gu, u is RIGHT CHILD of pu, and uncle is black LRr : pu is LEFT CHILD of gu, u is RIGHT CHILD of pu, and uncle is red RRb, RRr, RLb, RLr Imbalances of the type Xyr are handled by changing colors Imbalances of the type Xyb require a rotation -40-

41 Red-Black Tree Insertion (3) gu gu pu gur pu gur u pur u pur ul ur (a) LLr imbalance ul ur (b) After LLr color change gu gu pu gur pu gur pul u pul u ul ur (c) LRr imbalance ul ur (d) After LRr color change -41-

42 Red-Black Tree Insertion (4) gu pu pu gur pul gu pul pur (a) LLb imbalance pur gur (b) After LLb rotation gu u pu gur pu gu pul u pul ul ur gur ul ur (c) LRb imbalance (d) After LRb rotation -42-

43 Red-Black Tree Insertion Example (1) (a) Initial (b) Insert pu gu u pu u (c) Insert 60 (d) LLr color change -43-

44 Red-Black Tree Insertion Example (2) 10 pu 50 gu u (e) Insert 65 (f) LRb rotation -44-

45 Red-Black Tree Insertion Example (3) gu gu 50 u pu 90 pu u (g) Insert 62 (h) LRr color change (i) RLb rotation -45-

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013)

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013) More Binary Search Trees AVL Trees bstdelete if (key not found) return else if (either subtree is empty) { delete the node replacing the parents link with the ptr to the nonempty subtree or NULL if both

More information

More BSTs & AVL Trees bstdelete

More BSTs & AVL Trees bstdelete More BSTs & AVL Trees bstdelete if (key not found) return else if (either subtree is empty) { delete the node replacing the parents link with the ptr to the nonempty subtree or NULL if both subtrees are

More information

Data Structure. Chapter 10 Search Structures (Part II)

Data Structure. Chapter 10 Search Structures (Part II) Data Structure Chapter 1 Search Structures (Part II) Instructor: ngela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 29 Spring Outline VL trees Introduction

More information

COMP171. AVL-Trees (Part 1)

COMP171. AVL-Trees (Part 1) COMP11 AVL-Trees (Part 1) AVL Trees / Slide 2 Data, a set of elements Data structure, a structured set of elements, linear, tree, graph, Linear: a sequence of elements, array, linked lists Tree: nested

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

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

CS350: Data Structures AVL Trees

CS350: Data Structures AVL Trees S35: Data Structures VL Trees James Moscola Department of Engineering & omputer Science York ollege of Pennsylvania S35: Data Structures James Moscola Balanced Search Trees Binary search trees are not

More information

Data Structure - Advanced Topics in Tree -

Data Structure - Advanced Topics in Tree - Data Structure - Advanced Topics in Tree - AVL, Red-Black, B-tree Hanyang University Jong-Il Park AVL TREE Division of Computer Science and Engineering, Hanyang University Balanced binary trees Non-random

More information

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6 COMP11 Spring 008 AVL Trees / Slide Balanced Binary Search Tree AVL-Trees Worst case height of binary search tree: N-1 Insertion, deletion can be O(N) in the worst case We want a binary search tree with

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 21, 2018 Outline Outline 1 C++ Supplement 1.3: Balanced Binary Search Trees Balanced Binary Search Trees Outline

More information

Ch04 Balanced Search Trees

Ch04 Balanced Search Trees Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 05 Ch0 Balanced Search Trees v 3 8 z Why care about advanced implementations? Same entries,

More information

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17 AVL Trees (AVL Trees) Data Structures and Programming Spring 2017 1 / 17 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

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

ECE250: Algorithms and Data Structures AVL Trees (Part A)

ECE250: Algorithms and Data Structures AVL Trees (Part A) ECE250: Algorithms and Data Structures AVL Trees (Part A) Ladan Tahvildari, PEng, SMIEEE Associate Professor Software Technologies Applied Research (STAR) Group Dept. of Elect. & Comp. Eng. University

More information

8.1. Optimal Binary Search Trees:

8.1. Optimal Binary Search Trees: DATA STRUCTERS WITH C 10CS35 UNIT 8 : EFFICIENT BINARY SEARCH TREES 8.1. Optimal Binary Search Trees: An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such

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

8. Binary Search Tree

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

AVL Trees (10.2) AVL Trees

AVL Trees (10.2) AVL Trees AVL Trees (0.) CSE 0 Winter 0 8 February 0 AVL Trees AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by

More information

Part 2: Balanced Trees

Part 2: Balanced Trees Part 2: Balanced Trees 1 AVL Trees We could dene a perfectly balanced binary search tree with N nodes to be a complete binary search tree, one in which every level except the last is completely full. A

More information

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor COSC160: Data Structures Balanced Trees Jeremy Bolton, PhD Assistant Teaching Professor Outline I. Balanced Trees I. AVL Trees I. Balance Constraint II. Examples III. Searching IV. Insertions V. Removals

More information

Balanced Search Trees. CS 3110 Fall 2010

Balanced Search Trees. CS 3110 Fall 2010 Balanced Search Trees CS 3110 Fall 2010 Some Search Structures Sorted Arrays Advantages Search in O(log n) time (binary search) Disadvantages Need to know size in advance Insertion, deletion O(n) need

More information

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees Some Search Structures Balanced Search Trees Lecture 8 CS Fall Sorted Arrays Advantages Search in O(log n) time (binary search) Disadvantages Need to know size in advance Insertion, deletion O(n) need

More information

CS Transform-and-Conquer

CS Transform-and-Conquer CS483-11 Transform-and-Conquer Instructor: Fei Li Room 443 ST II Office hours: Tue. & Thur. 1:30pm - 2:30pm or by appointments lifei@cs.gmu.edu with subject: CS483 http://www.cs.gmu.edu/ lifei/teaching/cs483_fall07/

More information

In a non-ideal situation, we can allow the binary tree to grow to twice the height of the perfect tree (2 lg n) and periodically balance it

In a non-ideal situation, we can allow the binary tree to grow to twice the height of the perfect tree (2 lg n) and periodically balance it Balanced Trees bst algorithms can degenerate to worst case performance, which is bad because the worst case is likely to occur in practice, with ordered files, for example We will like to keep our trees

More information

CS 261 Data Structures. AVL Trees

CS 261 Data Structures. AVL Trees CS 261 Data Structures AVL Trees 1 Binary Search Tree Complexity of BST operations: proportional to the length of the path from a node to the root Unbalanced tree: operations may be O(n) E.g.: adding elements

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

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

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE CSI Section E01 AVL Trees AVL Property While BST structures have average performance of Θ(log(n))

More information

UNIT III TREES 3.1 Basic Terminologies Root Child Parent Siblings Descendant Ancestor Leaf Inte rnal node External node Degree Edge Path Level

UNIT III TREES 3.1 Basic Terminologies Root Child Parent Siblings Descendant Ancestor Leaf Inte rnal node External node Degree Edge Path Level UNIT III TREES 3.1 Basic Terminologies Terminologies used in Trees Root The top node in a tree. Child A node directly connected to another node when moving away from the Root. Parent The converse notion

More information

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version. Announcements: Prelim tonight! 7:30-9:00 in Thurston 203/205 o Handed back in section tomorrow o If you have a conflict you can take the exam at 5:45 but can t leave early. Please email me so we have a

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

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

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

CHAPTER 5. Trees CHAPTER 5 1/70

CHAPTER 5. Trees CHAPTER 5 1/70 CHAPTER 5 Trees All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed Fundamentals of Data Structures in C /2nd Edition, Silicon Press, 2008. CHAPTER 5

More information

Balanced search trees. DS 2017/2018

Balanced search trees. DS 2017/2018 Balanced search trees. DS 2017/2018 Red-black trees Symmetric binary B-tree, Rudolf Bayer, 1972. The balancing is maintained by using a coloring of the nodes. The red-black trees are binary search trees

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

Balanced Binary Search Trees

Balanced Binary Search Trees Balanced Binary Search Trees Pedro Ribeiro DCC/FCUP 2017/2018 Pedro Ribeiro (DCC/FCUP) Balanced Binary Search Trees 2017/2018 1 / 48 Motivation Let S be a set of comparable objects/items: Let a and b be

More information

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1 AVL Trees v 6 3 8 z 20 Goodrich, Tamassia, Goldwasser AVL Trees AVL Tree Definition Adelson-Velsky and Landis binary search tree balanced each internal node v the heights of the children of v can 2 3 7

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees PLSD210 Searching - Re-visited Binary tree O(log n) if it stays balanced Simple binary tree good for static collections

More information

Advanced Set Representation Methods

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

Binary Trees. Height 1

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

CIS265/ Trees Red-Black Trees. Some of the following material is from:

CIS265/ Trees Red-Black Trees. Some of the following material is from: CIS265/506 2-3-4 Trees Red-Black Trees Some of the following material is from: Data Structures for Java William H. Ford William R. Topp ISBN 0-13-047724-9 Chapter 27 Balanced Search Trees Bret Ford 2005,

More information

CMPE 160: Introduction to Object Oriented Programming

CMPE 160: Introduction to Object Oriented Programming CMPE 6: Introduction to Object Oriented Programming General Tree Concepts Binary Trees Trees Definitions Representation Binary trees Traversals Expression trees These are the slides of the textbook by

More information

CS 380 ALGORITHM DESIGN AND ANALYSIS

CS 380 ALGORITHM DESIGN AND ANALYSIS CS 380 ALGORITHM DESIGN AND ANALYSIS Lecture 12: Red-Black Trees Text Reference: Chapters 12, 13 Binary Search Trees (BST): Review Each node in tree T is a object x Contains attributes: Data Pointers to

More information

CS200: Balanced Search Trees

CS200: Balanced Search Trees Value Oriented Data Structures CS200: Balanced Search Trees Walls & Mirrors Chapters 12,13 Homework 4 extension Next week: Programming quiz during recit Midterm 2 April 8 th (in class) New partners and

More information

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya CS62: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Balanced search trees Balanced search tree: A search-tree data structure for which a height of O(lg n) is guaranteed when

More information

TREES Lecture 12 CS2110 Spring 2018

TREES Lecture 12 CS2110 Spring 2018 TREES Lecture 12 CS2110 Spring 2018 Important Announcements 2 A4 is out now and due two weeks from today. Have fun, and start early! Data Structures 3 There are different ways of storing data, called data

More information

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya CS62: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Binary Search Tree - Best Time All BST operations are O(d), where d is tree depth minimum d is d = ëlog for a binary tree

More information

Data Structures in Java

Data Structures in Java Data Structures in Java Lecture 10: AVL Trees. 10/1/015 Daniel Bauer Balanced BSTs Balance condition: Guarantee that the BST is always close to a complete binary tree (every node has exactly two or zero

More information

Trees. Prof. Dr. Debora Weber-Wulff

Trees. Prof. Dr. Debora Weber-Wulff Trees Prof. Dr. Debora Weber-Wulff Flickr, _marmota, 2007 Major Sources Michell Waite & Robert Lafore, Data Structures & Algorithms in Java Michael T. Goodrich and Roberto Tamassia Data Structures and

More information

Search Trees (Ch. 9) > = Binary Search Trees 1

Search Trees (Ch. 9) > = Binary Search Trees 1 Search Trees (Ch. 9) < 6 > = 1 4 8 9 Binary Search Trees 1 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestbefore(k) closestafter(k) Binary Search Trees Binary

More information

Binary Search Tree: Balanced. Data Structures and Algorithms Emory University Jinho D. Choi

Binary Search Tree: Balanced. Data Structures and Algorithms Emory University Jinho D. Choi Binary Search Tree: Balanced Data Structures and Algorithms Emory University Jinho D. Choi Binary Search Tree Worst-case Complexity Search Insert Delete Unbalanced O(n) O(n) O(n) + α Balanced O(log n)

More information

Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees

Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees Reading materials Dale, Joyce, Weems: 9.1, 9.2, 8.8 Liang: 26 (comprehensive edition) OpenDSA:

More information

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium CSC 172 Data Structures and Algorithms Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium Announcement Coming week: Nov 19 Nov 25 No Quiz No Workshop No New Lab Monday and Tuesday: regular Lab

More information

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 0 AVL Trees v 6 3 8 z 0 Goodrich, Tamassia, Goldwasser

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

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees Computer Science 10 Data Structures Siena College Fall 018 Topic Notes: Binary Search Trees Possibly the most common usage of a binary tree is to store data for quick retrieval. Definition: A binary tree

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Spring 2009-2010 Outline BST Trees (contd.) 1 BST Trees (contd.) Outline BST Trees (contd.) 1 BST Trees (contd.) The bad news about BSTs... Problem with BSTs is that there

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

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS LECTURE 13 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 12... Binary Search Trees Binary Tree Traversals Huffman coding Binary Search Tree Today Binary Search

More information

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees // AVL Trees AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel son-vel skii and Landis AVL tree approximates the ideal tree (completely balanced

More information

Red-black tree. Background and terminology. Uses and advantages

Red-black tree. Background and terminology. Uses and advantages Red-black tree A red-black tree is a type of self-balancing binary search tree, a data structure used in computer science, typically used to implement associative arrays. The original structure was invented

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

Balanced BST. Balanced BSTs guarantee O(logN) performance at all times

Balanced BST. Balanced BSTs guarantee O(logN) performance at all times Balanced BST Balanced BSTs guarantee O(logN) performance at all times the height or left and right sub-trees are about the same simple BST are O(N) in the worst case Categories of BSTs AVL, SPLAY trees:

More information

3137 Data Structures and Algorithms in C++

3137 Data Structures and Algorithms in C++ 3137 Data Structures and Algorithms in C++ Lecture 4 July 17 2006 Shlomo Hershkop 1 Announcements please make sure to keep up with the course, it is sometimes fast paced for extra office hours, please

More information

Binary Trees. Examples:

Binary Trees. Examples: Binary Trees A tree is a data structure that is made of nodes and pointers, much like a linked list. The difference between them lies in how they are organized: In a linked list each node is connected

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees Computer Science 10 Data Structures Siena College Fall 016 Topic Notes: Binary Search Trees Possibly the most common usage of a binary tree is to store data for quick retrieval. Definition: A binary tree

More information

Data Structures Lesson 7

Data Structures Lesson 7 Data Structures Lesson 7 BSc in Computer Science University of New York, Tirana Assoc. Prof. Dr. Marenglen Biba 1-1 Binary Search Trees For large amounts of input, the linear access time of linked lists

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

Data Structures and Algorithms(12)

Data Structures and Algorithms(12) Ming Zhang "Data s and Algorithms" Data s and Algorithms(12) Instructor: Ming Zhang Textbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher Education Press, 28.6 (the "Eleventh Five-Year" national

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

Data Structure - Binary Tree 1 -

Data Structure - Binary Tree 1 - Data Structure - Binary Tree 1 - Hanyang University Jong-Il Park Basic Tree Concepts Logical structures Chap. 2~4 Chap. 5 Chap. 6 Linear list Tree Graph Linear structures Non-linear structures Linear Lists

More information

Assignment 4 - AVL Binary Search Trees

Assignment 4 - AVL Binary Search Trees Assignment 4 - AVL Binary Search Trees MTE 140 - Data Structures and Algorithms DUE: July 22-11:59 PM 1 Introduction For this assignment, you will be implementing a basic AVL tree. AVL trees are an important

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

Lecture 16 AVL Trees

Lecture 16 AVL Trees Lecture 16 AVL Trees 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning Binary search trees are an excellent data structure to implement associative arrays, maps, sets, and similar

More information

Algorithms. Red-Black Trees

Algorithms. Red-Black Trees Algorithms Red-Black Trees Red-Black Trees Balanced binary search trees guarantee an O(log n) running time Red-black-tree Binary search tree with an additional attribute for its nodes: color which can

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

Transform & Conquer. Presorting

Transform & Conquer. Presorting Transform & Conquer Definition Transform & Conquer is a general algorithm design technique which works in two stages. STAGE : (Transformation stage): The problem s instance is modified, more amenable to

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

A set of nodes (or vertices) with a single starting point

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

Data Structures Week #6. Special Trees

Data Structures Week #6. Special Trees Data Structures Week #6 Special Trees Outline Adelson-Velskii-Landis (AVL) Trees Splay Trees B-Trees October 5, 2015 Borahan Tümer, Ph.D. 2 AVL Trees October 5, 2015 Borahan Tümer, Ph.D. 3 Motivation for

More information

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

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

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree 0/2/ Preview Binary Tree Tree Binary Tree Property functions In-order walk Pre-order walk Post-order walk Search Tree Insert an element to the Tree Delete an element form the Tree A binary tree is a tree

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

Red-Black, Splay and Huffman Trees

Red-Black, Splay and Huffman Trees Red-Black, Splay and Huffman Trees Kuan-Yu Chen ( 陳冠宇 ) 2018/10/22 @ TR-212, NTUST AVL Trees Review Self-balancing binary search tree Balance Factor Every node has a balance factor of 1, 0, or 1 2 Red-Black

More information

Properties of red-black trees

Properties of red-black trees Red-Black Trees Introduction We have seen that a binary search tree is a useful tool. I.e., if its height is h, then we can implement any basic operation on it in O(h) units of time. The problem: given

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

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

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

Data Structures Week #6. Special Trees

Data Structures Week #6. Special Trees Data Structures Week #6 Special Trees Outline Adelson-Velskii-Landis (AVL) Trees Splay Trees B-Trees 21.Aralık.2010 Borahan Tümer, Ph.D. 2 AVL Trees 21.Aralık.2010 Borahan Tümer, Ph.D. 3 Motivation for

More information

Fall, 2015 Prof. Jungkeun Park

Fall, 2015 Prof. Jungkeun Park Data Structures and Algorithms Binary Search Trees Fall, 2015 Prof. Jungkeun Park Copyright Notice: This material is modified version of the lecture slides by Prof. Rada Mihalcea in Univ. of North Texas.

More information

TREES Lecture 12 CS2110 Spring 2019

TREES Lecture 12 CS2110 Spring 2019 TREES Lecture 12 CS2110 Spring 2019 Announcements 2 Submit P1 Conflict quiz on CMS by end of day Wednesday. We won t be sending confirmations; no news is good news. Extra time people will eventually get

More information

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood Red-Black Trees Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood Quick Review of Binary Search Trees n Given a node n... q All elements of n s left subtree are less than n.data q

More information

Lecture 16 Notes AVL Trees

Lecture 16 Notes AVL Trees Lecture 16 Notes AVL Trees 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning 1 Introduction Binary search trees are an excellent data structure to implement associative arrays,

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

AVL Trees. Reading: 9.2

AVL Trees. Reading: 9.2 AVL Trees Reading: 9.2 Balance Factor of a Node The difference in height of its two subtrees (h R -h L ) Balanced Node if -1 BF 1 Unbalanced Node if BF 1 h L h R Balance Factor of a Binar Tree Corresponds

More information

Lecture 13: AVL Trees and Binary Heaps

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

CHAPTER 10 AVL TREES. 3 8 z 4

CHAPTER 10 AVL TREES. 3 8 z 4 CHAPTER 10 AVL TREES v 6 3 8 z 4 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY

More information