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

Similar documents
CSE 214 Computer Science II Introduction to Tree

TREES. Trees - Introduction

Binary Trees

Programming II (CS300)

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

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

Binary Trees Fall 2018 Margaret Reid-Miller

Trees. Truong Tuan Anh CSE-HCMUT

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

Chapter 20: Binary Trees

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

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

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. CSE 373 Data Structures

Trees. Tree Structure Binary Tree Tree Traversals

CSC148 Week 6. Larry Zhang

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

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

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

March 20/2003 Jayakanth Srinivasan,

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

Programming II (CS300)

Trees. Trees. CSE 2011 Winter 2007

CSE 214 Computer Science II Heaps and Priority Queues

Principles of Computer Science

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

BBM 201 Data structures


CE 221 Data Structures and Algorithms

CS24 Week 8 Lecture 1

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

Why Do We Need Trees?

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.

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

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

Garbage Collection: recycling unused memory

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

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

Associate Professor Dr. Raed Ibraheem Hamed

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

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

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

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

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

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

Programming II (CS300)

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

Friday Four Square! 4:15PM, Outside Gates

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

Algorithms and Data Structures

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

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

Binary Trees. Height 1

Data Structures and Algorithms

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

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

Algorithms. Deleting from Red-Black Trees B-Trees

Topic 14. The BinaryTree ADT

Trees and Binary Trees

Data Structures - Binary Trees 1

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

Successor/Predecessor Rules in Binary Trees

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

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

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

COSC 2011 Section N. Trees: Terminology and Basic Properties

CSE 230 Intermediate Programming in C and C++ Recursion

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

Trees Algorhyme by Radia Perlman

CS302 - Data Structures using C++

Data Structure - Binary Tree 1 -

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

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

Introduction to Computers and Programming. Concept Question

INF2220: algorithms and data structures Series 1

Trees. Eric McCreath

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

CS 206 Introduction to Computer Science II

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

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

Binary Trees. Examples:

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

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

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

TREES Lecture 10 CS2110 Spring2014

CMPT 225. Binary Search Trees

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

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

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

Objectives. In this session, you will learn to:

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

CSI33 Data Structures

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

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

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices.

tree nonlinear Examples

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

Transcription:

CSE 230 Intermediate Programming in C and C++ Binary Tree Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu

Introduction to Tree Tree is a non-linear data structure which is a collection of data (Node) organized in hierarchical structure. In tree data structure, every individual element is called as Node. Node stores the actual data of that particular element and link to next element in hierarchical structure. Tree with 11 nodes and 10 edges Shebuti Rayana (CS, Stony Brook University) 2

Tree Terminology Root In a tree data structure, the first node is called as Root Node. Every tree must have root node. In any tree, there must be only one root node. Root node does not have any parent. (same as head in a LinkedList). Here, A is the Root node Shebuti Rayana (CS, Stony Brook University) 3

Tree Terminology Edge The connecting link between any two nodes is called an Edge. In a tree with 'N' number of nodes there will be a maximum of 'N-1' number of edges. Edge is the connecting link between the two nodes Shebuti Rayana (CS, Stony Brook University) 4

Tree Terminology Parent The node which is predecessor of any node is called as Parent Node. The node which has branch from it to any other node is called as parent node. Parent node can also be defined as "The node which has child / children". Here, A is Parent of B and C B is Parent of D, E and F C is the Parent of G and H Shebuti Rayana (CS, Stony Brook University) 5

Tree Terminology Child The node which is descendant of any node is called as CHILD Node. In a tree, any parent node can have any number of child nodes. In a tree, all the nodes except root are child nodes. Here, B and C are Children of A G and H are Children of C K is a Child of G Shebuti Rayana (CS, Stony Brook University) 6

Tree Terminology Siblings In a tree data structure, nodes which belong to same Parent are called as Siblings. In simple words, the nodes with same parent are called as Sibling nodes. Here, B and C are siblings D, E and F are siblings G and H are siblings Shebuti Rayana (CS, Stony Brook University) 7

Tree Terminology Leaf The node which does not have a child is called as Leaf Node. leaf node is also called as 'Terminal' node. Here, D, I, J, F. K and H are leaf nodes Shebuti Rayana (CS, Stony Brook University) 8

Tree Terminology Internal Nodes In a tree data structure, the node which has at least one child is called as Internal Node. Here, A, B, E, C, G are Internal Nodes Shebuti Rayana (CS, Stony Brook University) 9

Tree Terminology Degree the total number of children of a node is called as Degree of that Node. The highest degree of a node among all the nodes in a tree is called as 'Degree of Tree' Here, Degree of A is 2 Degree of B is 3 Degree of F is 0 Shebuti Rayana (CS, Stony Brook University) 10

Tree Terminology Level In a tree data structure, the root node is said to be at Level 0 and the children of root node are at Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on. In simple words, in a tree each step from top to bottom is called as a Level and the Level count starts with '0' and incremented by one at each level (Step). Shebuti Rayana (CS, Stony Brook University) 11

Tree Terminology Height the total number of edges from leaf node to a particular node in the longest path is called the Height of that Node. In a tree, height of the root node is said to be height of the tree. In a tree, height of all leaf nodes is '0'. Here, Height of the tree is 3 Shebuti Rayana (CS, Stony Brook University) 12

Tree Terminology Depth The total number of edges from root node to a particular node is called as Depth of that Node. In a tree, the total number of edges from root node to a leaf node in the longest path is said to be Depth of the tree. Here, Depth of the tree is 3 Shebuti Rayana (CS, Stony Brook University) 13

Tree Terminology Path The sequence of Nodes and Edges from one node to another node is called a Path between that two Nodes. Length of a Path is total number of nodes in that path. In below example the path A - B - E - J has length 4. Here, Path between A and J: A-B-E-J Path between C and K: C-G-K Shebuti Rayana (CS, Stony Brook University) 14

Tree Terminology Sub-tree Each child from a node forms a subtree recursively. Every child node will form a subtree on its parent node. Shebuti Rayana (CS, Stony Brook University) 15

Binary Tree Binary tree is a special type of tree data structure in which every node can have a maximum of 2 children. One is known as left child and the other is known as right child. In a binary tree, every node can have either 0 children or 1 child or 2 children but not more than 2 children. Shebuti Rayana (CS, Stony Brook University) 16

Types of Binary Tree A full binary tree is a binary tree in which each node has exactly zero or two children. A complete binary tree is a binary tree, which is completely filled, with the possible exception of the bottom level, which is filled from left to right. Full Tree Complete Tree Shebuti Rayana (CS, Stony Brook University) 17

Binary Tree (tree.h) Shebuti Rayana (CS, Stony Brook University) 18

Binary Tree Traversal A traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data structure, there is no unique traversal. We will consider several traversal algorithms with we group in the following two kinds depth-first traversal breadth-first traversal There are three different types of depth-first traversals: Pre Order traversal In Order traversal Post Order traversal Shebuti Rayana (CS, Stony Brook University) 19

Binary Tree Traversal Pre Order Traversal: Visit the root. Perform a preorder traversal of the left subtree. Perform a preorder traversal of the right subtree. Pre Order - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3 Shebuti Rayana (CS, Stony Brook University) 20

Binary Tree Traversal Preorder traversal Shebuti Rayana (CS, Stony Brook University) 21

Binary Tree Traversal In Order Traversal: Perform an in order traversal of the left subtree. Visit the root. Perform an in order traversal of the right subtree. In Order - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11 Shebuti Rayana (CS, Stony Brook University) 22

Binary Tree Traversal Inorder traversal Shebuti Rayana (CS, Stony Brook University) 23

Binary Tree Traversal Post Order Traversal: Perform a postorder traversal of the left subtree. Perform a postorder traversal of the right subtree. Visit the root. PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8 Shebuti Rayana (CS, Stony Brook University) 24

Binary Tree Traversal Postorder traversal Shebuti Rayana (CS, Stony Brook University) 25

Binary Tree Traversal There is only one kind of breadth-first traversal--the level order traversal. This traversal visits nodes by levels from top to bottom and from left to right. LevelOrder - 8, 5, 4, 9, 7, 11, 1, 12, 3, 2 Shebuti Rayana (CS, Stony Brook University) 26

Binary Tree Traversal Level order traversal Shebuti Rayana (CS, Stony Brook University) 27

Binary Tree Traversal These common traversals can be represented as a single algorithm by assuming that we visit each node three times. An Euler tour is a walk around the binary tree where each edge is treated as a wall, which you cannot cross. In this walk each node will be visited either on the left, or from the below, or on the right. The Euler tour in which When we visit nodes on the left produces a preorder traversal When we visit nodes from the below, we get an inorder traversal. And when we visit nodes on the right, we get a postorder traversal. Shebuti Rayana (CS, Stony Brook University) 28

Binary Tree Traversal Shebuti Rayana (CS, Stony Brook University) 29

Binary Tree Traversal: Example Pre Order In Order Post Order Shebuti Rayana (CS, Stony Brook University) 30

Binary Tree Traversal: Example Pre Order ABDFGCEHI In Order BFDGAEIHC Post Order FGDBIHECA Shebuti Rayana (CS, Stony Brook University) 31

Creating Binary Trees Shebuti Rayana (CS, Stony Brook University) 32

Creating Binary Trees We will use these routines as primitives to create a binary tree from data values in an array. There is a very nice mapping from the indices of a linear array into nodes of a binary tree. We do this by taking the value a[i] and letting it have as child a[2*i+1] and a[2*i+2]. Then we map a[0] into the unique root node of the resulting binary tree. Its left child will be a [1], and its right child will be a [2]. The function create_tree() embodies this mapping. The formal parameter size is the number of nodes in the binary tree. Shebuti Rayana (CS, Stony Brook University) 33

Creating Binary Trees Shebuti Rayana (CS, Stony Brook University) 34