Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Binary Trees. Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
© 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 Using C++1 Chapter 11 Binary Trees.
Binary Trees. 2 Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
BINARY TREES && TREE TRAVERSALS. DEFINITION : Binary Tree A binary tree is made of nodes Each node contains –a "left" pointer -- left child –a "right"
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
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 ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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. 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:
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 Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
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)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Chapter 12 – Data Structures
CSCE 3110 Data Structures & Algorithm Analysis
Binary Trees.
Recursive Objects (Part 4)
Binary Search Tree (BST)
Trees.
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Binary Trees.
Tree data structure.
Tree data structure.
Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Tree traversals BST properties Search Insertion
Non-Linear data structures
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C

Senem Kumova Metin Spring2009 DEFINITION : Binary Tree A binary tree is made of nodes Each node contains –a "left" pointer -- left child –a "right" pointer – right child –a data element. The "root" pointer points to the topmost node in the tree. The left and right pointers recursively point to smaller "subtrees" on either side. A null pointer represents a binary tree with no elements -- the empty tree. right childleft child root

Senem Kumova Metin Spring2009 DEFINITION : Binary Tree The size of a binary tree is the number of nodes in it –This tree has size 9 The depth of a node is its distance from the root –a is at depth zero –e is at depth 2 The depth of a binary tree is the depth of its deepest node –This tree has depth 3 a b c de f g hı

Senem Kumova Metin Spring2009 a bc d e ghi l f jk DEFINITION : Binary Tree The size of tree?? The depth of tree??

Senem Kumova Metin Spring2009 struct node { int data; struct node * left; struct node * right;}; A Typical Binary Tree Declaration

Senem Kumova Metin Spring2009 Create a binary tree EXAMPLE: Get numbers from user till -1. Insert a new node with the given number into the tree in the correct place Rule : each right node will be greater than its root and each left node will be less than its root

Senem Kumova Metin Spring2009 Create a binary tree : EXAMPLE typedef struct node * BTREE; /* CREATE A NEW NODE */ BTREE new_node(int data) { BTREE p; p=( BTREE)malloc(sizeof(struct node)); p->data=data; p->left=NULL; p->right=NULL; return p; }

Senem Kumova Metin Spring2009 Create a binary tree : EXAMPLE typedef struct node * BTREE; /* INSERT DATA TO TREE */ BTREE insert(BTREE root, int data) { if(root!=NULL) { if(data data) root->left= insert(root->left,data); else root->right=insert(root->right,data); } else {root=new_node(data);} return root; }

Senem Kumova Metin Spring2009 Create binary tree : Example main() { BTREE myroot =NULL; int i=0; scanf(“%d”,&i); while(i!=-1) { myroot=insert(myroot,i); scanf(“%d”,&i); } // INPUT VALUES

Senem Kumova Metin Spring2009 BINARY TREE TRAVERSALS 1/5 Several ways to visit nodes(elements) of a tree Inorder 1. Left subtree 2. Root 3. Right subtree Preorder 1. Root 2. Left subtree 3. Right subtree Postorder 1. Left subtree 2. Right subtree 3. Root

Senem Kumova Metin Spring2009 void inorder(BTREE root) { if(root!=NULL) { inorder(root->left); printf(“%d”,root->data); inorder(root->right); } } BINARY TREE TRAVERSALS 2/5 void preorder(BTREE root) { if(root!=NULL) {printf(“%d”,root->data); preorder(root->left); preorder(root->right); } } void postorder(BTREE root) { if(root!=NULL) {postorder(root->left); postorder(root->right); printf(“%d”,root->data); } }

Senem Kumova Metin Spring2009 void inorder(BTREE root) { if(root!=NULL) { inorder(root->left); printf(“%d”,root->data); inorder(root->right); } } // OUTPUT : BINARY TREE TRAVERSALS 3/5

Senem Kumova Metin Spring2009 void preorder(BTREE root) { if(root!=NULL) {printf(“%d”,root->data); preorder(root->left); preorder(root->right); } } // OUTPUT : BINARY TREE TRAVERSALS 4/5

Senem Kumova Metin Spring2009 void postorder(BTREE root) { if(root!=NULL) {postorder(root->left); postorder(root->right); printf(“%d”,root->data); } } // OUTPUT : BINARY TREE TRAVERSALS 5/5

Senem Kumova Metin Spring2009 FIND SIZE OF A TREE int size ( BTREE root) { if(root!=NULL) return(size(root->left) size(root->right)); else return 0; }

Senem Kumova Metin Spring2009 Max Depth of a tree int maxDepth(BTREE node) { int lDepth; int rDepth; if (node==NULL) return(0); else { // compute the depth of each subtree lDepth = maxDepth(node->left); rDepth = maxDepth(node->right); // use the larger one if (lDepth > rDepth) return(lDepth+1); else return(rDepth+1); }

Senem Kumova Metin Spring2009 Delete a node from a tree (1/5) BTREE delete_node(BTREE root,int x) // SEARCH AND DELETE x in tree 1.x> root->data  search right subtree 2.x data  search left subtree 3.root->data==x 3.1root is a leaf node  free root =free tree 3.2root has no left subtree  root=root->right 3.3root has no right subtree  root=root->left 3.4root has right and left subtree   append right subtree to left subtree

Senem Kumova Metin Spring2009 Delete a node from a tree (2/5) BTREE delete_node(BTREE root,int x) // SEARCH AND DELETE x in tree 1.x> root->data  search right subtree 2.x data  search left subtree 3.root->data==x 3.1root is a leaf node 3.2root has no left subtree  root=root->right 3.3root has no right subtree 3.4root has right and left subtree  ( 3.2nd Case)

Senem Kumova Metin Spring2009 Delete a node from a tree (3/5) BTREE delete_node(BTREE root,int x) // SEARCH AND DELETE x in tree 1.x> root->data  search right subtree 2.x data  search left subtree 3.root->data==x 3.1root is a leaf node 3.2root has no left subtree 3.3root has no right subtree  root=root->left 3.4root has right and left subtree  ( 3. 3th Case)

Senem Kumova Metin Spring2009 Delete a node from a tree (4/5) BTREE delete_node(BTREE root,int x) // SEARCH AND DELETE x in tree 1.x> root->data  search right subtree 2.x data  search left subtree 3.root->data==x 3.1root is a leaf node 3.2root has no left subtree 3.3root has no right subtree 3.4root has right and left subtree   append right subtree to left subtree ( 3.4th Case)

Senem Kumova Metin Spring2009 Delete a node from a tree (5/5) BTREE delete_node(BTREE root,int x) {BTREE p,q; if(root==NULL) return NULL; // no tree if(root->data==x) // find x in root { if(root->left==root->right) // root is a leaf node { free(root); return NULL; } else { if(root->left==NULL){ p=root->right; free(root); return p; } else if(root->right==NULL){ p=root->left; free(root); return p; } else { p=q=root->right; while(p->left!=NULL) p=p->left; p->left=root->left; free(root); return q;} } if(root->data right=delete_node(root->right,x); } else { root->left=delete_node(root->left,x); } return root; }

Senem Kumova Metin Spring2009 Search a node in a tree Search in binary trees requires O(log n) time in the average case, but needs O(n) time in the worst-case, when the unbalanced tree resembles a linked list PSEUDOCODE search_binary_tree(node, key) { if ( node is NULL) return None // key not found if (key key) return search_binary_tree(node->left, key) elseif (key > node->key) return search_binary_tree(node->right, key) else return node // found key }

Senem Kumova Metin Spring2009 eXERCISE Using the binary tree declaration below create a tree till user enters 0 for ID Ask user an ID to delete some node in tree Display initial and final tree after deletion. Ask user an ID to search some node in tree. Print out name of the node. struct node { int ID; char name[100]; struct node * left; struct node * right;};