Trees in C CSE 2541 Rong Shi. Tree definition Recursively defined data structure Tree (in general) – Empty – Data + a specific number of subtrees Binary.

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

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Computer Science C++ High School Level By Guillermo Moreno.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
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.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
BINARY TREES && TREE TRAVERSALS. DEFINITION : Binary Tree A binary tree is made of nodes Each node contains –a "left" pointer -- left child –a "right"
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
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.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Trees and Recursive Algorithms. Recursive Algorithms A function can call another function; What would happen if a statement in F contained a call of F?
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
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 Data Structures.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
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)
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.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Foundation of Computing Systems Lecture 4 Trees: Part I.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 CMSC 341 Introduction to Trees Textbook sections:
Trees A non-linear implementation for collection classes.
(c) University of Washington20-1 CSC 143 Java 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
Recursive Objects (Part 4)
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
CS223 Advanced Data Structures and Algorithms
Chapter 21: Binary Trees.
slides created by Alyssa Harding
Trees.
Trees Definitions Implementation Traversals K-ary Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Trees in C CSE 2541 Rong Shi

Tree definition Recursively defined data structure Tree (in general) – Empty – Data + a specific number of subtrees Binary tree – Empty – Data + left subtree + right subtree

C Binary Tree node struct btnode { int data; struct btnode *left; struct btnode *right; } ;

What is this? struct treenode { int data; struct treenode *mynode; } ; Equivalent to a linked list

Creating a tree struct btnode *root; struct btnode *mynode = (struct btnode *) malloc (sizeof(struct btnode)); root = mynode; // use root to access the tree, like head for a linked list

Visual example of a binary tree Each value corresponds to a node in the tree root is a struct btnode pointer that points at the node containing the 9

Tree traversal (preorder) PreOrderPrint(struct btnode *anode) { printf(“%i”, anode->data); PreOrderPrint(anode->left); PreOrderPrint(anode->right); } Any problems with this function?

Tree traversal (preorder corrected) PreOrderPrint(struct btnode *anode) { if(anode == NULL) return; printf(“%i ”, anode->data); PreOrderPrint(anode->left); PreOrderPrint(anode->right); } Output of PreOrderPrint(root) is: (po9  pt9  po6  pt6… see blackboard)

Tree traversal (inorder) InOrderPrint(struct btnode *anode) { if(anode == NULL) return; InOrderPrint(anode->left); printf(“%i ”, anode->data); InOrderPrint(anode->right); } Output of InOrderPrint(root) is:

Tree traversal (postorder) PostOrderPrint(struct btnode *anode) { if(anode == NULL) return; PostOrderPrint(anode->left); PostOrderPrint(anode->right); printf(“%i ”, anode->data); } Output of PostOrderPrint(root) is:

Tree termination NULL pointers NULL are not btnodes, but the value of their parent’s left and right pointers NULL data -1 are btnodes, whose left and right btnodes are uninitialized Assumption: -1 is never valid data in the tree

Creating nodes struct node * NewNode(int data) { struct node *mynode = (struct node *) malloc (sizeof(struct node)); mynode->data = data; mynode->left = NULL; mynode->right = NULL; return(node); }

Deallocating binary trees Three things to do – Free current node – Recursively free left subtree – Recursively free right subtree What is the order? void delete_tree(struct btnode *leaf) { if( leaf != NULL ) { delete_tree(leaf->left); delete_tree(leaf->right); free( leaf ); }

Trees and arrays Figure from Map current node, left child, and right child to array positions Order in the array by level in the tree

Trees and arrays Figure from Map current node, left child, and right child to array positions t[i], t[2i+1], t[2i+2] Order in the array by level in the tree

Additional Terminology Depth: number of edges from the root to the node (node ‘7’ has depth 2, etc) Height: number of edges from the node to the deepest leaf Height of Tree: height of the root (tree-height = 2)

Reference Wiki Slides from CMU 121/lectures/Trees/trees.html