Trees. Tree Structure Binary Tree Tree Traversals

Similar documents
Binary Trees

CSC148H Week 8. Sadia Sharmin. July 12, /29

Binary Search Tree. Revised based on textbook author s notes.

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

CSI33 Data Structures

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

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

CSC148 Week 6. Larry Zhang

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

Last week. Last week. Last week. Today. Binary Trees. CSC148 Intro. to Computer Science. Lecture 8: Binary Trees, BST

Binary Trees, Binary Search Trees

Trees. CSE 373 Data Structures

Trees. Truong Tuan Anh CSE-HCMUT

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

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

TREES. Trees - Introduction

CSI33 Data Structures

March 20/2003 Jayakanth Srinivasan,

Binary Tree Application Expression Tree. Revised based on textbook author s notes.

Programming II (CS300)

COSC 2011 Section N. Trees: Terminology and Basic Properties

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

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

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

Garbage Collection: recycling unused memory

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

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

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

Data Structures and Algorithms for Engineers

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

Successor/Predecessor Rules in Binary Trees

Chapter 20: Binary Trees

Stacks, Queues and Hierarchical Collections

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

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

Binary Trees Fall 2018 Margaret Reid-Miller

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

tree nonlinear Examples


Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

Principles of Computer Science

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Topic 14. The BinaryTree ADT

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

Advanced Tree Data Structures

Introduction to Computers and Programming. Concept Question

Trees. Eric McCreath

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

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

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

CS350: Data Structures Tree Traversal

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

Binary Trees. Height 1

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

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

CS302 - Data Structures using C++

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

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

CE 221 Data Structures and Algorithms

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

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

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Why Do We Need Trees?

CS 206 Introduction to Computer Science II

Introduction to Trees. D. Thiebaut CSC212 Fall 2014

Programming II (CS300)

Trees. Trees. CSE 2011 Winter 2007

Trees Algorhyme by Radia Perlman

CS24 Week 8 Lecture 1

Name CPTR246 Spring '17 (100 total points) Exam 3

Algorithms and Data Structures

CS 206 Introduction to Computer Science II

Binary Search Trees. See Section 11.1 of the text.

Cpt S 122 Data Structures. Data Structures Trees

BBM 201 Data structures

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

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

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

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

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

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

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

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

Recursive Data Structures and Grammars

Programming II (CS300)

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud Goodrich, Tamassia, Goldwasser Trees

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

CSC148-Section:L0301

Analysis of Algorithms

Computational Optimization ISE 407. Lecture 19. Dr. Ted Ralphs

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

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Data Structure - Binary Tree 1 -

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

Transcription:

Trees Tree Structure Binary Tree Tree Traversals

The Tree Structure Consists of nodes and edges that organize data in a hierarchical fashion. nodes store the data elements. edges connect the nodes. The organization of the nodes form relationships between the data elements.

Tree Example #1

Tree Example #2

Root Node Topmost node of the tree. Provides the single access point into the tree. Has no incoming edges.

Tree Path The nodes encountered when following the edges from the root node to the destination node. Access to all other nodes must start with the root.

Parent Node The node from which an incoming edge originates. Every node, except the root, has a parent node. A node can only have one parent.

Child Node The nodes to which outgoing edges are connected. Each node can have one or more child nodes. Results in a parent- child relationship. sibling nodes all nodes that have the same parent.

Types of Nodes Nodes can be classified as either: interior node a node that has at least one child. leaf node a node that has no children.

Subtree A tree is by definition a recursive structure. Every node can be the root of its own subtree. A subtree consists of a subset of nodes and edges of the larger tree.

Relatives descendants All nodes of a subtree are the descendants of the subtree's root. Every node in the tree is a descendant of the root. ancestors The ancestors of a node include all of the nodes along the node's path, excluding the node itself. The root is the ancestor of all the other nodes.

The Binary Tree A tree in which each node can have at most two children. The nodes are commonly labeled: left child right child

Binary Tree Properties There are several properties associated with binary trees that depend on the node organization. depth the distance of a node from the root. level all nodes at a given depth share a level. height number of levels in the tree. width number of nodes on the level containing the most nodes. size number of nodes in the tree.

Binary Tree Properties

Binary Tree Properties Given a tree of size n: max height = n min height =

Binary Tree Structure Height of a tree will be important in analyzing the efficiency of binary tree algorithms. Structural properties can play a role in the efficiency of an algorithm.

Full Binary Tree A binary tree in which each interior node contains two children.

Perfect Binary Tree A full binary tree in which all leaf nodes are at the same level.

Complete Binary Tree A binary tree of height h, is a perfect binary tree down to height h 1 and the nodes at the lowest level are filled from left to right (no gaps).

Binary Tree Implementation Commonly implemented as a dynamic structure in the same fashion as linked lists. Can be used to in many different applications. Operations depend on the application. # The storage class for creating binary tree nodes. class _BinTreeNode : def init ( self, data ): self.data = data self.left = None self.right = None

Physical Implementation

Tree Traversals Iterates through the nodes of a tree, one node at a time in order to visit every node. With a linear structure this was simple. How is this done with a hierarchical structure? Must begin at the root node. Every node must be visited. Results in a recursive solution.

Preorder Traversal After visiting the root, traverse the nodes in the left subtree then traverse the nodes in the right subtree.

Preorder Traversal

Preorder Traversal The implementation is rather simple. Given a binary tree of size n, a complete traversal requires O(n) to visit every node. def preordertrav( subtree ): if subtree is not None : print( subtree.data ) preordertrav( subtree.left ) preordertrav( subtree.right )

Inorder Traversal Similar to the preorder traversal, but we traverse the left subtree before visiting the node.

Inorder Traversal The implementation swaps the order of the visit operation and the recursive calls. def inordertrav( subtree ): if subtree is not None : inordertrav( subtree.left ) print( subtree.data ) inordertrav( subtree.right )

Postorder Traversal Is the opposite of the preorder traversal. Traverse both the left and right subtrees before visiting the node.

Breadth- First Traversal The nodes are visited by level, from left to right. The previous traversals are all depth- first traversals.

Breadth- First Traversal Recursion can not be used with this traversal. We can use a queue and an iterative loop. def breadthfirsttrav( bintree ): q = Queue() q.enqueue( bintree ) while not q.isempty() : # Remove the next node from the queue and visit it. node = q.dequeue() print( node.data ) # Add the two children to the queue. if node.left is not None : q.enqueue( node.left ) if node.right is not None : q.enqueue( node.right )