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

Similar documents
Trees. CSE 373 Data Structures

Binary 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

Binary Trees, Binary Search Trees

Binary trees. Binary trees. Binary trees

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

Trees. Trees. CSE 2011 Winter 2007

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

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

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

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

TREES. Trees - Introduction

OUTLINE. General Trees (Ch. 7.1) Binary Trees (Ch. 7.3) Tree Traversals (Ch. 7.2)

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

CE 221 Data Structures and Algorithms

Trees. 9/21/2007 8:11 AM Trees 1

Programming II (CS300)

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

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

Trees 3/19/14. Mammal. Dog Pig Cat

Trees Algorhyme by Radia Perlman

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

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

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

Trees. Data structures and Algorithms. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme

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

Why Do We Need Trees?

CSI33 Data Structures

March 20/2003 Jayakanth Srinivasan,

Trees. Truong Tuan Anh CSE-HCMUT

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

Topic 14. The BinaryTree ADT

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

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

tree nonlinear Examples

Data Structures and Algorithms. Trees

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

Trees and Binary Trees

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

Data Structures. Trees, Binary trees & Binary Search Trees

Stacks, Queues and Hierarchical Collections

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

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

Algorithms and Data Structures

Trees. Tree Structure Binary Tree Tree Traversals

12 Abstract Data Types

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

Chapter 20: Binary Trees

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Recursive Data Structures and Grammars

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

Introduction to Computers and Programming. Concept Question

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 02/06/2006 Trees 1

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

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

Stock Fraud. Nancy Amato. Parasol Lab, Dept. CSE, Texas A&M University

Outline. Definitions Traversing trees Binary trees

DATA STRUCTURES USING C

CSI33 Data Structures

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

Binary Trees Fall 2018 Margaret Reid-Miller

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

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

Chapter 5. Binary Trees

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

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

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

Garbage Collection: recycling unused memory

Trees. Eric McCreath

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

Data Structures Question Bank Multiple Choice

BBM 201 Data structures

CSC148 Week 6. Larry Zhang

Binary Trees. Height 1

Tree traversals and binary trees

CSCI2100B Data Structures Trees

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 1/18/2005 4:17 AM Trees 1

Trees, Binary Trees, and Binary Search Trees

COSC 2011 Section N. Trees: Terminology and Basic Properties

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

Data Structures Lecture 6

3137 Data Structures and Algorithms in C++

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

Data Structure. IBPS SO (IT- Officer) Exam 2017

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

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

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

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

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

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

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

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

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.


COMP 250 Fall binary trees Oct. 27, 2017

Graph Algorithms Using Depth First Search

Transcription:

Tree A tree consists of a set of nodes and a set of edges connecting pairs of nodes. A tree has the property that there is exactly one path (no more, no less) between any pair of nodes. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Rooted Tree In a rooted tree, one distinguished node is called the root. In the above figure, we chose H to be the root. Every node c, except the root, has exactly one parent node p, which is the first node after c on the path from c to the root. Node c is node p child. So, K is the parent of D, and K is a child of F. The root has no parent. A node can have any number of children.

Other definitions A leaf is a node with no children. An internal node is a non-leaf node Siblings are nodes with the same parent. The ancestors of a node d are the nodes on the path from d to the root. This includes node d. If node a is an ancestor of node d, then d is a descendant of a. The length of a path is the number of edges in the path. The depth of a node n is the length of the path from n to the root. (The depth of the root is zero.) The height of a node n is the length of the path from n to its deepest descendant. (The height of a leaf node is zero.) The height of a tree is the depth of its deepest node = height of the root.

What is the depth of node D? What is the height of K? What is the height of the rooted tree? The subtree rooted at node n is the tree formed by n and its descendants. What is the subtree rooted at node K?

Binary tree A binary tree is a tree in which no node has more than two children, and every child is either a left child or a right child even if it is the only child its parent has. (Source: Wikipedia) A full binary tree is one in which every internal node has two children. Is the above tree a full binary tree?

Tree data structure A tree node has three references: 1. The item that it stores 2. Its parent node 3. The list of children public static class Node<T> { private T data; private Node<T> parent; private List<Node<T>> children; } Another option is that the parent only stores information about the first child. Each child also links with the next sibling. Thus the siblings are directly linked Public class Node { Object item Node parent; Node firstchild; Node NextSibling; }

Sample methods Depth of a node p public int depth(node p) { if (isroot(p)) return 0; else return 1+depth(parent(p)) } Height of a subtree public int height(node p) { int h = 0 for (node c: children(p)) h = Math.max(h, 1+ height(c); }

The Tree ADT A tree ADT can have the following methods. getelement (node p) root ( ) children (node p) parent (node p) Return the object stored at node p Returns the root node Returns the children of node p Returns the parent of node p isleaf (node p) isroot (node p) size (node p) Returns true if node p is a leaf Returns true if node p is the root Returns the number of nodes and many more

Binary tree as Decision Tree Troubleshooting of a TV set. A decision tree is a tree in which each internal node contains a question or an option that has a finite number of responses. When there are two possible responses, the tree is a binary decision tree. Nodes that are conclusions are leaf nodes.

A game tree (not a binary tree)

Properties of a full binary tree Parameters n number of nodes e number of leaf nodes i number of internal nodes h height Properties e = i + 1 n = 2e - 1 h i (How will you show these are true?) h (n - 1)/2 e 2 h h log 2 e h log 2 (n + 1) - 1

A Recursive Definition of a Binary Tree A full binary tree is A single node is a full binary tree A new root node that is connected with the roots of two full binary trees is also a full binary tree

Implementing a Binary Tree Use a linked structure left child parent ELEMENT right child A single node Now recall how a linked list was implemented. Running times Method Running time isempty O(1) Root, parent, children O(1) Depth (p) O(d p +1) Height O(n)

Array-based representation 0 1 2 3 4 5 6 9 10 Rank (root)=0 Rank (left child) = 2 x rank of parent+1 Rank (right child) = 2x rank of parent + 2

Traversal Algorithms of a Binary Tree Visit each node of a tree in an organized manner. PostOrder Traversal (Figure source: Wikipedia) Order: A, C, E, D, B, H, I, G, F Algorithm postorder(v) for each child w of v postorder (w) visit(v)

InOrder Traversal (Figure source: Wikipedia) Order: A, B, C, D, E, F, G, H, I Algorithm inorder(v) if left (v) null inorder (left (v)) visit(v) if right(v) null inorder (right (v)) We follow this in writing mathematical expressions.

Preorder traversal (Figure source: Wikipedia) Order: F, B, A, D, C, E, G, I, H

Depth-first traversal and Breadth-first traversal Depth-first traversal Pre-order traversal leads to a depth-first traversal. One can use a stack for generating this order.

Breadth First traversal Breadth-first order of traversal can be generated using a queue.

Representing Expressions / * + 24 4-6 3 15 InOrder traversal: (24/4) * ((3-15) + 6)) PostOrder traversal: 24 4 / 3 15-6 + * a.k.a. Reverse Polish Notation, no need for parentheses How can you evaluate an expression presented in the Reverse Polish Notation? Use a single stack.

Evaluating an expression in the Reverse Polish Notation Consider the input string as a stream of tokens. Assume that the input is a meaningful one. 1. If the token is a value, then push into the stack 2. If the token is an operator, then pop k values from the stack, (where k is the number of operands for that operator), apply it on the popped values, and push the result into the stack 3. After the last token, the stack will contain only one element, which is the result.

Consider the following tree showing the composition of a book. What kind of tree traversal is this?