Tree data structure.

Slides:



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

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.
1 Binary Search Trees II Chapter 6. 2 Objectives You will be able to use a binary search tree template with your own classes.
1 Introduction to Binary Trees. 2 Background All data structures examined so far are linear data structures. Each element in a linear data structure has.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Class 9: Review. cis 335 Fall 2001 Barry Cohen Big O Complexity n Complexity of problem, complexity of algorithm (Sum (n)) n Intuition: worst case rate.
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"
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
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.
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.
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.
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.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
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
Recursive Objects (Part 4)
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
Tree Traversals – reminder
Tree data structure.
Chapter 20: Binary Trees.
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Depict the Tree Structure in a picture
Tree Traversals – reminder
Chapter 21: Binary Trees.
Binary Trees.
CS212: Data Structures and Algorithms
Tree data structure.
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Trees.
Tree data structure.
Binary Trees.
Binary Trees.
Trees Definitions Implementation Traversals K-ary Trees
Binary Tree Traversal Methods
Binary Trees, Binary Search Trees
Unit - 3 Trees - Binary tress - Terminology - Representation
Chapter 20: Binary Trees.
Trees.
Tree traversals BST properties Search Insertion
Binary Tree Traversal.
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); }