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

Size: px
Start display at page:

Download "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"

Transcription

1 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

2 4.1 Preliminaries A Root B C D E F G Height=3 Leaves H I J K L M N P Q Malek Mouhoub, CS340 Fall

3 Terms Child Parent Sibling : nodes that share a common parent. Leaf : a node with no children. Node depth : the number of links on the path from the root to the node. Tree height : the depth of the deepest node. Malek Mouhoub, CS340 Fall

4 Recursive view A tree is either : Empty Contains a root and N s (N 0). Malek Mouhoub, CS340 Fall

5 Implementation of Trees The tree is a collection of nodes : struct TreeNode g; Object element; TreeNode ΛfirstChild; TreeNode ΛnextSibling; f The tree stores a reference to the root node, which is the starting point. Malek Mouhoub, CS340 Fall

6 Implementation of Trees A Root B C D E F G Height=3 H I J K L M N P Q Malek Mouhoub, CS340 Fall

7 Tree Traversals // List a directory in a hierarchical file system void FileSystem::listAll(int depth = 0) const f printname( depth ); if (isdirectory()) for each file c in this directory (for each child) c.listall(depth + 1); g; // Calculate the size of a directory void FileSystem::size( ) const f int totalsize = sizeofthisfile(); if (isdirectory()) for each file c in this directory (for each child) totalsize += c.size(); return totalsize; g; Malek Mouhoub, CS340 Fall

8 4.2 Binary Trees Λ Recursive view A binary tree is either : Empty Contains a root and N binary s (0» N» 2). Malek Mouhoub, CS340 Fall

9 Implementation struct BinaryNode g; Object element; // the data in the node BinaryNode Λleft; // left child BinaryNode Λright; // right child f Malek Mouhoub, CS340 Fall

10 Expression trees Inorder traversal ) infix notation. postorder traversal ) postfix notation. preorder traversal ) prefix notation. + + * a * + g b c * f d e Malek Mouhoub, CS340 Fall

11 Constructing an expression tree Converting a postfix expression into an expression tree : Same principle as evaluating an expression in postfix notation. Use a stack of pointers. For infix expression : Step 1 : Use the algorithm seen in chapter 3 to convert the expression in postfix notation. Step 2 : Use the algorithm above to produce the corresponding expression tree. Malek Mouhoub, CS340 Fall

12 4.3 Binary Search Trees : The Binary Search Tree ADT Goals : Binary search supports Find in O(log N ) worst-case time, but Insert and Remove O(N are ). Would like to support all three operations in O(log N ) worst-case time. Today s result : can support all three operations in O(log N ) average-case time. Later : do it in O(log N ) worst-case time. Basic Ideas : Use a tree to show the logic applied by a binary search. Malek Mouhoub, CS340 Fall

13 Binary Search Expanded Malek Mouhoub, CS340 Fall

14 Binary Search Tree Order Ordering property : for every node in the tree, all items in left are smaller and all items in right are larger (assume no duplicates). X < X > X Malek Mouhoub, CS340 Fall

15 Thinking Recursively Computing the height of a tree is complex without recursion. The height of a tree is one more than the maximum of the heights of the s. Hl+1 X Hr+1 Hl < X > X Hr Malek Mouhoub, CS340 Fall

16 Routine to Compute Height Handle base case (empty tree). Use previous observation for other case. int height(binarynode ΛT) f If (T == null) return -1; else return 1 + Max(height(T->left),height(T->right)); g Malek Mouhoub, CS340 Fall

17 ADT Binary Search Tree Structure BinarySearchTree is Data : A set of N nodes (N 0). Functions : for every bstree 2 Binary Search Tree, item 2 Comparable BynarySearchTree Create(item) ::= return a Binary Search Tree containing item Comparable findmax(bstree) ::= return the maximum of bstree Comparable findmin(bstree) ::= return the minumum of bstree Comparable find(bstree,item) ::= return the matching element if found and ITEM NOT FOUND otherwise. Bool isempty(list) ::= return bstree == empty BinarySearchTree insert(bstree,item) ::= add item to bstree and return the new tree BinarySearchTree remove(bstree,item) ::= removes item and return the new tree Malek Mouhoub, CS340 Fall

18 Implementation template Comparable> <class class BinarySearchTree; template Comparable> <class class BinaryNode f Comparable element; BinaryNode *left; BinaryNode *right; BinaryNode( const Comparable & theelement, BinaryNode *lt, BinaryNode *rt ) : element( theelement ), left( lt ), right( rt ) f g 10 friend class BinarySearchTree<Comparable>; g; Malek Mouhoub, CS340 Fall

19 template Comparable> <class class BinarySearchTree f public: explicit BinarySearchTree( const Comparable & notfound ); BinarySearchTree( const BinarySearchTree & rhs ); BinarySearchTree( ); const Comparable & findmin( ) const; const Comparable & findmax( ) const; 10 const Comparable & find( const Comparable & x ) const; bool isempty( ) const; void printtree( ) const; void makeempty( ); void insert( const Comparable & x ); void remove( const Comparable & x ); const BinarySearchTree & operator=( const BinarySearchTree & rhs ); Malek Mouhoub, CS340 Fall

20 private: BinaryNode<Comparable> *root; const Comparable ITEM NOT FOUND; const Comparable & elementat( BinaryNode<Comparable> *t ) const; void insert( const Comparable & x, BinaryNode<Comparable> * & t ) const; void remove( const Comparable & x, BinaryNode<Comparable> * & t ) const; BinaryNode<Comparable> * findmin( BinaryNode<Comparable> *t ) const; BinaryNode<Comparable> * findmax( BinaryNode<Comparable> *t ) const; BinaryNode<Comparable> * find( const Comparable & x, BinaryNode<Comparable> *t ) const; void makeempty( BinaryNode<Comparable> * & t ) const; 10 void printtree( BinaryNode<Comparable> *t ) const; * clone( BinaryNode<Comparable> *t ) const; BinaryNode<Comparable> g; Malek Mouhoub, CS340 Fall

21 Searching : the find function Set the current node to the root. Repeat : If current node is null, then item is not found. If item is smaller than what is stored in the current node, then branch left. If item is larger than what is stored in the current node, than branch right. Otherwise. we have a match. Malek Mouhoub, CS340 Fall

22 findmin and findmax For findmin, repeatedly branch left until you reach a node with no left child. For findmax, repeatedly branch right until you reach a node with no right child. Malek Mouhoub, CS340 Fall

23 Running Time Time to perform a search is proportional to the depth of the node that terminates the search. For the perfectly balanced tree, this is roughly log N. Malek Mouhoub, CS340 Fall

24 Insertion A new item can be inserted by placing it at the location where an unsuccessful search for it terminates new item 18 Malek Mouhoub, CS340 Fall

25 Coding Simplest code uses recursion.four cases : Case 1 : If the tree is empty, create and return a new node tree. Case 2 : If the item matches the item in the root node do nothing (no duplicates allowed). Case 3 : If the item is less than the item in the root node, recursively insert it in left. Case 4 : If item is greater than the item in the root node, recursively insert in right. Malek Mouhoub, CS340 Fall

26 template <class Comparable> void BinarySearchTree<Comparable>:: insert( const Comparable & x, BinaryNode<Comparable> * & t ) const if( t == NULL ) f t = new BinaryNode<Comparable>( x, NUL L, NULL ); else if( < x t >element ) insert( x, t >left ); else if( < t >element x ) insert( x, t >right ); 10 else ; // Duplicate; do nothing g Malek Mouhoub, CS340 Fall

27 Running Time Running time is still proportional to depth. Arbitrary insertion no longer guarantees that the depth of the tree O(log N is ). However it can be shown that the depth of the tree will be O(log N ) on the average, where average means all possible insertion sequences are equally likely. Malek Mouhoub, CS340 Fall

28 Worst case The most common insertion sequences produce the worst tree. Inserting items in sorted order is disastrous, O(N ) yielding operations. A B C D E Malek Mouhoub, CS340 Fall

29 Bottom Line Insertion and access will be logarithmic on average, but this is meaningful only if insertion sequence is reasonably random. If insertion sequence has some order in it, the binary search tree will degenerate. Need methods of maintaining balance in the tree that preserve a logarithmic worst-case bound. In the following, we will examine two candidates : AVL tree and splay tree. Malek Mouhoub, CS340 Fall

30 Deletion Deletion algorithm is complicated. Basic problem : if the node is deleted, it potentially disconnects the tree. Standard algorithm breaks into three cases : Node to be deleted is a leaf. Node to be deleted has one child. Node to be deleted has two children. Malek Mouhoub, CS340 Fall

31 Deletion of a Leaf Simplest case, because leaf does not connect two parts of the tree. Wipe out the leaf node (its parent should reference a null TreeNode instead of the leaf). Malek Mouhoub, CS340 Fall

32 Deletion of a 1 child node Bypass the node : parent of deleted node gets new child (example below deletes 15) delete item Note : root with one child is a special case, because it does not have a parent. In this case, we simply obtain a new root. Malek Mouhoub, CS340 Fall

33 Deletion of two child node Step 1 : replace node contents with smallest key in right. Example below deletes delete ?? Malek Mouhoub, CS340 Fall

34 Deletion of two child node Step 2 : delete node used as replacement ?? The second step in the two-child case is guaranteed to be simple because that node cannot have a left child (why???). Malek Mouhoub, CS340 Fall

35 Implementation Simple implementation is recursive. Some cases can be combined. The deletion algorithm potentially destroys the balance of the tree because it is a biased algorithm. Notice that in the two child case, it always reduces the right s size, potentially making the left tree heavy after a large number of deletion/insertion pairs. In practice : not noticeable at all. Malek Mouhoub, CS340 Fall

36 template <class Comparable> void BinarySearchTree<Comparable>:: remove( const Comparable & x, BinaryNode<Comparable> * & t ) const f if( t == NULL ) return; // Item not found; do nothing if( x < t >element ) remove( x, t >left ); else if( t >element < x ) remove( x, t >right ); 10 else if( t >left!= NULL && t >right!= NULL ) // Two childre f t >element = findmin( t >right ) >element; g remove( t >element, t >right ); else f BinaryNode<Comparable> *oldnode = t; t = ( t >left!= NULL )? t >left : t >right; delete oldnode;gg 20 Malek Mouhoub, CS340 Fall

37 Printing contents of the tree Easy to print entire tree in sorted order. Case 1 : if tree is empty, print nothing. Case 2 : if tree is not empty : Recursively print left tree. Print item at the root, Recursively print right tree. Strategy is an inorder traversal. Running time is O(N ) : constant work per item. Malek Mouhoub, CS340 Fall

38 4.4 AVL Trees The structure of the tree depends on the order in which keys are entered. Entering the letters A-G in alphabetical order will produce a degenerate tree that is nothing more than a linked list. The solution of this problem is somehow to reorganize the nodes of the tree as we receive new keys, maintaining a near optimal tree structure. AVL trees are used for handling such organization. An AVL tree is a height-balanced 1-tree or HB(1) tree : the maximum difference allowed between the heights of any two s sharing a common root, is one. The two features that make AVL trees important are : by setting a maximum allowable difference in the height of any two s, AVL trees guarantee a minimum level of performance in searching; and Maintaining a tree in AVL form as new nodes are inserted involves the use of one of a set of four possible rotations. Malek Mouhoub, CS340 Fall

39 AVL trees AVL technique used to keep a tree in height-balance : Each time an insertion is made, one must do the following : 1. Let the node to be inserted travel down the appropriate branch, keeping track along the way of the deepest level node on that branch that has a balance factor of +1 or -1 (this particular node is called pivot). 2. Inclusive and below the pivot node, recompute all balance factors along the insertion path traced in step Determine whether the absolute value of the pivot node s balance factor switched from 1 to If there was such a switch, perform a manipulation of tree pointers centered at the pivot node to bring the tree back into height-balance. This operation is frequently referred to as an AVL-rotation. Malek Mouhoub, CS340 Fall

40 AVL rotations 4 cases to consider : Case 1 : The insertion that unbalanced the tree occurred in the left of the left child of the pivot node. Case 2 : The insertion that unbalanced the tree occurred in the right of the right child of the pivot node. Case 3 : The insertion causing the imbalance occurred in the right of the left child of the pivot node. Subcase 1 : Neither the pivot node nor its left child has a right child. Subcase 2 : Insertion is in the left of the right child of the left child of the pivot. Subcase 3 : Insertion occurs in the right of the right child of the left child of the pivot. Case 4 : The insertion that causes the imbalance in the tree is made in the left of the right child of the pivot node. Case 4 is to case 3 as case 2 is to case 1. Malek Mouhoub, CS340 Fall

41 Case 1 Pivot S BF=+1 rotation Pivot S BF=+2 Height = h+2 BF=0 Left of M M Right of M Right of S Height = h inserting a new key BF=+1 Left of M M Right of M Right of S insert balancing Pivot M BF=0 Height = h+2 Left of M insert Old right of M S BF=0 Right of S Malek Mouhoub, CS340 Fall

42 Case 2 Pivot S BF=-1 rotation Pivot S BF=-2 Height = h+2 Height = h Left of S BF=0 Left of V V Right of V inserting a new key Left of S BF=-1 Left of V V Right of V insert balancing Pivot V BF=0 Height = h+2 BF=0 Left of S S Old left of V Right of V insert Malek Mouhoub, CS340 Fall

43 Case 3 : subcase 1 Pivot S BF=1 Pivot S BF=2 Pivot Q BF=0 M BF=0 inserting a new key M BF=-1 balancing M BF=0 S BF=0 Q BF=0 Malek Mouhoub, CS340 Fall

44 Case 3 : subcase 2 Pivot S BF=+1 Pivot S BF=+2 Height = h+2 BF=0 M BF=0 Q Right of S Height = h inserting a new key BF=-1 M BF=1 Q Right of S Left of M Height = h Left of Q Right of Q Height = h-1 Left of M Left of Q insert Right of Q Balancing Pivot Q BF=0 Height = h+2 BF=0 Left of M M Old left of Q insert Old right of Q S BF=-1 Right of S Malek Mouhoub, CS340 Fall

45 Case 3 : subcase 3 Pivot S BF=+1 Pivot S BF=+2 Height = h+2 BF=0 M BF=0 Q Right of S Height = h inserting a new key BF=-1 M BF=-1 Q Right of S Left of M Height = h Left of Q Right of Q Height = h-1 Left of M Left of Q Right of Q insert Balancing Pivot Q BF=0 Height = h+2 BF=-1 Left of M M Old left of Q Old right of Q insert S BF=0 Right of S Malek Mouhoub, CS340 Fall

46 4.5 Splay Trees Think in terms of a set of operations instead of a single one. Goal : guarantees that any M consecutive tree operations starting from an empty tree take at O(M log N ) most time. Idea : After a node is accessed, it is pushed to the root by a series of AVL tree rotations. Malek Mouhoub, CS340 Fall

47 the wrong way Perform single rotations, bottom up. In the case of inserting the keys 1; 2; : : : ; N into an empty tree : O(N ) is required to build the tree. Ω(N 2 ) for accessing all the keys in order. After the keys are accessed, the tree reverts to its original state. Malek Mouhoub, CS340 Fall

48 Splaying If the parent of the node to be pushed is the root, perform a single rotation. Otherwise perform a double rotation : 2 cases to consider (plus symmetries). Malek Mouhoub, CS340 Fall

49 Case 1 G X P X D P G A B C D B C Malek Mouhoub, CS340 Fall

50 Case 2 G X X P C D A B P G B C D Malek Mouhoub, CS340 Fall

51 Advantage of the method When inserting items 1; 2; 3; : : : ; N into an initially empty tree : O(N ) is required to insert all items (same as 1st method). After accessing and pushing node 1 to to root, N=2 units only are required to access node 2 (instead of N 2 for the 1s method). After accessing and pushing the other nodes, the depth becomes log N. Malek Mouhoub, CS340 Fall

52 4.6 B-Trees Problem : Disk utilization of a binary search tree is extremely inefficient. A minimum of a single page is read (at least 512 bytes) in order to get the following information : key value, address of left and right s. First Solution : Divide a binary tree into pages and then store each page in a block of contiguous locations on disk. The number of seeks associated to any search will be then reduced. Problem when inserting new items. ) use B-trees. Malek Mouhoub, CS340 Fall

53 Paged Binary Trees Malek Mouhoub, CS340 Fall

54 B-Trees 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 non leaf nodes store up to M-1 keys to guide the searching; key i represents the smallest key in i The root is either a leaf or has between two and M children. 4. All non leaf 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. Malek Mouhoub, CS340 Fall

55 B-Tree of order Malek Mouhoub, CS340 Fall

56 Calculating M, L and the level of the B-Tree Example : driving records for citizens in the state of Florida 10,000,000 records. Record (data) : 256 bytes. Key (representing a name) : 32 bytes. Pointer (branch) : 4 bytes (address of disk block). Node = disk block = 8,192 bytes. ) L= 8; = 32 ) (M-1) keys + M pointers = 8,192 ) 32 (M-1) + 4 M = 8,192 ) 36M - 32 = 8,192 ) M ß 228 ) 625,000 leaves. Why? ) Leaves would be on level 4 in the worst case. Why? ) Worst case number of access : O(log M=2 N ). Why? Malek Mouhoub, CS340 Fall

57 Inserting item inserting item 57 Malek Mouhoub, CS340 Fall

58 Inserting 55 : Split into 2 leaves Split into 2 leaves after inserting 55 Malek Mouhoub, CS340 Fall

59 Inserting 40 : split into 2 leaves + split of the parent nserting item 40 causes a split into 2 leaves and then a split of the parent node Malek Mouhoub, CS340 Fall

60 Deleting Malek Mouhoub, CS340 Fall

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

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

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

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

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

CMSC 341 Lecture 10 Binary Search Trees

CMSC 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

Trees, Binary Trees, and Binary Search Trees

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

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CISC 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 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

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

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

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

CMSC 341. Binary Search Trees CMSC 341 BST

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

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

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

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! 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 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

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

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

Trees, Part 1: Unbalanced Trees

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

void insert( Type const & ) void push_front( Type const & )

void insert( Type const & ) void push_front( Type const & ) 6.1 Binary Search Trees A binary search tree is a data structure that can be used for storing sorted data. We will begin by discussing an Abstract Sorted List or Sorted List ADT and then proceed to describe

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. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Trees. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University Trees CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1 Overview Tree data structure Binary search trees Support O(log 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

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

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

CMSC 341 Lecture 15 Leftist Heaps

CMSC 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 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

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

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

Trees. Trees. CSE 2011 Winter 2007

Trees. Trees. CSE 2011 Winter 2007 Trees CSE 2011 Winter 2007 2/5/2007 10:00 PM 1 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 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

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

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

Overview. Tree data structure Binary search trees. STL set and map classes B-trees for accessing secondary storage Applications

Overview. Tree data structure Binary search trees. STL set and map classes B-trees for accessing secondary storage Applications Trees 1 Overview Tree data structure Binary search trees Support O(log 2 N) operations Balanced trees STL set and map classes B-trees for accessing secondary storage Applications 2 Trees G is parent of

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

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

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

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Discussion 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 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

CS102 Binary Search Trees

CS102 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 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

CMSC 341 Lecture 15 Leftist Heaps

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

CMSC 341 Leftist Heaps

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

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Dr. Malek Mouhoub Department of Computer Science University of Regina Fall 2002 Malek Mouhoub, CS3620 Fall 2002 1 6. Priority Queues 6. Priority Queues ffl ADT Stack : LIFO.

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

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

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748 Data Structures Giri Narasimhan Office: ECS 254A Phone: x-3748 giri@cs.fiu.edu Search Tree Structures Binary Tree Operations u Tree Traversals u Search O(n) calls to visit() Why? Every recursive has one

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

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority. 3. Priority Queues 3. Priority Queues ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority. Malek Mouhoub, CS340 Winter 2007 1 3. Priority Queues

More information

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad SCJ2013 Data Structure & Algorithms Binary Search Tree Nor Bahiah Hj Ahmad Binary Search Tree A binary search tree has the following properties: For every node n in the tree Value of n is greater than

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

UNIT III BALANCED SEARCH TREES AND INDEXING

UNIT III BALANCED SEARCH TREES AND INDEXING UNIT III BALANCED SEARCH TREES AND INDEXING OBJECTIVE The implementation of hash tables is frequently called hashing. Hashing is a technique used for performing insertions, deletions and finds in constant

More information

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F)) DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES UNIT IV NONLINEAR DATA STRUCTURES Part A 1. Define Tree [N/D 08]

More information

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

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues Copyright 1998 by Addison-Wesley Publishing Company 147 Chapter 15 Stacks and Queues Copyright 1998 by Addison-Wesley Publishing Company 148 tos (-1) B tos (1) A tos (0) A A tos (0) How the stack routines

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Trees Kostas Alexis Trees List, stacks, and queues are linear in their organization of data. Items are one after another In this section, we organize data in a

More information

Overview. Tree data structure Binary search trees. STL set and map classes B-trees for accessing secondary storage Applications

Overview. Tree data structure Binary search trees. STL set and map classes B-trees for accessing secondary storage Applications Trees 1 Overview Tree data structure Binary search trees Support O(log 2 N) operations Balanced trees STL set and map classes B-trees for accessing secondary storage Applications 2 Trees G is parent of

More information

CS 350 : Data Structures Binary Search Trees

CS 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 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

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT... Steven J. Zeil July 11, 2013 Contents 1 Definition: Binary Search Trees 2 1.1 The Binary Search Tree ADT.................................................... 3 2 Implementing Binary Search Trees 7 2.1 Searching

More information

Data Structures in Java

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

Priority Queues. 04/10/03 Lecture 22 1

Priority Queues. 04/10/03 Lecture 22 1 Priority Queues It is a variant of queues Each item has an associated priority value. When inserting an item in the queue, the priority value is also provided for it. The data structure provides a method

More information

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

ROOT: A node which doesn't have a parent. In the above tree. The Root is A. TREE: A tree is a finite set of one or more nodes such that there is a specially designated node called the Root, and zero or more non empty sub trees T 1, T 2...T k, each of whose roots are connected

More information

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

B-Trees. Version of October 2, B-Trees Version of October 2, / 22 B-Trees Version of October 2, 2014 B-Trees Version of October 2, 2014 1 / 22 Motivation An AVL tree can be an excellent data structure for implementing dictionary search, insertion and deletion Each operation

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

Binary Search Trees 1

Binary Search Trees 1 Binary Search Trees 1 The Problem with Linked Lists 8Accessing a item from a linked list takes O(N) time for an arbitrary element 8Binary trees can improve upon this and reduce access to O( log N ) time

More information

CS350: Data Structures Binary Search Trees

CS350: 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 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

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

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

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

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II 05 A: Trees II CS1102S: Data Structures and Algorithms Martin Henz February 10, 2010 Generated on Wednesday 10 th February, 2010, 10:54 CS1102S: Data Structures and Algorithms 05 A: Trees II 1 1 Review:

More information

3137 Data Structures and Algorithms in C++

3137 Data Structures and Algorithms in C++ 3137 Data Structures and Algorithms in C++ Lecture 3 July 12 2006 Shlomo Hershkop 1 Announcements Homework 2 out tonight Please make sure you complete hw1 asap if you have issues, please contact me will

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

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

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

Chapter 4: Trees. 4.2 For node B :

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

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

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

Binary Search Trees Treesort

Binary Search Trees Treesort Treesort CS 311 Data Structures and Algorithms Lecture Slides Friday, November 13, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009

More information

Topic Binary Trees (Non-Linear Data Structures)

Topic Binary Trees (Non-Linear Data Structures) Topic Binary Trees (Non-Linear Data Structures) CIS210 1 Linear Data Structures Arrays Linked lists Skip lists Self-organizing lists CIS210 2 Non-Linear Data Structures Hierarchical representation? Trees

More information

Priority Queues (Heaps)

Priority Queues (Heaps) Priority Queues (Heaps) 1 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted order. Often we collect a set of items and process the

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

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015 CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Kevin Quinn Fall 2015 Where we are Studying the absolutely essential ADTs of computer science and classic data structures

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

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

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 18.4 A Unix directory 02/10/04 Lecture 9 1 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002

More information

The questions will be short answer, similar to the problems you have done on the homework

The questions will be short answer, similar to the problems you have done on the homework Introduction The following highlights are provided to give you an indication of the topics that you should be knowledgeable about for the midterm. This sheet is not a substitute for the homework and the

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

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

Dynamic Access Binary Search Trees

Dynamic Access Binary Search Trees Dynamic Access Binary Search Trees 1 * are self-adjusting binary search trees in which the shape of the tree is changed based upon the accesses performed upon the elements. When an element of a splay tree

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

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge. Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from

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

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date: Upcoming ACM Events Linux Crash Course Date: Oct. 2nd and 3rd Time: 1:15 pm - 3:15 pm Location: UW1-210 (10/02) and UW1-221 (10/03) Weekly Crack the Coding Interview Date: Weekly Fridays from Oct. 5th

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

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

Chapter 22 Splay Trees

Chapter 22 Splay Trees Chapter 22 Splay Trees Introduction Splay trees support all the operations of binary trees. But they do not guarantee Ο(log N) worst-case performance. Instead, its bounds are amortized, meaning that although

More information