ECE 103 Engineering Programming Chapter 64 Tree Implementation

Slides:



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

Comp 245 Data Structures Trees. Introduction to the Tree ADT A tree is a non-linear structure. A treenode can point to 0 to N other nodes. There is one.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
Computer Science C++ High School Level By Guillermo Moreno.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Starting Out with C++, 3 rd Edition 1 Chapter 20 Binary Trees.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
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.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
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.
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.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
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.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
CS261 – Recitation 5 Fall Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1.
1 CS 163 Data Structures Chapter 9 Building, Printing Binary Trees Herbert G. Mayer, PSU Status 5/21/2015.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Programming Practice 3 - Dynamic Data Structure
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
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 CSE 2341 Object Oriented Programming with C++ Note Set #21.
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.
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Binary Search Trees. 2 Overview Recursive linked list ops Binary Trees.
Binary Search Trees Manolis Koubarakis Data Structures and Programming Techniques 1.
Chapter 12 – Data Structures
CS 201 Data Structures and Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Lectures linked lists Chapter 6 of textbook
Lecture 22 Binary Search Trees Chapter 10 of textbook
External Methods Chapter 15 (continued)
Chapter 16 Tree Implementations
Tree data structure.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Tree data structure.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Chapter 16 Tree Implementations
Review & Lab assignments
ECE 103 Engineering Programming Chapter 32 Array Parameters
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 37 C Macro Parameters
ECE 103 Engineering Programming Chapter 62 Stack Implementation
CSE 1002 Fundamentals of Software Development 2 More Linked Structures
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
ECE 103 Engineering Programming Chapter 63 Queue Implementation
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
ECE 103 Engineering Programming Chapter 18 Iteration
Chapter 20: Binary Trees.
Tree traversals BST properties Search Insertion
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

ECE 103 Engineering Programming Chapter 64 Tree Implementation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

Syllabus Pointer Version

Tree : Pointer Version A binary sort tree is created using pointers to nodes. Nodes are created at runtime using malloc(). 2

Define structure of tree element struct tree_node { int value; /* Node value */ struct tree_node *left; /* Pointer to left child node */ struct tree_node *right; /* Pointer to right child node */ }; typedef struct tree_node Node; 3

Binary sort tree 4 tree value left right value left … right value left

Create the tree 5 int main (void) { Node *tree = NULL; /* Pointer to tree */ int rvalue; /* Random value */ int k; /* Loop index */ srand(time(NULL)); /* Initialize random seed */ /* Build the tree with randomly generated values */ printf("Original values: "); for (k = 0; k < 10; k++) rvalue = rand() % 100 + 1; /* 1 to 100 */ printf("%d ", rvalue); insert_node(&tree, rvalue); } /* Show sorted list of values */ printf("\n\nSorted values: "); display_inorder(tree); return 0; 5

Create a new tree node 6 Node * create_node (int num) { Node *p; /* Work pointer */ /* Use run-time memory allocation to create new node */ if ((p = (Node *) malloc(sizeof(Node))) == NULL) printf("ERROR: Unable to allocate memory for node\n"); exit(1); } /* Initialize contents of new node */ p->value = num; p->left = NULL; p->right = NULL; return p; /* Return pointer to newly created node */ 6

Insert a new node in the tree void insert_node (Node **tree, int value) { /* The value is sorted as it is placed in the binary tree */ if (*tree == NULL) /* No more children, so insert here */ *tree = create_node(value); else if (value <= (*tree)->value) /* Traverse left branch */ insert_node(&(*tree)->left, value); else /* Traverse right branch */ insert_node(&(*tree)->right, value); } 7

Display the contents of the entire tree void display_inorder (Node *tree) { /* Use inorder recursion */ if (tree->left != NULL) display_inorder(tree->left); printf("%d ", tree->value); if (tree->right != NULL) display_inorder(tree->right); } 8

Example: 9 Pre-order traversal: display node traverse(left) traverse(right) Display order: 8 6 79 64 56 39 13 46 42 86 In-order traversal: 6 8 13 39 42 46 56 64 79 86 Post-order traversal: 6 13 42 46 39 56 64 86 79 8 Sample binary tree 8 6 79 64 86 56 39 13 46 42 tree Original order of input values: 8 79 6 64 56 86 39 46 13 42 9