TREES. Trees - Introduction

Similar documents
7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Binary Trees, Binary Search Trees

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

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

Programming II (CS300)

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

March 20/2003 Jayakanth Srinivasan,

Binary Trees

Chapter 20: Binary Trees

Trees. Truong Tuan Anh CSE-HCMUT

Associate Professor Dr. Raed Ibraheem Hamed

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

Data Structures and Algorithms for Engineers

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

Friday Four Square! 4:15PM, Outside Gates

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CE 221 Data Structures and Algorithms

Garbage Collection: recycling unused memory

Trees. Trees. CSE 2011 Winter 2007

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

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Binary Trees and Binary Search Trees

Binary Search Tree (2A) Young Won Lim 5/17/18

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

IX. Binary Trees (Chapter 10)

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

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Binary Trees Fall 2018 Margaret Reid-Miller

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

Algorithms. Deleting from Red-Black Trees B-Trees

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

Trees, Binary Trees, and Binary Search Trees

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

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

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

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Introduction to Computers and Programming. Concept Question

CS 171: Introduction to Computer Science II. Binary Search Trees

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

INF2220: algorithms and data structures Series 1

CSI33 Data Structures

Data Structure - Binary Tree 1 -

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)


Cpt S 122 Data Structures. Data Structures Trees

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Formal Languages and Automata Theory, SS Project (due Week 14)

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

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

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

Partha Sarathi Mandal

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

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

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

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

12 Abstract Data Types

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Stacks, Queues and Hierarchical Collections

CSC148 Week 6. Larry Zhang

Binary Trees. Examples:

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

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Lecture 26. Introduction to Trees. Trees

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

Binary search trees (chapters )

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

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

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Trees. CSE 373 Data Structures

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

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Binary search trees (chapters )

Multi-Way Search Tree

CS24 Week 8 Lecture 1

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

CS301 - Data Structures Glossary By

Analysis of Algorithms

CSE 214 Computer Science II Introduction to Tree

Binary search trees (BST) Binary search trees (BST)

COMP 250 Fall binary trees Oct. 27, 2017

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CSI33 Data Structures

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Self-Balancing Search Trees. Chapter 11

Objectives. In this session, you will learn to:

TREES Lecture 10 CS2110 Spring2014

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

CS350: Data Structures Red-Black Trees

Topic 14. The BinaryTree ADT

Principles of Computer Science

Algorithms and Data Structures

We have the pointers reference the next node in an inorder traversal; called threads

Transcription:

TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n) Trees are nonlinear and hierarchical Tree nodes can have multiple successors (but only one predecessor) 1

Tree Terminology and Applications Section 6.1 Tree Terminology (cont.) A tree consists of a collection of elements or nodes, with each node linked to its successors cat dog wolf Node, Link Root Branches Successors, Children Predecessors, Parent Siblings Leaf Node Subtree canine A subtree of a node is a tree whose root is a child of that node 2

Tree Terminology (cont.) A tree consists of a collection of elements or nodes, with each node linked to its successors Level 2 Level 1 cat dog wolf The level of a node is its distance from the root plus 1 Level 3 canine Tree Terminology (cont.) A tree consists of a collection of elements or nodes, with each node linked to its successors Level 1 dog Level 2 cat wolf The level of a node is defined recursively Level 3 canine If node n is the root of tree T, its level is 1 If node n is not the root of tree T, its level is 1 + the level of its parent 3

Tree Terminology (cont.) A tree consists of a collection of elements or nodes, with each node linked to its successors The height of a tree is the number of nodes in the longest path from the root node to a leaf node cat dog wolf Node, Link Root Branches Successors, Children Predecessors, Parent Siblings Leaf Node Subtree Level Height canine Tree Terminology (cont.) A tree consists of a collection of elements or nodes, with each node linked to its successors The height of a tree is the number of nodes in the longest path from the root node to a leaf node cat dog wolf The height of this tree is 3 canine 4

Common Types of Trees Binary Tree Expression Trees Huffman Trees Binary Search Trees Many many more! Binary Trees In a binary tree, each node has two subtrees A set of nodes T is a binary tree if either of the following is true T is empty Its root node has two subtrees, T L and T R, such that T L and T R are binary trees (T L = left subtree; T R = right subtree) 5

Full, Perfect, and Complete Binary Trees A full binary tree is a binary tree where all nodes have either 2 children or 0 children (the leaf nodes) 0 1 3 7 9 10 2 5 11 12 13 Node, Link Root Branches Successors, Children Predecessors, Parent Siblings Leaf Node Subtree Binary Tree Binary Search Tree Full Binary Tree 4 6 Full, Perfect, and Complete Binary Trees (cont.) A perfect binary tree is a full binary tree of height n with exactly 2 n 1 nodes In this case, n = 3 and 2 n 1 = 7 0 1 2 3 4 5 6 Node, Link Root Branches Successors, Children Predecessors, Parent Siblings Leaf Node Subtree Binary Tree Binary Search Tree Full Binary Tree Perfect Binary Tree 6

Full, Perfect, and Complete Binary Trees (cont.) A complete binary tree is a perfect binary tree through level n - 1 with some extra leaf nodes at level n (the tree height), all toward the left 0 1 2 3 4 5 Node, Link Root Branches Successors, Children Predecessors, Parent Siblings Leaf Node Subtree Binary Tree Binary Search Tree Full Binary Tree Perfect Binary Tree Complete Binary Tree Expression Tree Each node contains an operator or an operand Operands are stored in leaf nodes Parentheses are not stored in the tree because the tree structure dictates the order of operand evaluation Operators in nodes at higher tree levels are evaluated after operators in nodes at lower tree levels (x + y) * ((a + b) / c) 7

Binary Search Tree Binary search trees All elements in the left subtree precede those in the right subtree A formal definition: cat dog wolf canine A set of nodes T is a binary search tree if either of the following is true: T is empty If T is not empty, its root node has two subtrees, T L and T R, such that T L and T R are binary search trees and the value in the root node of T is greater than all values in T L and is less than all values in T R Binary Search Tree (cont.) A binary search tree never has to be sorted because its elements always satisfy the required order relationships When new elements are inserted (or removed) properly, the binary search tree maintains its order In contrast, a sorted array must be expanded whenever new elements are added, and compacted whenever elements are removed expanding and contracting are both O(n) 8

Binary Search Tree (cont.) When searching a BST, each probe has the potential to eliminate half the elements in the tree, so searching can be O(log n) In the worst case, searching is O(n) Recursive Algorithm for Searching a Binary Tree 1. if the tree is empty 2. return null (target is not found) else if the target matches the root node's data 3. return the data stored at the root node else if the target is less than the root node's data 4. return the result of searching the left subtree of the root else 5. return the result of searching the right subtree of the root 9

Tree Traversals Section 6.2 Tree Traversals Often we want to determine the nodes of a tree and their relationship We can do this by walking through the tree in a prescribed order and visiting the nodes as they are encountered This process is called tree traversal Three common kinds of tree traversal Inorder Preorder Postorder 10

Tree Traversals (cont.) Preorder: visit root node, traverse T L, traverse T R Inorder: traverse T L, visit root node, traverse T R Postorder: traverse T L, traverse T R, visit root node Visualizing Tree Traversals a b c d e f g h i j a b d g e h c f i j 11

Visualizing Tree Traversals a b c d e f g h i j d g b h e a i f j c Visualizing Tree Traversals a b c d e f g h i j g d h e b i j f c a 12

Tree Traversals (cont.) Preorder: visit root node, traverse T L, traverse T R Inorder: traverse T L, visit root node, traverse T R Postorder: traverse T L, traverse T R, visit root node Traversals of Binary Search Trees and Expression Trees An inorder traversal of a binary search tree results in the nodes being visited in sequence by increasing data value canine cat dog wolf canine, cat, dog, wolf 13

Traversals of Binary Search Trees and Expression Trees (cont.) An inorder traversal of an expression tree results in the sequence x + y * a + b / c x + y * + / c If we insert parentheses where they belong, we get the infix form: a b (x + y) * ((a + b) / c) Traversals of Binary Search Trees and Expression Trees (cont.) A postorder traversal of an expression tree results in the sequence x y + a b + c / * x + y * + / c This is the postfix or reverse polish form of the expression a b Operators follow operands 14

Traversals of Binary Search Trees and Expression Trees (cont.) A preorder traversal of an expression tree results in the sequence * + x y / + a b c x + y * + / c This is the prefix or forward polish form of the expression a b Operators precede operands 15