Data Structures in Java

Similar documents
Binary Trees, Binary Search Trees

Recursion examples: Problem 2. (More) Recursion and Lists. Tail recursion. Recursion examples: Problem 2. Recursion examples: Problem 3

Programming II (CS300)

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Trees. Trees. CSE 2011 Winter 2007

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

CE 221 Data Structures and Algorithms

Principles of Computer Science

Data Structures in Java

Trees, Binary Trees, and Binary Search Trees

TREES. Trees - Introduction

Outline: Software Design

XML and Databases. Lecture 3 XML into RDBMS. Sebastian Maneth NICTA and UNSW

Back To LinkedLists 1. 3 public Node left; 4 public Node right; 6 public Node(int data, Node left, Node right) {

Tree Structures. A hierarchical data structure whose point of entry is the root node

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Gray Codes for Reflectable Languages

L11 Balanced Trees. Alice E. Fischer. Fall Alice E. Fischer L11 Balanced Trees... 1/34 Fall / 34

Binary Trees

Tree traversals and binary trees

CSCI2100B Data Structures Trees

Trees. CSE 373 Data Structures

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

Extracting Partition Statistics from Semistructured Data

CS24 Week 8 Lecture 1

Garbage Collection: recycling unused memory

Why Do We Need Trees?

COMP : Trees. COMP20012 Trees 219

CS 151. Binary Trees. Friday, October 5, 12

Topic 14. The BinaryTree ADT

Interconnection Styles

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

CS350: Data Structures Red-Black Trees

Building Java Programs

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

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

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

tree nonlinear Examples

Computational Biology 6.095/6.895 Database Search Lecture 5 Prof. Piotr Indyk

BBM 201 Data structures

Algorithms for External Memory Lecture 6 Graph Algorithms - Weighted List Ranking

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

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Trees. See Chapter 18 of Weiss

Data Structures and Algorithms

csci 210: Data Structures Trees

Trees, Part 1: Unbalanced Trees

CSE 326: Data Structures Binary Search Trees

Heaps. 2/13/2006 Heaps 1

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

CSI33 Data Structures

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

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

Calculation of typical running time of a branch-and-bound algorithm for the vertex-cover problem

A Binary Search Tree Implementation

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS

Data Structures and Algorithms for Engineers

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

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

CS 206 Introduction to Computer Science II

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

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

Compilation Lecture 11a. Register Allocation Noam Rinetzky. Text book: Modern compiler implementation in C Andrew A.

Binary Trees Fall 2018 Margaret Reid-Miller

COMP 181. Prelude. Intermediate representations. Today. Types of IRs. High-level IR. Intermediate representations and code generation

Multi-Way Search Tree

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Trees. A tree is a directed graph with the property

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

Trees. Truong Tuan Anh CSE-HCMUT

CIS265/ Trees Red-Black Trees. Some of the following material is from:

INF2220: algorithms and data structures Series 1

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

CSE 230 Intermediate Programming in C and C++ Binary Tree

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

CS302 - Data Structures using C++

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Binary trees. Binary trees. Binary trees

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

Objectives. In this session, you will learn to:

Algorithms. Deleting from Red-Black Trees B-Trees

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

CSE 530A. B+ Trees. Washington University Fall 2013

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

LECTURE 13 BINARY TREES

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

Checkout BinaryTrees from SVN. Java Collections Framework Intro to Trees


Transcription:

Data Strutures in Java Leture 8: Trees and Tree Traversals. 10/5/2015 Daniel Bauer 1

Trees in Computer Siene A lot of data omes in a hierarhial/nested struture. Mathematial expressions. Program struture. File systems. Deision trees. Natural Language Syntax, Taxonomies, Family Trees,

Example: Expression Trees (a b ) (d e f) g a g b f d e

More Effiient Algorithms with Trees Sometimes we an represent data in a tree to speed up algorithms. Only need to onsider part of the tree to solve ertain problems: Searhing, Sorting, Can often speed up O(N) algorithms to O(log N) one data is represented as a tree.

Tree ADT A tree T onsists of A root node r. T zero or more nonempty subtrees T1, T2, TN, eah onneted by a direted edge from r. Support typial olletion operations: size, get, set, add, remove, find,

Tree ADT r A tree T onsists of A root node r. T1 T2 Tn zero or more nonempty subtrees T1, T2, TN, eah onneted by a direted edge from r. Support typial olletion operations: size, get, set, add, remove, find,

Tree Terminology A Root B C D Leaves E F G

Tree Terminology A Parent of B, C, D B C D Children of A E F G

Tree Terminology A B C D Siblings E F G

Tree Terminology Grandparent of E, F A Parent of B, C, D B C D Children of A E F Grandhildren of A G

Tree Terminology A Path from A to E. Length = 2 B C D E F Path from n1 to nk : the sequene of nodes nk, n2,, nk, suh that ni is the parent of ni1 for 1 i<k. G Length of a path: k-1 = number of edges on the path

Tree Terminology A Path from A to E depth of E = 2 B C D E F Depth of nk: the length of the path from root to nk. G

Tree Terminology A height = 3 B C D E F Height of tree T: the length of the longest path from root to a leaf. G

Representing Trees Option 1: Every node has fixed number of referenes to hildren. n0 n1 n2 n3 Problem: Only reasonable for small or onstant number of hildren.

Binary Trees For binary trees, the number of hildren is at most two. Binary trees are very ommon in data strutures and algorithms. Binary tree algorithms are onvenient to analyze.

Implementing Binary Trees publi lass BinaryTree<T> { // The BinaryTree is essentially just a wrapper around the // linked struture of BinaryNodes, rooted in root. private BinaryNode<T> root; } / Represent a binary subtree. / private stati lass BinaryNode<T>{ } publi T data; publi BinaryNode<T> left; publi BinaryNode<T> right;

Representing Trees Option 2: Organize siblings as a linked list. 1st hild n0 next sibling n1 n2 n3 1st hild next sibling Problem: Takes longer to find a node from the root.

Siblings as Linked List A B C D E F G

Siblings as Linked List A B C D E F G

Implementing Siblings as Linked List publi lass LinkedSiblingTree<E> { private TreeNode<E> root; private stati lass TreeNode<E> { E element; TreeNode<E> firstchild; TreeNode<E> nextsibling; } }...

Full Binary Trees In a full binary tree every node is either a leaf. or has exatly two hildren. D B A C F E G not full

Full Binary Trees In a full binary tree every node is either a leaf. or has exatly two hildren. D F B E A G C full

Complete Binary Trees A omplete binary tree is a full binary tree in whih all levels (exept possibly the last) are ompletely filled. A B C D E F G

Complete Binary Trees A omplete binary tree is a full binary tree in whih all levels (exept possibly the last) are ompletely filled. A B C D E F G full, but not omplete

Complete Binary Trees A omplete binary tree is a binary tree in whih all levels (exept possibly the last) are ompletely filled and every node is as far left as possible. A B C D E

Complete Binary Trees A omplete binary tree is a binary tree in whih all levels (exept possibly the last) are ompletely filled and every node is as far left as possible. A B C D E omplete

Complete Binary Trees A omplete binary tree is a binary tree in whih all levels (exept possibly the last) are ompletely filled and every node is as far left as possible. A B C D E F omplete but not full

Storing Complete Binary Trees in Arrays A B C D E F 0 1 2 3 4 5 A B C D E F Struture of the tree only depends on the number of nodes.

Example Binary Tree: Expression Trees a g b f d e

Tree Traversals: In-order 1. Proess left hild 2. Proess root 3. Proess right hild (a b ) (d e f) g a g b f d e

Tree Traversals: Post-order 1. Proess left hild 2. Proess right hild 3. Proess root a b d e f g a g b f d e

Tree Traversals: Pre-order 1. Proess root 2. Proess left hild 3. Proess right hild a b d e f g a g b f d e

Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a g b f d e

Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a g b f d e

Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a a g b f d e

Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a a Depending on traversal order (in-/post order), keep node on stak or pop it. Let s do post order. g b f d e

Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a a g b f d e

Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a b a g b f d e

Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a b a g b f d e

Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a b a g b f d e

Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a b a g b f d e

Tree Traversals and Staks Keep nodes that still need to be proessed on a stak. a b a g b f d e

Tree Traversal using Reursion We often use reursion to traverse trees (making use of Java s method all stak impliitly). publi void printtree(int indent ) { for (i=0;i<indent;i) System.out.print(" "); } System.out.println( data); // Node if( left!= null ) left.printtree(indent 1); // Left if( right!= null ) right.printtree(indent 1); // Right 30

Bare-bones Implementation of a Binary Tree Publi methods in BinaryTree usually all reursive methods, implementation either in BinaryNode or in BinaryTree. (sample ode)

Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: pop the top 2 trees t1 and t2 push t1 t1 pop the result. 5

Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: pop the top 2 trees t1 and t2 push t1 t1 27 pop the result. 5

Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: pop the top 2 trees t1 and t2 push 2 27 t1 pop the result. t1 5

Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: 3 pop the top 2 trees t1 and t2 push 2 27 t1 pop the result. t1 5

Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: pop the top 2 trees t1 and t2 push 2 3 t1 t1 27 pop the result. 5

Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: / pop the top 2 trees t1 and t2 push 27 t1 t1 2 3 pop the result. 5

Construting Expression Trees using a Stak 5 27 2 3 / for in input if is an operand, push a tree if is an operator: pop the top 2 trees t1 and t2 push 5 / t1 t1 27 pop the result. 2 3