Trees. Prof. Dr. Debora Weber-Wulff

Similar documents
Algorithms. AVL Tree

Binary Trees, Binary Search Trees

CS102 Binary Search Trees

TREES. Trees - Introduction

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

Binary Search Trees 1

BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B TREES WEEK - 6

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

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Self-Balancing Search Trees. Chapter 11

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

Lecture 23: Binary Search Trees

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

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

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

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

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

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

Balanced Search Trees. CS 3110 Fall 2010

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

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

Binary Search Tree (3A) Young Won Lim 6/2/18

Search Structures. Kyungran Kang

Ch04 Balanced Search Trees

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

CS Transform-and-Conquer

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

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

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

Data Structures Lesson 7

Binary search trees (chapters )

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

More BSTs & AVL Trees bstdelete

CMPE 160: Introduction to Object Oriented Programming

8. Binary Search Tree

Part 2: Balanced Trees

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

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Lesson 21: AVL Trees. Rotation

CSCI2100B Data Structures Trees

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

Binary Search Tree (2A) Young Won Lim 5/17/18

Data Structures in Java

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

Binary search trees (chapters )

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

Binary Search Trees. Analysis of Algorithms

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

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

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

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

CS350: Data Structures B-Trees

CS 350 : Data Structures B-Trees

Binary Search Tree (3A) Young Won Lim 6/4/18

CS 261 Data Structures. AVL Trees

If you took your exam home last time, I will still regrade it if you want.

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Programming II (CS300)

CmpSci 187: Programming with Data Structures Spring 2015

Algorithms. Deleting from Red-Black Trees B-Trees

Module 4: Priority Queues

Advanced Set Representation Methods

CS24 Week 8 Lecture 1

CS350: Data Structures Red-Black Trees

Chapter 2: Basic Data Structures

Module 4: Dictionaries and Balanced Search Trees

Data Structures and Algorithms for Engineers

Friday Four Square! 4:15PM, Outside Gates

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

CS301 - Data Structures Glossary By

Trees Slow Insertion in an Ordered Array If you re going to be doing a lot of insertions and deletions, an ordered array is a bad choice.

Section 4 SOLUTION: AVL Trees & B-Trees

Lecture 6: Analysis of Algorithms (CS )

CSE 100 Advanced Data Structures

CS350: Data Structures AVL Trees

AVL Tree. Idea. The performance (Search, Insertion, Deletion):

CSCI Trees. Mark Redekopp David Kempe

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Binary Trees and Binary Search Trees

Analysis of Algorithms

Balanced Search Trees

CMSC 341 Lecture 14: Priority Queues, Heaps

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

Trees. Eric McCreath

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

Search Trees - 1 Venkatanatha Sarma Y

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

Trees. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

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

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

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

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Trees. A tree is a directed graph with the property

CS 315 Data Structures mid-term 2

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

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Balanced Binary Search Trees

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.

Transcription:

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 Algorithms in Java 3

Why use trees? You can search quickly in ordered arrays but inserting is a pain You can easily insert into a linked list, but searching is a pain Is there a structure that affords quick searching and easy insertion? Yes, that s what a tree will offer! 4

Trees Computer science trees grow the "wrong way round"! 5

Trees Root 1. One node is the root. The circles are nodes, the arrows edges. 6

Trees Parent Child 1. One node is the root. 2. Every node c except the root has exactly one parent p. c is called p s child. 7

Trees Path 1. One node is the root. 2. Every node c except the root has exactly one parent p. c is called p s child. 3. There is a unique path from the root to each node. 8

Trees Leaf 1. One node is the root. 2. Every node c except the root has exactly one parent p. c is called p s child. 3. There is a unique path from the root to each node. 4. Childless nodes are called leaves. 9

Trees 1. One node is the root 2. Every node c except the root has exactly one parent p. c is called p s child. 3. There is a unique path from the root to each node. 4. Childless nodes are called leaves. 10

Tree-based structure File System My Computer A: C: E: dww Programs Tmp Windows CS101 CS102 11

Tree-based structure my Computer A: C: dww CS101 CS102 Programs Line-at-a-time Tree 12

Tree-based structure AnimationFigure Head Body Torso Chest Clothing Legs IDGD, HTW Berlin, 2012 13

Tree-based structure Book Chapter 1 Chapter 2 Chapter 2.2 Chapter 2.2.1 Chapter 2.2.2 Chapter 2.3 CC-BY-SA, Debora Weber-Wulff 14

Binary Search Tree Any node has a maximum of two children. D B G A C E F Useful for searching Comparable objects. The objects are stored in an ordered fashion. 15

Binary Search Tree D B G A C E F Simple implementation à average case O(log N) worst case O(N) degenerate Careful implementation à worst case O(log N) 16

The Node Class for Trees public class Node { Comparable data; Node leftchild; Node rightchild; public String displaynode() { // } data leftchild rightchild 17

The Tree Class public class Tree { private Node root; public Node find(comparable obj) { } public void insert(comparable obj) { } public void delete(comparable obj) { } public boolean isempty() { return root == null; } } 18

Finding a Node public Node find(comparable obj) { Node curr = root; while(curr!= null) { switch (curr.data.compareto(obj)) { case 1 : curr = curr.leftchild; break; case 0 : return curr; case -1 : curr = curr.rightchild; break; } } // didn't find it return null; } 19

Finding a Node recursively (Step 1) public Node find(comparable obj) { return find (obj, root); } 20

Finding a Node recursively (Step 2) public Node find(comparable obj, Node n) { if (n == null) return null; switch (n.data.compareto(obj)) { 1 : return find (obj, n.leftchild); 0 : return n; -1 : return find (obj, n.rightchild);} } } 21

Complexity of finding a node? At each step of the loop or the recursion we are only looking at half of the remaining nodes. This makes the complexity O(log N) 22

Inserting a new Node 1. Create the Node, call it newnode 2. Is the root == null? then root = newnode 3. set current = root; 4. while(true) do... root data leftchild rightchild 23

Inserting a new Node 4. while(true) do 4.1. Remember parent as curr 4.2. Go left if curr > obj if current now null, parent.leftchild = newnode return 4.3. else go right if current now null parent.rightchild = newnode return Worst-case? O(N) 24

Result of Insertion Order: D, B, A, F, G, C, E, H D B F A C E G H 25

The order is important! Order: E, B, A, D, G, C, F, H E B G A D F H C 26

An Animation 28

A Degenerate Tree Order: A, B, C, D, E, F, G, H A B We ll see how to avoid this problem next lecture. C D E F G H 29

Tree Traversal If you want to iterate something for every element in a tree, you want to be doing tree traversal or tree-walking. This means that you visit all of the nodes according to a pre-specified rule. There are four different kinds: in-order, pre-order, post-order, level-order. 30

Tree Traversal b a c In-order a b c Pre-order b a c Post-order a c b Level-order b d e a c d b e a c f f 31

Traversing the Tree private void inorder(node local) { if (local!= null) { inorder(local.leftchild); local.displaynode(); inorder(local.rightchild); } } Application: mytree.inorder(root) 32

Traversing the Tree In-order: Pre-order: Post-order: 1. left subtree 2. visit 3. right subtree 1. visit 2. left subtree 3. right subtree 1. left subtree 2. right subtree 3. visit 33

Keyed Trees We often find trees that are not just comparable, but which have a key value in an extra field in order to make some operations faster. key 42 data leftchild rightchild 34

Searching Trees Data in node 3 27 39 1 15 14 Data in leaf, only key in node 1 1 15 3 14 15 27 27 39 3 14 35

Got to here 2018-06-05 36

Deleting a Node Most complicated binary tree operation. But studying the details builds character! 1. Find the node 2. Which case? 2.1 Node is a leaf à 2.2 Node has one child à 2.3 Node has two children à 2.1 Easy! Set child field in parent to null, garbage collection will do the work. 37

2.2 Not too difficult "Snip" the node out by having the parent s child pointer point directly to the child 80 80 52 52 48 63 71 67 delete 48 63 67 Watch out: 4 variations! Node can be left or right child of the parent, and the child can be left or right child. 38

2.3 Fun Replace the node with its in-order successor. 15 5 20 25 50 35 30 47 The in-order successor is the smallest of the set of the nodes that are larger than the original node. Go right, then left, left, left Until no leftchild. 2.3.1 Successor is rightchild of node to be deleted 2.3.2 Successor is left descendent of rightchild 2.3.3 If the successor has a rightchild Fix it! Insert and rearrange! 39 15 5 20 30 50 35 47

Aaaaaargh! Some programmers are afraid of implementing this. They just add a field to a node: isdeleted and skip over them when searching in a tree. 40

How can we avoid degenerate trees? This is a complex algorithm! We have to have balanced trees, that is, we must not insert a node if it inbalances the tree. When the insertion point is found and the node has been inserted, the tree is tested for imbalance, i.e. the maximum path length is computed. If this is longer than k/2, where k is the number of nodes in the tree, then a rebalancing algorithm "shakes up" the tree and selects a new root node. 42

AVL Tree Self-balancing binary search tree Published by the Russian inventors, G.M. Adel son- Vel skiў and E.M. Landis in 1962 The heights of the two child subtrees of any node differ by at most one Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases. AVL-Tutorial: Data Structures and Algorithms with Object- Oriented Design Patterns in Java http://www.brpreiss.com/books/opus5/html 43

Balanced Trees We could say that a binary tree is balanced if the left and right subtrees of every node have the same height. But then the only trees which are balanced are the perfect binary trees. A perfect binary tree of height h has exactly 2 h+1-1 internal nodes. Therefore, it is only possible to create perfect trees with n nodes for n = 1, 3, 7, 15, 31,... nodes. This is rather not what we want. 44

A Better Balance Condition What are the characteristics of a good balance condition? 1. A good balance condition ensures that the height of a tree with n nodes is O (log n) 2. A good balance condition can be maintained efficiently. That is, the additional work necessary to balance the tree when an item is inserted or deleted is O(1) 45

AVL Balance Condition If we write a tree as (root, T left, T right ), then we can say that it is balanced if height left - height right <= 1 It can be shown that all trees that satisfy the AVL balance condition are logarithmic in the number of internal nodes. 46

Inserting in AVL Trees Inserting an item into an AVL tree is a two-part process. First, the item is inserted into the tree using the usual insertion method in binary search trees. After the item has been inserted, it is necessary to check that the resulting tree is still AVL balanced and to balance the tree when it is not. The heights of all the nodes along the access path must be recomputed and the AVL balance condition must be checked if it is violated, the structure of the tree must be changed. 47

Problem: Insert 5 unbalances tree 7 7 4 4 5

Rotation When an AVL tree becomes unbalanced, it is possible to bring it back into balance by performing an operation called a rotation. It turns out that there are only four cases to consider and each case has its own rotation. A node is rotated when it trades places with its in-order predecessor. The rotation function is its own inverse function, if you rotate again, you get the same tree again. 49

rotate left B D rotate F B F F 1 D 2 3 3 1 2

rotate right B F rotate D B D D 3 F 1 2 1 2 3

All four: Single rotations Rotate left child left (LL) Rotate right child right (RR) Double rotations Rotate left child right (LR) Rotate right child left (RL)

Properties of Rotation Rotation leaves a sorted tree sorted No nodes are added or deleted Only the structure is changed and an AVL tree that was unbalanced by insertion is again an AVL tree after the proper rotation. The tree still has the same height! 53

When to rotate It is not necessary to know the exact height! It is sufficient to know the balance factor: -1, 0, 1 54

Let s play! http://www.qmatica.com/datastructures/trees/ AVL/AVLTree.html 55

Your Turn! Balanced or not? 56

Balanced or not? 57

Construct an AVL Tree 10, 40, 35, 25, 60, 30, 80, 50, 27, 28, 38 58

The Result 59

Binary Tree Application Expression Tree 5 + - * 2 (5+((3-1)*2)) 3 1 Easy to evaluate! eval(node) = Node.contents(eval(Node.Left), eval(node.right)) 60

Binary Tree Application Huffman coding Tree Each symbol stored at a leaf, Path marked with 1 or 0 is code 1 1 0 0 R 1 S 1 1 0 0 1 A 0 0 B Z Q Most frequent symbols have shortest code. 62

Other Tree Applications - Decision Trees - Syntax Trees - Derivation Trees - Code Trees - Spanning Trees - Tree-structured search spaces - Searching Trees 63

Trie Trie (pronounced try ) Structure suggested by E. Fredkin in CACM 3 (1960) 490-500 A trie is an M-ary tree, whose nodes are M-place vectors with components corresponding to digits or characters. Each node on level l represents the set of all keys that begin with a certain sequence of l characters Algorithms to be found in Donald E. Knuth, The Art of Computer Programming, Volume 3, Sorting and Searching 73

Trie All the descendants of any one node have a common prefix of the string associated with that node, and the root is associated with the empty string. Values are normally not associated with every node, only with leaves and some inner nodes that happen to correspond to keys of interest. 74

A trie for keys 'to', 'tea', 'ten', 'inn'. 75

Complexity A trie needs 12 iterations to distiguish, for example COMPUTATION and COMPUTATIONS Linked list representation much better spacewise (many empty cells otherwise) 76

Complexity Search time for large N (M is the size of the alphabet): log M N = lg N / lg M iterations Needs N / log M nodes to distinguish between N random inputs Space proportional to M*N / ln M (lg is base 2, ln base e) 77

Long words? Only pays off for many short words. Otherwise - after the list of words gets down below 6, just use a list of words instead of continuing the trie structure. Decreases space needed by a factor of 6 by same running time. 78

Patricia Trie Practical Algorithm To Retrieve Information Coded in Alphanumeric N-node search trees based on the binary representation of the keys Good for extremely long, variable-length keys such as titles or phrases stored within a large bulk file 79

Storage? Tables Linearized lists: (T(H (E * (I (R * (S *)) (S (M *)) (N * (C (E *))) (M * (E *))))) The, Their, Theirs, Theism, Then, Thence, Them, Theme 80

Advantages of a Trie vs. BST Looking up keys is fast. Looking up a key of length m takes only O(m) time; in the worst case in a BST, O(m²) time is required, because initial characters are examined repeatedly during multiple comparisons. Tries require less space. Because the keys are not stored explicitly, only an amortized constant amount of space is needed to store each key. Tries make longest-prefix matching, where we wish to find the key sharing the longest possible prefix with a given key, efficient. They also allow one to associate a value with an entire group of keys that have a common prefix. 81