Tree data structure.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
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 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.
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 ),
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.
Tree Traversal.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
 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.
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 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
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 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.
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)
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.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Binary trees יום ראשון 08 אוקטובר 2017
Chapter 12 – Data Structures
Chapter 25 Binary Search Trees
Recursive Objects (Part 4)
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
Tree Traversals – reminder
Tree data structure.
Chapter 20: Binary Trees.
Tree Traversals – reminder
Chapter 21: Binary Trees.
Tree data structure.
Binary Tree Traversal Methods
Tree data structure.
Binary Tree Traversal Methods
Abstract Data Structures
Trees.
Binary Trees.
Trees Definitions Implementation Traversals K-ary Trees
Binary Tree Traversal Methods
Chapter 9 Binary Trees.
Binary Trees: Motivation
Binary Trees, Binary Search Trees
Binary Search Trees Chapter 7 Objectives
Binary Tree Properties & Representation
Chapter 20: Binary Trees.
Trees.
Tree traversals BST properties Search Insertion
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
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

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