Tree data structure.

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 Search Trees. John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement.
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 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary trees יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015 יום חמישי 02 יולי 2015.
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.
BINARY TREES && TREE TRAVERSALS. DEFINITION : Binary Tree A binary tree is made of nodes Each node contains –a "left" pointer -- left child –a "right"
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 8 – Trees.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
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.
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 (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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Binary trees יום ראשון 08 אוקטובר 2017
Chapter 12 – Data Structures
Recursive Definition of Tree Structures
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
Binary search tree. Removing a node
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Tree.
Section 8.1 Trees.
Trees.
Data Structures & Algorithm Design
Tree Traversals – reminder
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Depict the Tree Structure in a picture
Tree Traversals – reminder
Chapter 21: Binary Trees.
Tree data structure.
Tree data structure.
Find in a linked list? first last 7  4  3  8 NULL
Tree data structure.
Binary Trees, Binary Search Trees
Unit - 3 Trees - Binary tress - Terminology - Representation
Chapter 20: Binary Trees.
Trees.
Tree traversals BST properties Search Insertion
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Binary Search Tree.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Tree data structure

Binary Search Tree - Implementation Struct Node{ int data; Node *left; Node *right; }; Nodes will be created in heap using malloc function

Binary Search Tree - Implementation Struct Node{ int data; Node *left; Node *right; }; Nodes will be created in heap using malloc function

Binary Search Tree - Implementation in C

//To search an element in BST, returns true if element is found #include<stdio.h> #include<stdlib.h> //Definition of Node for Binary search tree struct node { int data; struct node* left; struct node* right; }; // Function to create a new Node in heap struct node* GetNewNode(int data) { struct node* newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = data; newNode->left = newNode->right = NULL; return newNode; //return the address of the newly created node } // To insert data in BST, returns address of root node struct node* Insert(struct node* root,int data) { if(root == NULL) { // empty tree root = GetNewNode(data); // if data to be inserted is lesser, insert in left subtree. else if(data <= root->data) { root->left = Insert(root->left,data); // else, insert in right subtree. else { root->right = Insert(root->right,data); return root; //To search an element in BST, returns true if element is found int Search(struct node* root,int data) { if(root == NULL) { return 0; } else if(root->data == data) { return 1; else if(data <= root->data) { return Search(root->left,data); else { return Search(root->right,data); int main() { struct node* root = NULL; // Creating an empty tree /*Evaluating the logic of the code*/ root = Insert(root,15); root = Insert(root,10); root = Insert(root,20); root = Insert(root,25); root = Insert(root,8); root = Insert(root,12); // Ask user to enter a number. int number; printf("Enter number be searched\n"); scanf("%d",&number); //If number is found, print "FOUND" if(Search(root,number) == 1) printf("Found\n"); else printf("Not Found\n");

Binary Tree Traversal Array Linked List Linear Data Structure Tree traversal is the process of visiting each node in the tree exactly once in some order Visit Reading/Processing data in a node Head

Binary Tree Traversal Three Common Tree Traversals Preorder Inorder Postorder

Tree Traversal Breadth First Depth First Breadth-First: P, D, J, B, E, G, K, A, C, I, Y Depth-First: 3 Strategies Preorder (Root, Left-Subtree, Right-Subtree): P, D, B, A, C, E, J, G, I, Y, K Inorder (Left-subtree, Root, Right-subtree): A, B, C, D, E, P, G, Y, I, J, K Postorder (Left-subtree, Right-subtree, Root): A, C, B, E, D, Y, I, G, K, J, P

Preorder Binary Tree Traversal Visit the root. Traverse the left subtree, i.e., call Preorder(left-subtree) Traverse the right subtree, i.e., call Preorder(right-subtree) #include<stdio.h> #include<stdlib.h> //Definition of Node for Binary search tree struct node { int data; struct node* left; struct node* right; }; // Recursive Function to print BST in Preorder void Preorder(struct node* root) { if (root == NULL) return; /* first print data of node */ printf("%d ", root->data); /* then recur on left sutree */ Preorder(root->left); /* now recur on right subtree */ Preorder(root->right); }

Inorder Binary Tree Traversal Traverse the left subtree, i.e., call Inorder(left-subtree) Visit the root. Traverse the right subtree, i.e., call Inorder(right-subtree) #include<stdio.h> #include<stdlib.h> //Definition of Node for Binary search tree struct node { int data; struct node* left; struct node* right; }; // Recursive Function to print BST in Inorder void Inorder(struct node* root) { if (root == NULL) return; /* first recur on left child */ Inorder(root->left); /* then print the data of node */ printf("%d ", root->data); /* now recur on right child */ Inorder(root->right); }

Postorder Binary Tree Traversal Traverse the left subtree, i.e., call Postorder(left-subtree) Traverse the right subtree, i.e., call Postorder(right-subtree) Visit the root. #include<stdio.h> #include<stdlib.h> //Definition of Node for Binary search tree struct node { int data; struct node* left; struct node* right; }; // Recursive Function to print BST in Postorder void Postorder(struct node* root) { if (root == NULL) return; // first recur on left subtree Postorder(root->left); // then recur on right subtree Postorder(root->right); // now deal with the node printf("%d ", root->data); }

Delete a node from BST

else if(root->left == NULL) { struct Node *temp = root; /* Deleting a node from Binary search tree */ #include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node *left; struct Node *right; }; //Function to find minimum in a tree. Node* FindMin(Node* root) { while(root->left != NULL) root = root->left; return root; } // Function to search a delete a value from tree. struct Node* Delete(struct Node *root, int data) { if(root == NULL) return root; else if(data < root->data) root->left = Delete(root->left,data); else if (data > root->data) root->right = Delete(root->right,data); // Yippe... I found you, Get ready to be deleted else { // Case 1: No child if(root->left == NULL && root->right == NULL) { free (root); root = NULL; //Case 2: One child else if(root->left == NULL) { struct Node *temp = root; root = root->right; free (temp); } else if(root->right == NULL) { root = root->left; // case 3: 2 children else { struct Node *temp = FindMin(root->right); root->data = temp->data; root->right = Delete(root->right,temp->data); return root; //Function to visit nodes in Inorder void Inorder(Node *root) { if(root == NULL) return; Inorder(root->left); //Visit left subtree printf("%d ",root->data); //Print data Inorder(root->right); // Visit right subtree // Function to Insert Node in a Binary Search Tree Node* Insert(Node *root,char data) { if(root == NULL) { root = new Node(); root->data = data; root->left = root->right = NULL; else if(data <= root->data) root->left = Insert(root->left,data); else root->right = Insert(root->right,data);

int main() { /*Code To Test the logic Creating an example tree 5 / \ 3 10 / \ \ 1 4 11 */ Node* root = NULL; root = Insert(root,5); root = Insert(root,10); root = Insert(root,3); root = Insert(root,4); root = Insert(root,1); root = Insert(root,11); // Deleting node with value 5, change this value to test other cases root = Delete(root,5); //Print Nodes in Inorder printf("Inorder: "); Inorder(root); printf("\n"); }

free (root); root = NULL; //Case 2: One child /* Deleting a node from Binary search tree */ #include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node *left; struct Node *right; }; //Function to find minimum in a tree. Node* FindMin(Node* root) { while(root->left != NULL) root = root->left; return root; } // Function to search a delete a value from tree. struct Node* Delete(struct Node *root, int data) { if(root == NULL) return root; else if(data < root->data) root->left = Delete(root->left,data); //from Delete(200,15) (1) else if (data > root->data) root->right = Delete(root- >right,data); //return is 350 (6) // Yippe... I found you, Get ready to be deleted else { // Case 1: No child //(5) if(root->left == NULL && root->right == NULL) { free (root); root = NULL; //Case 2: One child else if(root->left == NULL) { struct Node *temp = root; root = root->right; free (temp); } else if(root->right == NULL) { root = root->left; // case 3: 2 children //from Delete(350,15) (2) else { struct Node *temp = FindMin(root->right); root->data = temp->data; //Replace 15 by 17 (3) root->right = Delete(root->right,temp->data); //Delete(400,17) (4) ->return is NULL through step 5 } } return root; //Function to visit nodes in Inorder void Inorder(Node *root) { if(root == NULL) return; Inorder(root->left); //Visit left subtree printf("%d ",root->data); //Print data Inorder(root->right); // Visit right subtree // Function to Insert Node in a Binary Search Tree Node* Insert(Node *root,char data) { if(root == NULL) { root = new Node(); root->data = data; root->left = root->right = NULL; else if(data <= root->data) root->left = Insert(root->left,data); else root->right = Insert(root->right,data);