Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Tree B G E D I H F c A Binary tree
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Data Structures and Algorithms Session 13 Ver. 1.0 Objectives In this session, you will learn to: Store data in a tree Implement a binary tree Implement.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Tree Data Structures.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Trees Namiq Sultan. Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
5 -1 Chapter 5 Trees Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Trees (Unit 7).
1 CSC 211 Data Structures Lecture 25 Dr. Iftikhar Azim Niaz 1.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Trees.
Chapter 12 – Data Structures
Binary Trees.
Binary Trees and Binary Search Trees
Data Structures Binary Trees 1.
Binary Search Tree (BST)
Data Structures & Algorithm Design
Chapter 20: Binary Trees.
Introduction to Trees IT12112 Lecture 05.
Chapter 21: Binary Trees.
Binary Trees.
Binary Trees.
Trees.
Binary Trees.
Chapter 20: Binary Trees.
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Trees EENG212 Algorithms and Data Structures

Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node Representation of Binary Trees  Primitive Functions in Binary Trees

Introduction to Trees BINARY TREES: BASIC DEFINITIONS A binary tree is a finite set of elements that are either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the root of the tree. The other two subsets are themselves binary trees called the left and right subtrees of the original tree. A left or right subtree can be empty. Each element of a binary tree is called a node of the tree. The following figure shows a binary tree with 9 nodes where A is the root.

BINARY TREES: BASIC DEFINITIONS root left subtree right subtree

BINARY TREES: BASIC DEFINITIONS If A is the root of a binary tree and B is the root of its left or right subtrees, then A is said to be the father of B and B is said to be the left son of A. A node that has no sons is called the leaf. Node n1 is the ancestor of node n2 if n1 is either the father of n2 or the father of some ancestor of n2. In such a case n2 is a descendant of n1. Two nodes are brothers if they are left and right sons of the same father.

BINARY TREES: BASIC DEFINITIONS leaves left son right son

BINARY TREES: BASIC DEFINITIONS If every nonleaf node in a binary tree has nonempty left and right subtrees, the tree is called a strictly binary tree.

BINARY TREES: BASIC DEFINITIONS The level of a node in a binary tree is defined as follows: The root of the tree has level 0, and the level of any other node in the tree is one more than the level of its father. The depth of a binary tree is the maximum level of any leaf in the tree. A complete binary tree of depth d is the strictly binary all of whose leaves are at level d. A complete binary tree with depth d has 2d leaves and 2d-1 nonleaf nodes.

BINARY TREES: BASIC DEFINITIONS

TRAVERSING BINARY TREES One of the common operations of a binary tree is to traverse the tree. Traversing a tree is to pass through all of its nodes once. You may want to print the contents of each node or to process the contents of the nodes. In either case each node of the tree is visited. There are three main traversal methods where traversing a binary tree involves visiting the root and traversing its left and right subtrees. The only difference among these three methods is the order in which these three operations are performed.

TRAVERSING BINARY TREES Traversing a binary tree in preorder (depth-first order) 1. Visit the root. 2. Traverse the left subtree in preorder. 3. Traverse the right subtree in preorder.

Traversing a binary tree in preorder Preorder: ABDGCEHIF

TRAVERSING BINARY TREES Traversing a binary tree in inorder (or symmetric order) 1. Traverse the left subtree in inorder. 2. Visit the root. 3. Traverse the right subtree in inorder.

Traversing a binary tree in inorder Inorder: DGBAHEICF

TRAVERSING BINARY TREES Traversing a binary tree in postorder 1. Traverse the left subtree in postorder. 2. Traverse the right subtree in postorder. 3. Visit the root.

Traversing a binary tree in postorder Postorder: GDBHIEFCA

NODE REPRESENTATION OF BINARY TREES Each node in a binary tree contains info, left, right and father fields. The left, right and father fields points the node’s left son, right son and the father respectively. struct node{ int info; /* can be of different type*/ struct node *left; struct node *right; struct node *father; }; typedef struct node *NODEPTR;

PRIMITIVE FUNCTIONS IN BINARY TREES The maketree function allocates a node and sets it as the root of a single node binary tree. NODEPTR maketree(int x) { NODEPTR p; p = getnode(); p->info = x; p->left = NULL; p->right = NULL; return p; }

PRIMITIVE FUNCTIONS IN BINARY TREES The setleft and setright functions sets a node with content x as the left son and right son of the node p respectively. void setleft(NODEPTR p, int x) { if(p == NULL){ printf(“void insertion\n”); else if (p->left != NULL) printf(“invalid insertion\n”); else p->left = maketree(x); } void setright(NODEPTR p, int x) { if(p == NULL){ printf(“void insertion\n”); else if (p->right != NULL) printf(“invalid insertion\n”); else p->right = maketree(x); }

BINARY TREE TRAVERSAL METHODS Recursive functions can be used to perform traversal on a given binary tree. Assume that dynamic node representation is used for a given binary tree. In the following traversal methods, the tree is traversed always in downward directions. Therefore the father field is not needed. The following recursive preorder traversal function displays the info part of the nodes in preorder. Note that the info part is integer number and tree is a pointer to the root of the tree.

BINARY TREE TRAVERSAL METHODS void pretrav(NODEPTR tree) { if(tree != NULL){ printf(“%d\n”, tree->info); pretrav(tree->left); pretrav(tree->right); }

BINARY TREE TRAVERSAL METHODS The following recursive inorder traversal function displays the info part of the nodes in inorder. Note that the info part is integer number and tree is a pointer to the root of the tree.

BINARY TREE TRAVERSAL METHODS void intrav(NODEPTR tree) { if(tree != NULL){ intrav(tree->left); printf(“%d\n”, tree->info); intrav(tree->right); }

BINARY TREE TRAVERSAL METHODS The following recursive postorder traversal function displays the info part of the nodes in postorder. Note that the info part is integer number and tree is a pointer to the root of the tree.

BINARY TREE TRAVERSAL METHODS void posttrav(NODEPTR tree) { if(tree != NULL){ posttrav(tree->left); posttrav(tree->right); printf(“%d\n”, tree->info); }

BINARY SEARCH TREE: AN APPLICATION OF BINARY TREES A binary tree, that has the property that all elements in the left subtree of a node n are less than the contents of n, and all elements in the right subtree of n are greater than or equal to the contents of n, is called a Binary Search Tree or Ordered Binary Tree.

BINARY SEARCH TREE: AN APPLICATION OF BINARY TREES Given the following sequence of numbers, 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 The following binary search tree can be constructed.

BINARY SEARCH TREE: AN APPLICATION OF BINARY TREES

The inorder (left-root-right) traversal of the above Binary Search Tree and printing the info part of the nodes gives the sorted sequence in ascending order. Therefore, the Binary search tree approach can easily be used to sort a given array of numbers. The inorder traversal on the above Binary Search Tree is: 3, 4, 4, 5, 5, 7, 9, 9, 14, 14, 15, 16, 17, 18, 20

SEARCHING THROUGH THE BINARY SEARCH TREE Searching operation of the binary search tree is always in downward direction. Therefore the following node structure can be used to represent the node of a given binary search tree. Note that the father link is not required.

SEARCHING THROUGH THE BINARY SEARCH TREE struct node{ int info; /* can be of different type*/ struct node *left; struct node *right; }; typedef struct node *NODEPTR;

SEARCHING THROUGH THE BINARY SEARCH TREE The following recursive function can be used to search for a given key element in a given array of integers. The array elements are stored in a binary search tree. Note that the function returns TRUE (1) if the searched key is a member of the array and FALSE (0) if the searched key is not a member of the array.

SEARCHING THROUGH THE BINARY SEARCH TREE int BinSearch(NODEPTR p, int key) { if(p == NULL) return FALSE; else { if (key == p->info) return TRUE; else{ if(key info) return BinSearch(p->left, key); else return BinSearch(p->right, key); }

INSERTING NODES INTO A BINARY SEARCH TREE The following recursive function can be used to insert a new node into a given binary search tree.

NODEPTR insert(NODEPTR p, int x) { if(p == NULL){ p = getnode(); p->info = x; p->left = NULL; p->right = NULL; return p; } else{ if(x info) p->left = insert(p->left, x); else p->right = insert(p->right, x); return p; }

Application of Binary Search Tree Suppose that we wanted to find all duplicates in a list of numbers. One way of doing this to compare each number with all those that precede it. However this involves a large number of comparison. The number of comparison can be reduced by using a binary tree. The first number in the list is placed in a node that is the root of the binary tree with empty left and right sub-trees. The other numbers in the list is than compared to the number in the root. If it is matches, we have duplicate. If it is smaller, we examine the left sub-tree; if it is larger we examine the right sub- tree. If the sub-tree is empty, the number is not a duplicate and is placed into a new node at that position in the tree. If the sub-tree is nonempty, we compare the number to the contents of the root of the sub-tree and the entire process is repeated with the sub- tree. A program for doing this follows.

#include struct node { struct node *left ; int info ; struct node *right; }; typedef struct node *NODEPTR; NODEPTR maketree(int); NODEPTR getnode(void); void intrav(NODEPTR); void main() { int number; NODEPTR root, p, q; printf("%s\n","Enter First number"); scanf("%d",&number); root=maketree(number); /* insert first root item */ printf("%s\n","Enter the other numbers");

while(scanf("%d",&number) !=EOF) { p=q=root; /* find insertion point */ while((number !=p->info) && q!=NULL) {p=q; if (number info) q = p->left; else q = p->right; } q=maketree(number); /* insertion */ if (number==p->info) printf("%d is a duplicate \n",number); else if (number info) p->left=q; else p->right=q; } printf("Tree Created \n "); /* inorder Traversing */ intrav(root); }

void intrav(NODEPTR tree) { if(tree != NULL){ intrav(tree->left); printf(“%d\n”, tree->info); intrav(tree->right); } NODEPTR maketree(int x) { NODEPTR p; p = getnode(); p->info = x; p->left = NULL; p->right = NULL; return p; } NODEPTR getnode(void) { NODEPTR p; p=(NODEPTR) malloc(sizeof(struct node)); return p; }