LECTURE 13 BINARY TREES

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

DATA STRUCTURES USING C

void insert( Type const & ) void push_front( Type const & )

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 - Introduction

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

LECTURE 11 TREE TRAVERSALS

LECTURE 18 AVL TREES

Binary Trees, Binary Search Trees

CE 221 Data Structures and Algorithms

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

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

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

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

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

CSI33 Data Structures

11 TREES DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

March 20/2003 Jayakanth Srinivasan,

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

An abstract tree stores data that is hierarchically ordered. Operations that may be performed on an abstract tree include:

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

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

Chapter 20: Binary Trees

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

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

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Programming II (CS300)

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

Introduction to Computers and Programming. Concept Question

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

Data Structures and Algorithms for Engineers

Binary Trees. Height 1

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

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

Binary Trees

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

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

Trees. Trees. CSE 2011 Winter 2007

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

Garbage Collection: recycling unused memory

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

CSI33 Data Structures

12 Abstract Data Types

Trees, Binary Trees, and Binary Search Trees

Tree Data Structures CSC 221

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

Binary Trees Fall 2018 Margaret Reid-Miller

Associate Professor Dr. Raed Ibraheem Hamed

Topic Binary Trees (Non-Linear Data Structures)

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

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

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

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

An Introduction to Trees

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Binary Trees and Binary Search Trees

Trees. CSE 373 Data Structures

Computer Science II Fall 2009

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

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Partha Sarathi Mandal

Lecture 37 Section 9.4. Wed, Apr 22, 2009

COMP 250 Fall binary trees Oct. 27, 2017

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge


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

Stacks, Queues and Hierarchical Collections

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

Tree Applications. Processing sentences (computer programs or natural languages) Searchable data structures

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

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

Topic 14. The BinaryTree ADT

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION /08:30-9:50 RCH 105, RCH 110

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

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

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

CS61B Lecture #20: Trees. Last modified: Wed Oct 12 12:49: CS61B: Lecture #20 1

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

CSC148 Week 6. Larry Zhang

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization.

ITI Introduction to Computing II

CS301 - Data Structures Glossary By

Trees: examples (Family trees)

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

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

Advanced Tree Data Structures

tree nonlinear Examples

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Visit ::: Original Website For Placement Papers. ::: Data Structure

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

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

Tree traversals and binary trees

Trees. Truong Tuan Anh CSE-HCMUT

ITI Introduction to Computing II

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

Transcription:

DATA STRUCTURES AND ALGORITHMS LECTURE 13 BINARY TREES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD DEFINITION The arbitrary number of children in general trees is often unnecessary many real-life trees are restricted to two branches Expression trees using binary operators An ancestral tree of an individual, parents, grandparents, etc. Phylogenetic trees Lossless encoding algorithms There are also issues with general trees: There is no natural order between a node and its children 2 Lecture 13 - Binary Trees 1

DEFINITION A binary tree is a restriction where each node has exactly two children: Each child is either empty or another binary tree This restriction allows us to label the children as left and right subtrees We will also refer to the two sub-trees as The left-hand sub-tree, and The right-hand sub-tree 3 DEFINITION Sample variations on binary trees with five nodes: 4 Lecture 13 - Binary Trees 2

DEFINITION A full node is a node where both the left and right sub-trees are non-empty trees Legend: full nodes neither leaf nodes 5 DEFINITION An empty node or a null sub-tree is any location where a new leaf node could be appended 6 Lecture 13 - Binary Trees 3

DEFINITION A full binary tree is where each node is: A full node, or A leaf node These have applications in Expression trees Huffman encoding 7 BINARY NODE CLASS The binary node class is similar to the single node class: #include <algorithm> class Binary_node { protected: Type element; Binary_node *left_tree; Binary_node *right_tree; public: Binary_node( Type const & ); Type retrieve() const; Binary_node *left() const; Binary_node *right() const; bool empty() const; bool is_leaf() const; int size() const; int height() const; void clear(); 8 Lecture 13 - Binary Trees 4

BINARY NODE CLASS CONSTRUCTOR We will usually only construct new leaf nodes Binary_node<Type>::Binary_node( Type const &obj ): element( obj ), left_tree( nullptr ), right_tree( nullptr ) { // Empty constructor 9 BINARY NODE CLASS ACCESSORS The accessors are similar to that of Single_list Type Binary_node<Type>::retrieve() const { return element; Binary_node<Type> *Binary_node<Type>::left() const { return left_tree; Binary_node<Type> *Binary_node<Type>::right() const { return right_tree; 10 Lecture 13 - Binary Trees 5

BINARY NODE CLASS BASIC FUNCTIONALITY Much of the basic functionality is very similar to Simple_tree bool Binary_node<Type>::empty() const { return ( this == nullptr ); bool Binary_node<Type>::is_leaf() const { return!empty() && left()->empty() && right()->empty(); 11 BINARY NODE CLASS SIZE int Binary_node<Type>::size() const { return empty()? 0 : 1 + left()->size() + right()->size(); 12 Lecture 13 - Binary Trees 6

BINARY NODE CLASS HEIGHT int Binary_node<Type>::height() const { return empty()? -1 : 1 + std::max( left()->height(), right()->height() ); 13 BINARY NODE CLASS CLEAR Removing all the nodes in a tree is similarly recursive: void Binary_node<Type>::clear( Binary_node *&ptr_to_this ) { if ( empty() ) { return; left()->clear( left_node ); right()->clear( right_node ); delete this; ptr_to_this = nullptr; 14 Lecture 13 - Binary Trees 7

APPLICATION: EXPRESSION TREES Any basic mathematical expression containing binary operators may be represented using a binary tree For example, 3(4a + b + c) + d/5 + (6 e) 15 APPLICATION: EXPRESSION TREES Observations: Internal nodes store operators Leaf nodes store literals or variables No nodes have just one sub tree The order is not relevant for Addition and multiplication (commutative) Order is relevant for Subtraction and division (non-commutative) It is possible to replace non-commutative operators using the unary negation and inversion: (a/b) = a b -1 (a b) = a + ( b) 16 Lecture 13 - Binary Trees 8

APPLICATION: EXPRESSION TREES A post-order depth-first traversal converts such a tree to the reverse-polish format 3 4 a b c + + d 5 6 e + + 17 APPLICATION: EXPRESSION TREES Humans think in in-order Computers think in post-order: Both operands must be loaded into registers The operation is then called on those registers Most use in-order notation (C, C++, Java, C#, etc.) Necessary to translate in-order into post-order 18 Lecture 13 - Binary Trees 9

FULL VS COMPLETE BINARY TREE A full BT of depth k is a binary tree of depth k having 2 k -1 nodes, k>=0. A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. Full Binary Tree A Complete Binary Tree A B C B C D E F G D E F G H I H I J K J K J K 19 LABELING NODES 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 20 Lecture 13 - Binary Trees 10

NODE NUMBER PROPERTIES Parent of node i is node i / 2, unless i = 1. Node 1 is the root and has no parent. 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. 1 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. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 21 ARITHMETIC EXPRESSION USING BT inorder traversal A / B * C * D + E infix expression * + E preorder traversal + * * / A B C D E prefix expression * D postorder traversal A B / C * D * E + postfix expression A / B C level order traversal + * E * D / C A B 22 Lecture 13 - Binary Trees 11

INORDER TRAVERSAL (RECURSIVE VERSION) void inorder(ptnode ptr) /* inorder tree traversal */ { if (ptr) { inorder(ptr->left); cout<<ptr->data; indorder(ptr->right); A / B * C * D + E 10 Binary Trees Data Strutures & Algorithms Imran Ihsan - www.imranihsan.com 23 PREORDER TRAVERSAL (RECURSIVE VERSION) void preorder(ptnode ptr) /* preorder tree traversal */ { if (ptr) { cout<<ptr->data; preorder(ptr->left); predorder(ptr->right); + * * / A B C D E 10 Binary Trees Data Strutures & Algorithms Imran Ihsan - www.imranihsan.com 24 Lecture 13 - Binary Trees 12

POSTORDER TRAVERSAL (RECURSIVE VERSION) void postorder(ptnode ptr) /* postorder tree traversal */ { if (ptr) { postorder(ptr->left); postdorder(ptr->right); cout<<ptr->data; A B / C * D * E + 10 Binary Trees Data Strutures & Algorithms Imran Ihsan - www.imranihsan.com 25 LEVELORDER TRAVERSAL (USING QUEUE) void levelorder(ptnode ptr) /* level order tree traversal */ { int front = rear = 0; ptnode queue[max_queue_size]; if (!ptr) return; /* empty queue */ enqueue(front, &rear, ptr); for (;;) { ptr = dequeue(&front, rear); if (ptr) { cout<<ptr->data; if (ptr->left) enqueue(front, &rear, ptr->left); if (ptr->right) 10 Binary Trees Data Strutures & Algorithms enqueue(front, &rear, ptr->right); Imran Ihsan - www.imranihsan.com + * E * D / C A B 26 Lecture 13 - Binary Trees 13

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 10 Binary Trees Data Strutures & Algorithms Imran Ihsan - www.imranihsan.com 27 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; 10 Binary Trees Data Strutures & Algorithms Imran Ihsan - www.imranihsan.com 28 Lecture 13 - Binary Trees 14