Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Authorship ● ● A large portion of these slides come from material by others Original material from – – ● ● Goodrich, Tamassia, Goldwasser 2010: Data Structures and Algorithms in Java Ford, Topp 2005: Data Structures with Java Last Lecture – Covered trees and binary search trees (BST) This Lecture – Applications of trees to other ADTs – Achieving balance: ● Self-balancing structures Important properties of BSTs ● ● < Lecture 33 > [Balanced] Binary Search Trees do – Store elements in order, keeping the order – Find elements in log-time – Update single elements in log-time What can we do with BSTs? – SORT – Implement SETs – Implement MAPs (aka Dictionaries) Applications of BSTs, especially to ADTs Tree Sort ● ● ● ● ● TreeSort uses a BST to sort the elements Adding elements to a balanced tree is O(logn) per element (on a tree with n elements) Assume we use a self balancing tree Performance: n insertions, O(logn) per insertion → O(nlogn) cost Without self balancing tree: O(nlogn) in average, but O(n²) in worst case (like QuickSort) Tree Sort: In-Order ● ● ● Other tree traversals IN-ORDER. Recursive List building. – inord(Node t)= ● [], if t is null ● inord(t.left) + [t.value] + inord(t.right) Main idea: first process left subtree, then node at hand, then right subtree – By process we mean to visit or to do something else to the node ● ● ● TreeSort: build List this way, return it Breadth-first traversal ● ● PRE-ORDER. First node, then left subtree, then right subtree. – preord(Node t)= ● [], if t is null ● [t.value] + preord(t.left) + preord(t.right) POST-ORDER. First left subtree, then right subtree, then parent node. – postord(Node t)= ● [], if t is null ● postord(t.left) + postord(t.right) + [t.value] Exercise: tree traversals Mainly used in combinatorial optimization and artificial intelligence InOrder? C Go through all the elements of the level (same depth or height), from left to right, then navigate the next level, and repeat B PreOrder? 2 PostOrder? A Better use a Queue for this one 0 3 BreadthFirst? D Exercise: tree traversals InOrder? ABCD0123 C B A 0 D 3 1 Example: 1. implement a basic binary tree // this class implements a simple binary tree // as a linked structure, rather than the ADT public class BT { BT left, right; int value; PreOrder? CBA20D13 2 1 public BT(int val) { value = val; } PostOrder? ABD1032C public BT(int val,BT left,BT right) { value = val; this.left = left; this.right = right; } BreadthFirst? CB2A03D1 } 2. Get elements in tree's order 2. Get elements in tree's order // we will return the elements using a LIST import java.util.*; // in-order traversal of the tree public List<Integer> inord() { List<Integer> L = new ArrayList(); if (left!=null) L.addAll( left.inord() ); L.add( value ); if (right!=null) L.addAll( right.inord() ); return L; } public class BT { BT left, right; int value; public BT(int val) { value = val; } public BT(int val,BT left,BT right) { value = val; this.left = left; this.right = right; } // it continues next slide --> Set ADT ● ● Collection of elements with no concern of order and without repetition Sets and sets: – union – intersection – difference – subset // let us try our code public static void main(String[] sad) { BT b = new BT( 0, new BT(-1), new BT(3,null,new BT(4)) ); System.out.println(b.inord()); } } Set ADT – performance ● ● ● Sets and elements: – add – remove – contains If only set-set operations, we can implement using ordered array or linked list ● ● Set-Set operations (union, etc) – O( n + m ): reduce sets to ordered arrays, do merge, construct answer tree from merged array Set-element operations (add, etc) – O( log n ): these are BST operations Else, we can use a BST Map ADT (dictionary) ● ● Map ADT links a key to a value – Arrays are like this, if keys are 0..n-1 – General Map ADT uses any kind of key Important methods: – get(key) → value – put(key,value) – remove(key) → value – containsKey(key) – containsValue(key) ● ● Most operations are about searching for a key! – If the keys have a natural ordering, or are comparable, or we supply a comparator, – then can implement Map using a BST Map using BST: each node stores key, value; but search by key Self balancing trees Problem of balancing ● ● Balancing yields good performance – Many variants of BST introduced! Naïve solution: randomize input—can't always do ● AVL Trees: rotations ● Splay Trees: simpler rotations ● Red-Black Trees: dedicated nodes ● 2-4 Trees: not binary—nodes can overload – (a,b) trees: instead of 2-4, they are a-b – B-Trees: name for (a,2a) trees – Isomorphic to Red-Black trees, but simpler 2-4 trees: full nodes are splitted 2-4 Trees ● ● Nodes can have 1, 2, 3 values – Thus can have 2, 3, 4 children This is general culture for CS; I will not ask about how 2-4 trees work. You will likely study them later. Growth? Never add a leaf – In leaf: ● Try to add new value ● If overload (4 values), split node, and tell parent – In parent: ● Try to allocate new sibling ● Can't? Repeat overload process – Root overload? Create new sibling and root AVL Trees: rotations Splitting a node sends a value to a parent. If root, create new root! This is how a 2-4 Node looks at full capacity. The order is as BSTs, but with more values. S 1<a<S2<b<S3... AVL Trees ● ● ● ● 1. Recall order in BST I will NOT follow standard explanations of this The idea of AVL Trees can be simplified A LOT from what is in the book and online Key insight: – We can re-arrange the BST cheaply while preserving the order (in-order) of the elements (We will only focus on double rotations – They generalize single rotations...) (and S4) 1. Recall order in BST 2. Abstract order from tree 3. Different trees, same order 3. Different trees, same order 3. Different trees, same order 3. Different trees, same order 4. Same order, different imbalance When to balance the tree? ● ● ● ● ● After update! AVL tree will be balanced, except for path involved in insertion/deletion Rebalance tree [if necessary] in subtrees rooted in nodes from path O(log n) nodes involved in path! (AVL tree will be nearly balanced) Each rebalancing (rotation) takes O(1) time because of finite possibilities! 5. Algorithm: pick best balance for same order