Tree Binary Tree. ARRAYS 1. Good Data structure for Searching algorithms. 2. Disadvantage : Insertion and Deletion of Elements require Data movements(Time.

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Department of Computer Science University of Maryland, College Park
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Binary Trees Chapter 6.
Binary Trees. Node structure Data A data field and two pointers, left and right.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Tree.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
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.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
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.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
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.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
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.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Non Linear Data Structure
Trees Chapter 15.
Binary Search Tree Chapter 10.
Section 8.1 Trees.
Data Structures & Algorithm Design
Tonga Institute of Higher Education
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Introduction to Trees IT12112 Lecture 05.
Chapter 21: Binary Trees.
Trees and Binary Trees.
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Tree Binary Tree

ARRAYS 1. Good Data structure for Searching algorithms. 2. Disadvantage : Insertion and Deletion of Elements require Data movements(Time Consuming) LINKED LIST 1. It allows quick insertion and deletion of elements. 2. Searching is difficult. A Sequential traversal from the beginning of the list is required to search for an element until it is found or up to the end of the list Arrays and Linked List

What is a Tree?  A tree combines the advantages of an array and a linked list.  (Ie ) We can search quickly as well as Insert and Delete Elements quickly Trees are Non Linear Data Structures ( They are not Sequential as an array,stack etc….) Definition A Tree is a data structure that represents hierarchical relationships between individual data items

Tree DAD SON DAUGHTER SON GS1GS2GS3 Root (Which has no parents) Siblings / children ( which has parents) Branch/ edge ( Connectors between nodes) Leaf Node / External ( No children) Path : Sequence of Consecutive Edges Height : One More than the Last Level Level 0 Level 1 Level 2 Tree with n nodes will have n-1 edges

Binary Tree Definition: It is a tree in which every node has two children ( left child and right child) A B E D C F G HI

Binary Tree Node is defined as follows: struct node { int data; //Data field node *left; //A reference to the node’s left child. node *right; //A reference to the node’s right child. } Traversing : Means visiting all the nodes of the tree, in some specific order for processing. There are THREE ways of Traversing a Tree 1. PREODER TRAVERSAL 2. INORDER TRAVERSAL 3. POSTORDER TRAVERSAL

Traversing PreOrder Traversal : (VLR) Visit the current node.  Traverse the left sub-tree of the current node.  Traverse the right sub-tree of the current node. void preOrder(node *n) { if (n != NULL){ cout data<<endl;//Display the current node preOrder(n->left); //Traverse left sub-tree preOrder(n->right); //Traverse right sub-tree }} + * C / E F BA - +-AB*C/EF preOrder(root);

Traversing InOrder Traversal : (LVR)  Traverse the left sub-tree of the current node.  Visit the current node.  Traverse the right sub-tree of the current node. void preOrder(node *n) { if (n != NULL){ InOrder(n->left); //Traverse left sub-tree cout data<<endl;//Display the current node InOrder(n->right); //Traverse right sub-tree }} + * C / E F BA - A - B + C* E / F preOrder(root);

Traversing PostOrder Traversal : (LRV)  Traverse the left sub-tree of the current node.  Traverse the right sub-tree of the current node.  Visit the current node. void postOrder(node *n) { if (n != NULL){ postOrder(n->left); //Traverse left sub-tree postOrder(n->right); //Traverse right sub-tree cout data<<endl;//Display the current node }} + * C / E F BA - A B - C E F / * + preOrder(root);

Application A binary tree can be used to represent an expression –that involves only binary arithmetic operators +, -, /, and *. –The root node holds an operator, and each of its sub-trees represents either a variable (operand), or another expression. A*(B+C) * A+ BC Traversing : inorder :A*B+C preorder :*A+BC ( prefix ) postorder :ABC+*, (postfix).

BINARY SEARCH TREE A binary search tree is a 1) binary tree, 2) with the additional condition that if a node contains a value k, then every node in its left sub-tree contains a value less than k, and every node in its right sub-tree contains a value greater than or equal to k. An important consequence of the above property is that an inorder traversal on a binary tree will always visit the vertices of the tree in ascending order. The important operations on a binary search tree are: 1.Searching for a value 2.Inserting a node 3.Deleting a node 4.Finding the minimum or maximum of values stored in the tree.

Definition INORDER SUCCESSOR: If a node has a right sub-tree, its inorder successor is the last left node on this right sub-tree. The node containing the value 40 is called the inorder successor of the node containing the value 35. It is useful when deleting a node

SEARCHING in Binary Tree  Start from ROOT NODE.  Compare the Search Value with the data in the NODE. Possibilities: a.If “=“ Value has been Found/ Search is terminated. b.If search key “>” than the data of the current node, the search proceeds with the right child as the new current node. c.If the search key is ”<“ than the data of the current node, the search proceeds with the left child as the new current node. d.If the current node is NULL, the search is terminated as unsuccessful.

Algorithm void search(int val,node *root) { node *s = root; //start search at root int flag=1; while ((s->data != val)&&(flag)){ //examine current data if(val data) //go to left child s = s->left; else //go to right child s = s->right; if(s == NULL) flag=0; //no node! } if (flag) cout<<"found!"; else cout<<"not found";}

INSERTING A NEW NODE  Search Until “NULL” is encountered.  Insert the NEW NODE and connected to its parent. void Insert(int val,node *root) { node *n,*parent,*s; n = new node; //create a new node; n->data = val; //store value. n->left=NULL; n->right=NULL; s = root; //start search at root int flag=1; while(flag){ //tree traversal parent = s; if(val data){ //go to left child s = s->left; if(s == NULL){ //if null, insert new node parent->left = n; flag=0; } // end 2 nd if } // end 1 st if else { //go to right child s = s->right; if(s == NULL){ //if null, insert new node parent->right = n; flag=0; } } //end else } //end while } //end insert

DELETING A NODE Deleting a node is the most complicated operation required for binary search tree. There are three cases to consider: a.The node to be deleted is a leaf (has no children). b.The node to be deleted has one child. c.The node to be deleted has two children. 5 4null6 3

To delete a node with two children

Algorithm void Remove(int val,node *root) { node *s=root,*temp=s,*n,*pn; while((s->data!=val)&&(s!=NULL)) //LOCATE THE NODE { temp=s; if (val data) s=s->left; else s=s->right; } if (s!=NULL)//AFTER LOCATING {if ((s->left==NULL)&&(s-> right== NULL )) //LEAF NODE {if (temp->left->data==val) temp->left=NULL; //TEMP - PARENT else temp->right=NULL; delete s; }

Algorithm if ((s->left==NULL) || (s->right==NULL)) //the node to be deleted has //only one child {if (temp->left->data==val) {if (s->right!=NULL) temp->left=s->right; else temp->left=s->left; } else {if (s->right!=NULL) temp->right=s->right; else temp->right=s->left; } if ((s->left!=NULL)&&(s->right!=NULL)) {// find the inorder successor of node s 5 4null6 3

Algorithm n=s->right; while(n->left!=NULL) // FIND THE INORDER SUCCESSOR { pn=n; n=n->left; } s->data=n->data; // replace the data of the node to be deleted with the data of its in-order successor if (n->right!=NULL) //PUT THE RIGHT CHILD VALUE IN INORDER PLACE {n->data=n->right->data; delete n->right; n->right=NULL; } else { pn->left=NULL; delete n; } // IF NO RIGHT CHILD THEN PARENT LINK IS NULL } }else cout<<"element doesn't exist"<<endl; // COULD NOT FIND THE NODE }

int Maximum(node *root) { node *s = root; node * last; while (s != NULL) {last = s; s = s->right; } return last->data; } int Minimum (node *root) { node *s = root; node * last; while (s != NULL) {last = s; s = s->left; } return last->data; } COMPUTING THE MINIMUM AND MAXIMUM VALUES