CompSci 100e 7.1 From doubly-linked lists to binary trees l Instead of using prev and next to point to a linear arrangement, use them to divide the universe.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Search.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
AVL Trees ITCS6114 Algorithms and Data Structures.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
CPS Wordcounting, sets, and the web l How does Google store all web pages that include a reference to “peanut butter with mustard”  What efficiency.
CompSci Binary Trees l Linked lists: efficient insertion/deletion, inefficient search  ArrayList: search can be efficient, insertion/deletion.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CPS 100, Spring Trees: no data structure lovelier?
CPS Balanced Search Trees l BST: efficient lookup, insertion, deletion  Average case: O(log n) for all operations since find is O(log n) [complexity.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
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,
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
CPS Balanced Search Trees l BST: efficient lookup, insertion, deletion  Average case: O(log n) for all operations since find is O(log n) [complexity.
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
CPS Scoreboard l What else might we want to do with a data structure? l What are maps’ limitations? AlgorithmInsertionDeletionSearch Unsorted.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Search Trees (BST)
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
CompSci 100e 7.1 Plan for the week l Review:  Boggle  Coding standards and inheritance l Understand linked lists from the bottom up and top-down  As.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
CPS 100, Spring Interlude for trees l Joyce Kilmer Joyce Kilmer l Balanced Trees  Splay  Red-Black  AVL  B-tree.
From doubly-linked lists to binary trees
COMP261 Lecture 23 B Trees.
Non Linear Data Structure
CSE 373 Binary search trees; tree height and balance
PFTFBH Binary Search trees Fundamental Data Structure
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
PFTWBH More examples of recursion and alternatives
UNIT III TREES.
What’s the Difference Here?
ITEC 2620M Introduction to Data Structures
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Binary Trees, Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

CompSci 100e 7.1 From doubly-linked lists to binary trees l Instead of using prev and next to point to a linear arrangement, use them to divide the universe in half  Similar to binary search, everything less goes left, everything greater goes right  How do we search?  How do we insert? “llama” “tiger” “monkey” “jaguar” “elephant” “ giraffe ” “pig” “hippo” “leopard” “koala”

CompSci 100e 7.2 Basic tree definitions l Binary tree is a structure:  empty  root node with left and right subtrees l terminology: parent, children, leaf node, internal node, depth, height, path link from node N to M then N is parent of M –M is child of N leaf node has no children –internal node has 1 or 2 children path is sequence of nodes, N 1, N 2, … N k –N i is parent of N i+1 –sometimes edge instead of node depth (level) of node: length of root-to-node path –level of root is 1 (measured in nodes) height of node: length of longest node-to-leaf path –height of tree is height of root A B ED F C G

CompSci 100e 7.3 Implementing binary trees l Trees can have many shapes: short/bushy, long/stringy  if height is h, number of nodes is between h and 2 h -1  single node tree: height = 1, if height = 3  Java implementation, similar to doubly-linked list public class TreeNode { String info; TreeNode left; TreeNode right; TreeNode(String s, TreeNode llink, TreeNode rlink){ info = s; left = llink; right = rlink; } };

CompSci 100e 7.4 Printing a search tree in order l When is root printed?  After left subtree, before right subtree. void visit(TreeNode t) { if (t != null) { visit(t.left); System.out.println(t.info); visit(t.right); } l Inorder traversal “llama” “tiger” “monkey” “jaguar” “elephant” “ giraffe ” “pig” “hippo” “leopard”

CompSci 100e 7.5 Tree traversals l Different traversals useful in different contexts  Inorder prints search tree in order Visit left-subtree, process root, visit right-subtree  Preorder useful for reading/writing trees Process root, visit left-subtree, visit right-subtree  Postorder useful for destroying trees Visit left-subtree, visit right-subtree, process root “llama” “tiger” “monkey” “jaguar” “elephant” “ giraffe ”

CompSci 100e 7.6 Tree exercises 1. Build a tree Ø People standing up are nodes that are currently in the tree Ø Point at a sitting down person to make them your child Ø Is it a binary tree? Is it a BST? Ø Traversals, height, deepest leaf? 2. How many different binary search trees are there with specified elements?  E.g given elements {90,13,2,3}, how many possible legal BSTs are there? 3. Convert a binary search tree to a doubly linked list in O(n) time without creating any new nodes.

CompSci 100e 7.7 Insertion and Find? Complexity? l How do we search for a value in a tree, starting at root?  Can do this both iteratively and recursively, contrast to printing which is very difficult to do iteratively  How is insertion similar to search? l What is complexity of print? Of insertion?  Is there a worst case for trees?  Do we use best case? Worst case? Average case? l How do we define worst and average cases  For trees? For vectors? For linked lists? For arrays of linked-lists?

CompSci 100e 7.8 What does insertion look like? l Simple recursive insertion into tree (accessed by root)  root = insert("foo", root); public TreeNode insert(TreeNode t, String s) { if (t == null) t = new Tree(s,null,null); else if (s.compareTo(t.info) <= 0) t.left = insert(t.left,s); else t.right = insert(t.right,s); return t; } l Note: in each recursive call, the parameter t in the called clone is either the left or right pointer of some node in the original tree  Why is this important?  Why must the idiom t = treeMethod(t,…) be used? l What would removal look like?

CompSci 100e 7.9 Tree functions l Compute height of a tree, what is complexity? int height(Tree root) { if (root == null) return 0; else { return 1 + Math.max(height(root.left), height(root.right) ); } l Modify function to compute number of nodes in a tree, does complexity change?  What about computing number of leaf nodes?

CompSci 100e 7.10 Balanced Trees and Complexity l A tree is height-balanced if  Left and right subtrees are height-balanced  Left and right heights differ by at most one boolean isBalanced(Tree root) { if (root == null) return true; return isBalanced(root.left) && isBalanced(root.right) && Math.abs(height(root.left) – height(root.right)) <= 1; }

CompSci 100e 7.11 Rotations and balanced trees l Height-balanced trees  For every node, left and right subtree heights differ by at most 1  After insertion/deletion need to rebalance  Every operation leaves tree in a balanced state: invariant property of tree l Find deepest node that’s unbalanced then make sure:  On path from root to inserted/deleted node  Rebalance at this unbalanced point only Are these trees height- balanced?

CompSci 100e 7.12 What is complexity? l Assume trees are “balanced” in analyzing complexity  Roughly half the nodes in each subtree  Leads to easier analysis l How to develop recurrence relation?  What is T(n)?  What other work is done? l How to solve recurrence relation  Plug, expand, plug, expand, find pattern  A real proof requires induction to verify correctness

CompSci 100e 7.13 Balanced trees we won't study l B-trees are used when data is both in memory and on disk  File systems, really large data sets  Rebalancing guarantees good performance both asymptotically and in practice. Differences between cache, memory, disk are important l Splay trees rebalance during insertion and during search, nodes accessed often more closer to root  Other nodes can move further from root, consequences? Performance for some nodes gets better, for others …  No guarantee running time for a single operation, but guaranteed good performance for a sequence of operations, this is good amortized cost (vector push_back)

CompSci 100e 7.14 Balanced trees we will study l Both kinds have worst-case O(log n) time for tree operations l AVL (Adel’son-Velskii and Landis), 1962  Nodes are “height-balanced”, subtree heights differ by 1  Rebalancing requires per-node bookkeeping of height  l Red-black tree uses same rotations, but can rebalance in one pass, contrast to AVL tree  In AVL case, insert, calculate balance factors, rebalance  In Red-black tree can rebalance on the way down, code is more complex, but doable  Standard java.util.TreeMap/TreeSet use red-black

CompSci 100e 7.15 Rotation to rebalance l When a node N (root) is unbalanced height differs by 2 (must be more than one)  Change N.left.left doLeft  Change N.left.right doLeftRight  Change N.right.left doRightLeft  Change N.right.right doRight l First/last cases are symmetric l Middle cases require two rotations  First of the two puts tree into doLeft or doRight A B C A B C Node doLeft(Node root) { Node newRoot = root.left; root.left = newRoot.right; newRoot.right = root; return newRoot; } N N

CompSci 100e 7.16 Rotation up close (doLeft) l Why is this called doLeft?  N will no longer be root, new value in left.left subtree  Left child becomes new root l Rotation isn’t “to the left”, but rather “brings left child up”  doLeftChildRotate? A B C N A B C N Node doLeft(Node root) { Node newRoot = root.left; root.left = newRoot.right; newRoot.right = root; return newRoot; }

CompSci 100e 7.17 Rotation to rebalance l Suppose we add a new node in right subtree of left child of root  Single rotation can’t fix  Need to rotate twice l First stage is shown at bottom  Rotate blue node right (its right child takes its place)  This is left child of unbalanced B A C N A B C N AB1B1 B2B2 C ????? Node doRight(Node root) { Node newRoot = root.right; root.right = newRoot.left; newRoot.left = root; return newRoot; } B2B2 A B1B1

CompSci 100e 7.18 Double rotation complete AB1B1 B2B2 C B2B2 A B1B1 C B2B2 A B1B1 C l Calculate where to rotate and what case, do the rotations Node doRight(Node root) { Node newRoot = root.right; root.right = newRoot.left; newRoot.left = root; return newRoot; } Node doLeft(Node root) { Node newRoot = root.left; root.left = newRoot.right; newRoot.right = root; return newRoot; }

CompSci 100e 7.19 AVL tree practice l Insert into AVL tree:   After adding 16: doLeftRight  After 3, doLeft on doRight doLeft

CompSci 100e 7.20 AVL practice: continued, and finished l After adding 13, ok l After adding14, not ok  doRight at

CompSci 100e 7.21 Trie: efficient search of words/suffixes l A trie (from retrieval, but pronounced “try”) supports  Insertion: a word into the trie (delete and look up)  These operations are O(size of string) regardless of how many strings are stored in the trie! Guaranteed ! l In some ways a trie is like a 128 (or 26 or alphabet-size) tree, one branch/edge for each character/letter  Node stores branches to other nodes  Node stores whether it ends the string from root to it l Extremely useful in DNA/string processing  monkeys and typewriter simulation which is similar to some methods used in Natural Language understanding (n-gram methods)

CompSci 100e 7.22 Trie picture and code (see trie.cpp) l To add string  Start at root, for each char create node as needed, go down tree, mark last node l To find string  Start at root, follow links If NULL, not found  Check word flag at end l To print all nodes  Visit every node, build string as nodes traversed l What about union and intersection? a c p r n s a r a h ao st cd Indicates word ends here

CompSci 100e 7.23 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array Linked list Hash Maps Binary search tree AVL tree

CompSci 100e 7.24 Boggle: Tries, backtracking, structure Find words on 4x4 grid Adjacent letters:   l No re-use in same word Two approaches to find all words l Try to form every word on board  Look up prefix as you go Trie is useful for prefixes l Look up every word in dictionary  For each word: on board? l ZEAL and SMILES HISE W D BL A M IB Z S VE