Trees and Binary Trees

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

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

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

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

Data Structures. Trees, Binary trees & Binary Search Trees

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

Data Structures and Algorithms. Trees

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

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

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

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

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

Trees. Trees. CSE 2011 Winter 2007

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

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

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

Outline. Definitions Traversing trees Binary trees

Data Structures Lecture 6

In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1

What is a Tree. Trees. Tree ADT ( 6.1.2) Tree Terminology. subtree. In computer science, a tree is an abstract model

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

DATA STRUCTURES USING C

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

DataStruct 7. Trees. Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011.

Ch02 Data Structures

Ch02 Data Structures

Binary Trees, Binary Search Trees

Binary trees. Binary trees. Binary trees

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

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

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

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

Trees. CSE 373 Data Structures

Introduction to Computers and Programming. Concept Question

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

CSC148 Week 6. Larry Zhang

Topics on the Midterm

Binary Trees

CE 221 Data Structures and Algorithms

Programming II (CS300)

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

Data Structure - Binary Tree 1 -

Positions, Iterators, Tree Structures and Tree Traversals. Readings - Chapter 7.3, 7.4 and 8

March 20/2003 Jayakanth Srinivasan,

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

BBM 201 Data structures

Chapter 20: Binary Trees

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

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

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

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

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

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

TREES. Trees - Introduction

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Why Do We Need Trees?

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

Tree Data Structures CSC 221

Data Structures and Algorithms

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. Height 1

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

Trees, Binary Trees, and Binary Search Trees

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

COSC 2011 Section N. Trees: Terminology and Basic Properties

Trees. Truong Tuan Anh CSE-HCMUT

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

tree nonlinear Examples

Algorithms and Data Structures

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

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

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

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

Trees. Tree Structure Binary Tree Tree Traversals

Algorithms and Data Structures

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

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

Topic 14. The BinaryTree ADT

Lecture 26. Introduction to Trees. Trees

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

CS 206 Introduction to Computer Science II

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

CSI33 Data Structures

CMSC th Lecture: Graph Theory: Trees.

Trees Algorhyme by Radia Perlman

A Hierarchical Structure. Lecture11: Tree I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

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

Tree Traversal. Lecture12: Tree II. Preorder Traversal

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

Binary Trees. Examples:

Principles of Computer Science

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

Data Structures and Algorithms for Engineers

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

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

Garbage Collection: recycling unused memory

CMPT 225. Binary Search Trees


LECTURE 13 BINARY TREES

Transcription:

Trees and Binary Trees Become Rich Force Others to be Poor Rob Banks Stock Fraud The class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or ownership of the lecture notes.

Nature View of a Tree leaves branches root

Computer Scientist s View root leaves branches nodes

What is a Tree A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. consists of nodes with a parent-child relation. Applications: Organization charts File systems Programming environments US Computers R Us Sales Manufacturing International Laptops Desktops R&D Europe Asia Canada

Tree Terminology Root: node without parent (A) Siblings: nodes share the same parent Internal node: node with at least one child (A, B, C, F) External node (leaf ): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, etc. Descendant of a node: child, grandchild, grand-grandchild, etc. Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Degree of a node: the number of its children Degree of a tree: the maximum number of its node. E Subtree: tree consisting of a node and its descendants B F A G C H D I J K subtree

Tree Properties D H B E G A I F C Property Number of nodes Height Root Node Leaves Interior nodes Ancestors of H Descendants of B Siblings of E Right subtree of A Degree of this tree Value

Tree ADT We use positions to abstract nodes Generic methods: integer size() boolean isempty() objectiterator elements() positioniterator positions() Accessor methods: position root() position parent(p) positioniterator children(p) Query methods: boolean isinternal(p) boolean isexternal(p) boolean isroot(p) Update methods: swapelements(p, q) object replaceelement(p, o) Additional update methods may be defined by data structures implementing the Tree ADT

Intuitive Representation of Tree Node List Representation ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) The root comes first, followed by a list of links to sub-trees How many link fields are needed in such a representation? Data Link 1 Link 2 Link n

Trees Every tree node: object useful information children pointers to its children Data Data Data Data Data Data Data

A Tree Representation A node is represented by an object storing Element Parent node Sequence of children nodes B B A D F A D F C E C E

Left Child, Right Sibling Representation Left Child Data Right Sibling A B C D E F G H I J K L

Tree Traversal Two main methods: Preorder Postorder Recursive definition Preorder: visit the root traverse in preorder the children (subtrees) Postorder traverse in postorder the children (subtrees) visit the root

Preorder Traversal A traversal visits the nodes of a tree in a systematic manner In a preorder traversal, a node is visited before its descendants Application: print a structured document Algorithm preorder(v) visit(v) for each child w of v preorder (w) 1 Become Rich 2 1. Motivations 2. Methods 3. Success Stories 5 9 3 1.1 Enjoy Life 4 6 7 8 1.2 Help Poor Friends 2.1 Get a CS PhD 2.2 Start a Web Site 2.3 Acquired by Google

Postorder Traversal In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories Algorithm postorder(v) for each child w of v postorder (w) visit(v) 9 cs16/ 3 homeworks/ 7 programs/ 8 todo.txt 1K 1 2 4 5 6 h1c.doc 3K h1nc.doc 2K DDR.java 10K Stocks.java 25K Robot.java 20K

Binary Tree A binary tree is a tree with the following properties: Each internal node has at most two children (degree of two) The children of a node are an ordered pair We call the children of an internal node left child and right child Applications: arithmetic expressions decision processes searching A Alternative recursive definition: a binary tree is either a tree consisting of a single node, OR a tree whose root has an ordered pair of children, each of which is a binary tree D B E F C G H I

BinaryTree ADT The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT Additional methods: position leftchild(p) position rightchild(p) position sibling(p) Update methods may be defined by data structures implementing the BinaryTree ADT

Examples of the Binary Tree Skewed Binary Tree Complete Binary Tree A A 1 A B B 2 B C D C 3 D E F G E 5 4 H I

Differences Between A Tree and A Binary Tree The subtrees of a binary tree are ordered; those of a tree are not ordered. A A B B Are different when viewed as binary trees. Are the same when viewed as trees.

Data Structure for Binary Trees A node is represented by an object storing Element Parent node Left child node Right child node B B A D A D C E C E

Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2 (a - 1) + (3 b)) + 2 - a 1 3 b

Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision Want a fast meal? Yes How about coffee? No On expense account? Yes No Yes No Starbucks Spike s Al Forno Café Paragon

Maximum Number of Nodes in a Binary Tree The maximum number of nodes on depth i of a binary tree is 2 i, i>=0. The maximum nubmer of nodes in a binary tree of height k is 2 k+1-1, k>=0. Prove by induction. k i 0 2 i k 2 + 1-1

Full Binary Tree A full binary tree of a given height k has 2 k+1 1 nodes. Height 3 full binary tree.

Labeling Nodes In A Full Binary Tree Label the nodes 1 through 2 k+1 1. Label by levels from top to bottom. Within a level, label from left to right. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Node Number Properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Parent of node i is node i / 2, unless i = 1. Node 1 is the root and has no parent.

Node Number Properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Left child of node i is node 2i, unless 2i > n, where n is the number of nodes. If 2i > n, node i has no left child.

Node Number Properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes. If 2i+1 > n, node i has no right child.

Complete Binary Trees A labeled binary tree containing the labels 1 to n with root 1, branches leading to nodes labeled 2 and 3, branches from these leading to 4, 5 and 6, 7, respectively, and so on. A binary tree with n nodes and level k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of level k. 1 2 3 1 2 3 4 5 6 7 4 5 6 7 8 9 Complete binary tree 8 9 10 11 12 13 14 15 Full binary tree of depth 3

Binary Tree Traversals Let l, R, and r stand for moving left, visiting the node, and moving right. There are six possible combinations of traversal lrr, lrr, Rlr, Rrl, rrl, rlr Adopt convention that we traverse left before right, only 3 traversals remain lrr, lrr, Rlr inorder, postorder, preorder

Inorder Traversal In an inorder traversal a node is visited after its left subtree and before its right subtree 2 6 Algorithm inorder(v) if isinternal (v) inorder (leftchild (v)) visit(v) if isinternal (v) inorder (rightchild (v)) 8 1 4 7 9 3 5

Print Arithmetic Expressions Specialization of an inorder traversal print operand or operator when visiting node print ( before traversing left subtree print ) after traversing right subtree + Algorithm inorder (v) if isinternal (v){ print( ( ) inorder (leftchild (v))} print(v.element ()) if isinternal (v){ inorder (rightchild (v)) print ( ) )} 2 - a 1 3 b ((2 (a - 1)) + (3 b))

Evaluate Arithmetic Expressions recursive method returning the value of a subtree when visiting an internal node, combine the values of the subtrees + Algorithm evalexpr(v) if isexternal (v) return v.element () else x evalexpr(leftchild (v)) y evalexpr(rightchild (v)) operator stored at v return x y 2-5 1 3 2

Creativity: pathlength(tree) = depth(v) v tree Algorithm pathlength(v, n) Input: a tree node v and an initial value n Output: the pathlength of the tree with root v Usage: pl = pathlength(root, 0); if isexternal (v) return n return (pathlength(leftchild (v), n + 1) + pathlength(rightchild (v), n + 1) + n)

Euler Tour Traversal Generic traversal of a binary tree Includes a special cases the preorder, postorder and inorder traversals Walk around the tree and visit each node three times: on the left (preorder) from below (inorder) on the right (postorder) + L R 2 B - 3 2 5 1

Euler Tour Traversal eulertour(node v) { perform action for visiting node on the left; if v is internal then eulertour(v->left); perform action for visiting node from below; if v is internal then eulertour(v->right); perform action for visiting node on the right; }