Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees, Binary Trees, and Binary Search Trees COMP171.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
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.
CPS 100, Spring Trees: no data structure lovelier?
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
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.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Trees, Binary Trees, and Binary Search Trees COMP171.
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.
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.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Binary Search Trees (BST)
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.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 CMSC 341 Introduction to Trees Textbook sections:
(c) University of Washington20-1 CSC 143 Java Trees.
CSE 373 Data Structures Lecture 7
From doubly-linked lists to binary trees
Non Linear Data Structure
Data Structure and Algorithms
CC 215 Data Structures Trees
Printing a search tree in order
PFTFBH Binary Search trees Fundamental Data Structure
Data Structures Binary Trees 1.
Data Structures revisited
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?
Week 6 - Wednesday CS221.
Trees.
CSE 373 Data Structures Lecture 7
Compsci 201 Trees, Tries, Tradeoffs
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Trees CSE 373 Data Structures.
Trees Lecture 9 CS2110 – Fall 2009.
common code “abstracted out” same code written several times: don’t!
Binary Trees, Binary Search Trees
Trees.
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Chapter 20: Binary Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient insertion, deletion, and search trees used in many contexts, not just for searching, e.g., expression trees search in O(log n) like sorted array insertion/deletion O(1) like list, once location found! binary trees are inherently recursive, difficult to process trees non-recursively, but possible recursion never required, often makes coding simpler

From doubly-linked lists to binary trees 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? “koala” “llama” “tiger” “monkey” “jaguar” “elephant” “giraffe” “pig” “hippo” “leopard” “koala” “koala” “koala” “koala”

Basic tree definitions Binary tree is a structure: empty root node with left and right subtrees 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, N1, N2, … Nk Ni is parent of Ni+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 E D F C G

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

Insertion and Find? Complexity? 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? What is complexity of print? Of insertion? Is there a worst case for trees? Do we use best case? Worst case? Average case? How do we define worst and average cases For trees? For vectors? For linked lists? For arrays of linked-lists?

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

Tree functions 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)); } Modify function to compute number of nodes in a tree, does complexity change? What about computing number of leaf nodes?

Tree traversals 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”

Insertion into search tree Simple recursive insertion into tree t = insert(t, "foo"); Tree insert(Tree 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; } 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?

Balanced Trees and Complexity 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; else return isBalanced(root.left) && isBalanced(root.right) && Math.abs(height(root.left) – height(root.right)) <= 1; }

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

Danny Hillis The third culture consists of those scientists and other thinkers in the empirical world who, through their work and expository writing, are taking the place of the traditional intellectual in rendering visible the deeper meanings of our lives, redefining who and what we are. (Wired 1998) And now we are beginning to depend on computers to help us evolve new computers that let us produce things of much greater complexity. Yet we don't quite understand the process - it's getting ahead of us. We're now using programs to make much faster computers so the process can run much faster. That's what's so confusing - technologies are feeding back on themselves; we're taking off. We're at that point analogous to when single-celled organisms were turning into multicelled organisms. We are amoebas and we can't figure out what the hell this thing is that we're creating.