Trees. Lecture 8 CS2110 Fall 2011

Similar documents
Ordered Lists and Binary Trees

Binary Search Trees (BST)

Data Structures and Algorithms

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Binary Trees and Huffman Encoding Binary Search Trees

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Algorithms and Data Structures

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Data Structure with C

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Analysis of Algorithms I: Binary Search Trees

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Search Trees CMPSC 122

Chapter 14 The Binary Search Tree

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

DATA STRUCTURES USING C

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Binary Search Trees. Ric Glassey

Binary Heap Algorithms

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Data Structure [Question Bank]

TREE BASIC TERMINOLOGIES

Data Structures. Level 6 C Module Descriptor

Algorithms Chapter 12 Binary Search Trees

10CS35: Data Structures Using C

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Converting a Number from Decimal to Binary

PES Institute of Technology-BSC QUESTION BANK

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

How To Create A Tree From A Tree In Runtime (For A Tree)

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April

GRAPH THEORY LECTURE 4: TREES

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Algorithms and Data Structures Written Exam Proposed SOLUTION

Lecture Notes on Binary Search Trees

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Symbol Tables. Introduction

Big Data and Scripting. Part 4: Memory Hierarchies

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Why Use Binary Trees?

Exam study sheet for CS2711. List of topics

Data Structures Fibonacci Heaps, Amortized Analysis

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

From Last Time: Remove (Delete) Operation

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Any two nodes which are connected by an edge in a graph are called adjacent node.

Alex. Adam Agnes Allen Arthur

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Lecture Notes on Binary Search Trees

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

International Journal of Advanced Research in Computer Science and Software Engineering

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Graduate Assessment Test (Sample)

Full and Complete Binary Trees

Introduction to Data Structures and Algorithms

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Analysis of Algorithms I: Optimal Binary Search Trees

Persistent Binary Search Trees

Object Oriented Software Design

The ADT Binary Search Tree

Binary Heaps. CSE 373 Data Structures

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Database Design Patterns. Winter Lecture 24

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Data Structures and Algorithms Written Examination

Data storage Tree indexes

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Lecture 2: Regular Languages [Fa 14]

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Parallelization: Binary Tree Traversal

Heaps & Priority Queues in the C++ STL 2-3 Trees

Lecture 2 February 12, 2003

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

Chapter 8: Binary Trees

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

CSE 326: Data Structures B-Trees and B+ Trees

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

Sample Questions Csci 1112 A. Bellaachia

Binary Search Trees 3/20/14

Data Structures UNIT III. Model Question Answer

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

LINKED DATA STRUCTURES

Optimal Binary Search Trees Meet Object Oriented Programming

Data Structures and Data Manipulation

Binary Coded Web Access Pattern Tree in Education Domain

Optimizations. Optimization Safety. Optimization Safety. Control Flow Graphs. Code transformations to improve program

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

6 Creating the Animation

Lexical analysis FORMAL LANGUAGES AND COMPILERS. Floriano Scioscia. Formal Languages and Compilers A.Y. 2015/2016

Data Structures Using C++

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

SNMP....Simple Network Management Protocol...

Transcription:

Trees Lecture 8 CS2110 Fall 2011

Tree Overview Tree: recursive data structure (similar to list) Each cell may have two or more successors (or children) Each cell has at most one predecessor (or parent) Distinguished cell called root has no parent All cells reachable from root Binary tree: tree in which each cell can have at most two children: a left child and a right child 5 4 2 7 8 9 General tree 5 4 7 8 Not a tree 5 4 2 7 8 Binary tree 5 6 8 List-like tree 2

Tree Terminology M is the root of this tree G is the root of the left subtree of M B, H, J, N, and S are leaves N is the left child of P; S is the right child P is the parent of N M and G are ancestors of D P, N, and S are descendants of W Node J is at depth 2 (i.e., depth = length of path from root = number of edges) Node W is at height 2 (i.e., height = length of longest path to a leaf) A collection of several trees is called a...? B M G W D J P H N S 3

Class for Binary Tree Cells class TreeCell<T> {! private T datum;! private TreeCell<T> left, right;! }! public TreeCell(T x) { datum = x; }! public TreeCell(T x, TreeCell<T> l,! TreeCell<T> r) {! datum = x;! left = l;! right = r;! }! more methods: getdatum, setdatum,! getleft, setleft, getright, setright!... new TreeCell<String>("hello")...! 4

Class for General Trees class GTreeCell {! }! private Object datum;! private GTreeCell left;! private GTreeCell sibling;! appropriate getter and! setter methods! Parent node points directly only to its leftmost child Leftmost child has pointer to next sibling, which points to next sibling, etc 5 4 2 7 8 9 7 8 3 1 5 4 2 7 8 9 7 8 3 1 General tree Tree represented using GTreeCell 5

Applications of Trees Most languages (natural and computer) have a recursive, hierarchical structure This structure is implicit in ordinary textual representation Recursive structure can be made explicit by representing sentences in the language as trees: Abstract Syntax Trees (ASTs) ASTs are easier to optimize, generate code from, etc. than textual representation A parser converts textual representations to AST 6

Example Expression grammar: E integer E (E + E) In textual representation Parentheses show hierarchical structure In tree representation Hierarchy is explicit in the structure of the tree Text AST Representation -34-34 (2 + 3) + 2 3 ((2+3) + (5+7)) + + + 2 3 5 7 7

Recursion on Trees Recursive methods can be written to operate on trees in an obvious way Base case empty tree leaf node Recursive case solve problem on left and right subtrees put solutions together to get solution for full tree 8

Searching in a Binary Tree public static boolean treesearch(object x,! }! TreeCell node) {! if (node == null) return false;! if (node.datum.equals(x)) return true;! return treesearch(x, node.left)! treesearch(x, node.right);! Analog of linear search in lists: given tree and an object, find out if object is stored in tree Easy to write recursively, harder to write iteratively 2 9 0 8 3 5 7 9

Binary Search Tree (BST) If the tree data are ordered in any subtree, 5 All left descendents of node come before node All right descendents of node come after node 2 8 This makes it much faster to search 0 3 7 9 public static boolean treesearch (Object x, TreeCell node) {! }! if (node == null) return false;! if (node.datum.equals(x)) return true;! if (node.datum.compareto(x) > 0)! return treesearch(x, node.left);! else return treesearch(x, node.right);! 10

Building a BST To insert a new item Pretend to look for the item jan Put the new node in the place where you fall off the tree feb mar This can be done using either recursion or iteration apr jun may Example Tree uses alphabetical order Months appear for insertion in calendar order jul 11

What Can Go Wrong? A BST makes searches very fast, unless Nodes are inserted in alphabetical order In this case, we re basically building a linked list (with some extra wasted space for the left fields that aren t being used) apr feb jan jul BST works great if data arrives in random order jun mar may 12

Printing Contents of BST Because of the ordering rules for a BST, it is easy to print the items in alphabetical order Recursively print everything in the left subtree Print the node Recursively print everything in the right subtree /**! * Show the contents of the BST in! * alphabetical order! */! public void show() {! show(root);! System.out.println();! }! private static void show(treenode node) {! if (node == null) return;! show(node.lchild);! System.out.print(node.datum + " ");! show(node.rchild);! }! 13

Tree Traversals Walking over the whole tree is a tree traversal This is done often enough that there are standard names The previous example is an inorder traversal Process left subtree Process node Process right subtree Note: we re using this for printing, but any kind of processing can be done There are other standard kinds of traversals Preorder traversal Process node Process left subtree Process right subtree Postorder traversal Process left subtree Process right subtree Process node Level-order traversal Not recursive Uses a queue 14

Some Useful Methods //determine if a node is a leaf! public static boolean isleaf(treecell node) {! return (node!= null) && (node.left == null)! && (node.right == null);! }! //compute height of tree using postorder traversal! public static int height(treecell node) {! if (node == null) return -1; //empty tree! if (isleaf(node)) return 0;! return 1 + Math.max(height(node.left),! height(node.right));! }! //compute number of nodes using postorder traversal! public static int nnodes(treecell node) {! if (node == null) return 0;! return 1 + nnodes(node.left) + nnodes(node.right);! }! 15

Useful Facts about Binary Trees 2 d = maximum number of nodes at depth d depth 0 5 If height of tree is h Minimum number of nodes in tree = h + 1 Maximum number of nodes in tree = 2 0 + 2 1 + + 2 h = 2 h+1 1 1 2 4 7 8 2 0 4 Height 2, maximum number of nodes Complete binary tree All levels of tree down to a certain depth are completely filled 5 2 4 Height 2, minimum number of nodes 16

Tree with Parent Pointers In some applications, it is useful to have trees in which nodes can reference their parents 4 5 2 7 8 Analog of doubly-linked lists 17

Things to Think About What if we want to delete data from a BST? jan feb mar A BST works great as long as it s balanced apr jun may How can we keep it balanced? jul 18

Suffix Trees Given a string s, a suffix tree for s is a tree such that each edge has a unique label, which is a nonnull substring of s any two edges out of the same node have labels beginning with different characters the labels along any path from the root to a leaf concatenate together to give a suffix of s all suffixes are represented by some path the leaf of the path is labeled with the index of the first character of the suffix in s Suffix trees can be constructed in linear time 19

Suffix Trees a dabra$ bra $ ra cadabra$ dabra$ $ bra cadabra$ $ cadabra$ $ cadabra$ $ cadabra$ abracadabra$ 20

Suffix Trees Useful in string matching algorithms (e.g., longest common substring of 2 strings) Most algorithms linear time Used in genomics (human genome is ~4GB) 21

Huffman Trees 0 1 0 1 0 e 197 1 0 1 0 e t a s 63 40 26 t 1 0 a 1 s Fixed length encoding 197*2 + 63*2 + 40*2 + 26*2 = 652 Huffman encoding 197*1 + 63*2 + 40*3 + 26*3 = 521 22

Huffman Compression of Ulysses ' ' 242125 00100000 3 110 'e' 139496 01100101 3 000 't' 95660 01110100 4 1010 'a' 89651 01100001 4 1000 'o' 88884 01101111 4 0111 'n' 78465 01101110 4 0101 'i' 76505 01101001 4 0100 's' 73186 01110011 4 0011 'h' 68625 01101000 5 11111 'r' 68320 01110010 5 11110 'l' 52657 01101100 5 10111 'u' 32942 01110101 6 111011 'g' 26201 01100111 6 101101 'f' 25248 01100110 6 101100 '.' 21361 00101110 6 011010 'p' 20661 01110000 6 011001... '7' 68 00110111 15 111010101001111 '/' 58 00101111 15 111010101001110 'X' 19 01011000 16 0110000000100011 '&' 3 00100110 18 011000000010001010 '%' 3 00100101 19 0110000000100010111 '+' 2 00101011 19 0110000000100010110 original size 11904320 compressed size 6822151 42.7% compression 23

BSP Trees BSP = Binary Space Partition Used to render 3D images composed of polygons Each node n has one polygon p as data Left subtree of n contains all polygons on one side of p Right subtree of n contains all polygons on the other side of p Order of traversal determines occlusion! 24

Tree Summary A tree is a recursive data structure Each cell has 0 or more successors (children) Each cell except the root has at exactly one predecessor (parent) All cells are reachable from the root A cell with no children is called a leaf Special case: binary tree Binary tree cells have a left and a right child Either or both children can be null Trees are useful for exposing the recursive structure of natural language and computer programs 25