Binary Trees. Height 1

Similar documents
Binary Trees, Binary Search Trees

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

Binary Trees

Chapter 20: Binary Trees

Binary Trees. Examples:

8. Binary Search Tree

Programming II (CS300)

[ DATA STRUCTURES ] Fig. (1) : A Tree

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

Principles of Computer Science

Data Structure - Binary Tree 1 -

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

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

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

TREES. Trees - Introduction

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

BBM 201 Data structures

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

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

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

CE 221 Data Structures and Algorithms

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

March 20/2003 Jayakanth Srinivasan,

Trees. Truong Tuan Anh CSE-HCMUT

Module 04 Trees Contents

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

Topic Binary Trees (Non-Linear Data Structures)

DATA STRUCTURES USING C

Trees, Binary Trees, and Binary Search Trees

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

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

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

CSI33 Data Structures

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

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

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

Data and File Structures Laboratory

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

CHAPTER 5. Trees CHAPTER 5 1/70

Binary Trees and Binary Search Trees

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

Data Structures and Algorithms


Why Do We Need Trees?

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

Unit III - Tree TREES

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

INF2220: algorithms and data structures Series 1

Trees. Tree Structure Binary Tree Tree Traversals

Cpt S 122 Data Structures. Data Structures Trees

Data Structures And Algorithms

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

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

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

Data Structures and Algorithms

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

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

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

COSC 2011 Section N. Trees: Terminology and Basic Properties

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Introduction to Computers and Programming. Concept Question

Trees. CSE 373 Data Structures

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

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Algorithms. Deleting from Red-Black Trees B-Trees

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

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

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

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

Topic 14. The BinaryTree ADT

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

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

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

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

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Trees. A tree is a directed graph with the property

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

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

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

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

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

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

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

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

Programming II (CS300)

Objectives. In this session, you will learn to:

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

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSC148 Week 6. Larry Zhang

Chapter 5. Binary Trees

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

12 Abstract Data Types

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

CS301 - Data Structures Glossary By

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

CS24 Week 8 Lecture 1

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

Algorithms and Data Structures

Transcription:

Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3. TN for N>-0 which are children of root. Consider the following tree 100 Height 1 50 60 Height 2 70 80 40 35 30 1

Terminologies Root Node: The first node written at the top and does not have a parent Child: Node obtained from the parent. A parent node can have zero or more child nodes example 50 and 60 are children of 100 Siblings: Two or more nodes having the same parent Ex 50 and 60 are siblings of 100 But 70 is NOT a sibling of 50 Ancestors: Nodes obtained in the path from a specific node X while moving upwards towards root Ex 100 is the ancestor of 50 and 60 Descendents : Nodes in the path below the parent. ie nodes reachable from node X while moving downwards ex all nodes below 100 Left descendent : Nodes to the left sub tree of X ex 50 and 60 are left descendants of 100 Right descendent : Nodes to the right subtree of node X ex right descendents of 100 are 60,80,40,35,30 Left sub tree: All nodes that are left descendents of X forms the left sub tree ex Left sub tree of 100 is 50 and 70 Right Sub tree: All nodes that are right descendents of X is the right sub tree ex Right sub tree of 100 is 60,80,40,35,30 Parent: A node having left sub tree or right sub tree or both is a parent for left and / or right subtree ex parent of 50 and 60 is 100 2

Degree: Number of sub trees of a node ex 100 has 2 sub trees and hence degree is 2 Leaf: A node in a tree whose degree is 0 ex 70,35,30 Internal nodes: Nodes except the leaf ex 100,50,60 External nodes: They are leaf nodes Level: Distance of a node from root ex Distance of root is 0 Distance of 50 is 1 Height: Maximum level of any leaf Binary trees ex height is 4 A binary tree is a tree that has finite set of nodes that is eitherempty or consist of root and two subtrees left and right. A binary tree has Root If the tree is not empty trhen the first node is the root node Left sub tree- It is tree connected to the left of the root Right sub tree- It is the tree connected to the right of the root A tree which has zero,one,or two subtrees is a binary tree 3

ex Representation Binary trees can be represented in two ways Linked representation Array representation Linked representation Here a node has 3 fields -info contains the actual information -llink-contains the address of the left sub tree -rlink- contains the address of right subtree Hence a node can be represented as 4

struct node int info; struct node *llink; struct node *rlink; ; typedef struct node * NODE; Pictorially a node is represented as Llink info rlink A pointer variable root can be used to point to the root. Hence for a empty tree we have NODE root=null. Array representation A tree can be represented using a array. This method of representation is also called as sequential representation a b c d e f 5 g

The sequential representation of the above tree would be as follows A b c d e f g 0 1 2 3 4 5 6 Note : Nodes are numbered sequentially from 0 (root) Given the position of node I,2*I +1 gives the position of the left child and 2*i+2 gives the position of the right sub child. If I is the position of the left child then i+1 gives the position of the right child If I is the position of the right child then i-1 gives the position of left child There are two ways of representing a tree using arrays Method 1: Here some of the locations may be used and some may not be used. Hence we need to use a flag called used which will indicate whether the location is used or not. If the value is 0 then the location is not used and it indicates the absence of a node Hence we have #define MAX 200 struct node int info; int used; ; typedef struct node *NODE; A array of type NODE can be used now as 6

NODE a[max]; Method 2 Instead of using a separate flag field we initialize each location to 0 indicating that the node is not used.non zero value indicates the presence of a node. Types of binary trees We have the following types of binary trees Strictly binary tree(full binary tree) Skewed binary tree Complete binary tree Expression tree Binary search tree Level A 1 B C D 2 E F G H I J 3 K L M N 4 Binary Tree Types: Regular Binary Tree ( 2 ) 7

Skewed Left Binary Tree ( 1 ) Skewed Right Binary Tree ( 3 ) Three Graphical Pictures of the Binary Tree: A A A B B B C C C D E F G H D ( 1 ) Skewed Left Binary Tree ( 2 ) Regular Binary Tree ( 3 ) Skewed Right Binary Tree Properties of Binary Trees In particular, we want to find out the maximum number of nodes in a binary tree of depth k, and the number of leaf nodes and the number of nodes of degree two in a binary tree. Binary Tree Traversals There are many operations that we can perform on tree, but one that arises frequently is traversing a tree, that is, visiting each node in the tree exactly once. A full traversal produces a linear order for the information in a tree. Binary Tree Traversals Types Inorder Traversal (Left, Parent, Right) 8

Preorder Traversal (Parent, Left, Right) Postorder Traversal (Left, Right, Parent) Level Order Traversal (Top to Bottom, Left to Right) Example of the Binary Tree: Level A 1 B C 2 D E F G 3 H I J K L M N O 4 Binary Tree Traversals Functions Inorder Tree Traversal Recursive function: void inorder (ptr_node ptr) if (ptr) inorder (ptr->left_child); cout << ptr->data; 9

inorder (ptr->right_child); Result of binary tree example: H, D, I, B, J, E, K, A, L, F, M, C, N, G, O Preorder Tree Traversal Recursive function: void preorder (ptr_node ptr) if (ptr) cout << ptr->data; preorder (ptr->left_child); preorder (ptr->right_child); Result of binary tree example: A, B, D, H, I, E, J, K, C, F, L, M, G, N, O Postorder Tree Traversal Recursive function: 10

void postorder (ptr_node ptr) if (ptr) postorder (ptr->left_child); postorder (ptr->right_child); printf( %d, ptr->data); Result of binary tree example: H, I, D, J, K, E, B, L, M, F, N, O, G, C, A Level Order Tree Traversal Using queue: void level_order (ptr_node ptr) int front = rear = 0; ptr_node queue[max_queue_size]; if (!ptr) // empty tree; return; addq(front, &rear, ptr); for ( ; ; ) ptr = deleteq (&front, rear); 11

if (ptr) cout << ptr->data; if (ptr->left_child) addq (front, &rear, ptr->left_child); if (ptr->right_child) addq (front, &rear, ptr->right_child); else Break; Result of binary tree example: A, B, C, D, E, F, G, H, I, J, K, L, M, N, O Binary Search Tree Definition: A binary search tree is a binary tree. It may be empty. If it is not empty, it satisfies the following properties: (1) Every element has a key, and no two elements have the same key, that is, the keys are unique. (2) The keys in a nonempty left subtree must be smaller than the key in the root of the subtree. (3) The keys in a nonempty right subtree must be larger than the key in the root of the subtree. (4) The left and right subtrees are also binary search trees. Example of the Binary Search Tree: 12

20 15 25 12 17 22 30 Searching A Binary Search Tree Suppose we wish to search for an element with a key. We begin at the root. If the root is NULL, the search tree contains no elements and the search is unsuccessful. Otherwise, we compare key with the key value in root. If key equals root s key value, then the search terminates successfully. If key is less than root s key value, then no elements in the right subtree subtree can have a key value equal to key. Therefore, we search the left subtree of root. If key is larger than root s key value, we search the right subtree of root. Recursive Function for Binary Search Tree: tree_ptr search ( tree_ptr root, int key ) if (!=root ) return NULL; if ( key = = root->data ) return root; if ( key < root->data ) return search ( root->left_child, key ); return search ( root->right_child, key ); 13

Inserting Into A Binary Search Tree To insert a new element, key, we must first verify that the key is different from those of existing elements. To do this we search the tree. If the search is unsuccessful, then we insert the element at the point the search terminated. Insert Function: void insert_node ( tree_ptr *node, int num ) tree_ptr ptr, temp = modified_search ( *node, num ); // ** if ( temp! ( *node ) ) ptr = new node; if ( ptr = = NULL) cout << The memory is full \n ; exit ( 1 ); ptr->data = num; ptr->left_child = ptr->right_child = NULL; if ( *node ) if ( num < temp->data ) temp->left_child = ptr; 14

else temp->right_child = ptr; else *node = ptr; Deletion from a Binary Search Tree Deletion of a leaf node is easy. For example, if a leaf node is left child, we set the left child field of its parent to NULL and free the node. The deletion of a nonleaf node that has only a single child is also easy. We erase the node and then place the single child in the place of the erased node. When we delete a nonleaf node with two children, we replace the node with either the largest element in its left subtree or the smallest elements in its right subtree. Then we proceed by deleting this replacing element from the subtree from which it was taken. THREADED BINARY TREE A Threaded Binary Tree is a binary tree in which every node that does not have a right child has a THREAD (in actual sense, a link) to its INORDER successor. By doing this threading we avoid the recursive method of traversing a Tree, which makes use of stacks and consumes a lot of memory and time. The node structure for a threaded binary tree varies a bit and its like this -- struct NODE struct NODE *leftchild; int node_value; struct NODE *rightchild; struct NODE *thread; Let's make the Threaded Binary tree out of a normal binary tree... 15

The INORDER traversal for the above tree is -- D B A E C. So, the respective Threaded Binary tree will be -- B has no right child and its inorder successor is A and so a thread has been made in between them. Similarly, for D and E. C has no right child but it has no inorder successor even, so it has a hanging thread A Threaded Binary Tree is a binary tree in which every node that does not have a right child has a THREAD (in actual sense, a link) to its INORDER successor. By doing this threading we avoid the recursive method of traversing a Tree, which makes use of stacks and consumes a lot of memory and time. The node structure for a threaded binary tree varies a bit and its like this -- struct NODE struct NODE *leftchild; int node_value; struct NODE *rightchild; struct NODE *thread; 16

Tree traversal In computer science, tree traversal refers to the process of visiting (examining and/or updating) each node in a tree data structure, exactly once, in a systematic way. Such traversals are classified by the order in which the nodes are visited. The following algorithms are described for a binary tree, but they may be generalized to other trees as well. Depth-first traversal To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node: 1. Visit the root. 2. Traverse the left subtree. 3. Traverse the right subtree. To traverse a non-empty binary tree in inorder (symmetric), perform the following operations recursively at each node: 1. Traverse the left subtree. 2. Visit the root. 3. Traverse the right subtree. To traverse a non-empty binary tree in postorder, perform the following operations recursively at each node: 1. Traverse the left subtree. 2. Traverse the right subtree. 3. Visit the root. 17

Breadth-first traversal Trees can also be traversed in level-order, where we visit every node on a level before going to a lower level. Depth-first Preorder traversal sequence: F, B, A, D, C, E, G, I, H (root, left, right) Inorder traversal sequence: A, B, C, D, E, F, G, H, I (left, root, right); note how this produces a sorted sequence Postorder traversal sequence: A, C, E, D, B, H, I, G, F (left, right, root) Breadth-first Level-order traversal sequence: F, B, G, A, D, I, C, E, H Algebraic expressions such as 18

a/b+(c-d) have an inherent tree-like structure. For example, Figure is a representation of the expression in Equation. This kind of tree is called an expression tree. The terminal nodes (leaves) of an expression tree are the variables or constants in the expression (a, b, c, d, and e). The non-terminal nodes of an expression tree are the operators (+, -,, and ). Notice that the parentheses which appear in Equation do not appear in the tree. Nevertheless, the tree representation has captured the intent of the parentheses since the subtraction is lower in the tree than the multiplication.. The common algebraic operators are either unary or binary. For example, addition, subtraction, multiplication, and division are all binary operations and negation is a unary operation. Therefore, the non-terminal nodes of the corresponding expression trees have either one or two non-empty subtrees. That is, expression trees are usually binary trees. What can we do with an expression tree? Perhaps the simplest thing to do is to print the expression represented by the tree. Notice that an inorder traversal of the tree visits the nodes in the order A, /,b,+,c,-,d,e Except for the missing parentheses, this is precisely the order in which the symbols appear in Equation. This suggests that an inorder traversal should be used to print the expression. Consider an inorder traversal which, when it encounters a terminal node simply prints it out; and when it encounters a non-terminal node, does the following: 19

1. Print a left parenthesis; and then 2. traverse the left subtree; and then 3. print the root; and then 4. traverse the right subtree; and then 5. print a right parenthesis. Application of trees One reason to use trees might be because you want to store information that naturally forms a hierarchy. For example, the file system on a computer: file system / <-- root / \... home / \ ugrad course / / \... cs101 cs112 cs113 2) If we organize keys in form of a tree (with some ordering e.g., BST), we can search for a given key in moderate time (quicker than Linked List and slower than arrays ) 3) We can insert/delete keys in moderate time (quicker than Arrays and slower than Unordered Linked Lists). 4) Like Linked Lists and unlike Arrays, Pointer implementation of trees don t have an upper limit on number of nodes as nodes are linked using pointers. The following are the common uses of tree. 1. Manipulate hierarchical data. 2. Make information easy to search (see tree traversal). 3. Manipulate sorted lists of data. 4. As a workflow for compositing digital images for visual effects. 5. Router algorithms 20

. 21

22

23