Trees (part 2) CSE 2320 – Algorithms and Data Structures

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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Tree C and Data Structures Baojian Hua
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.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
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.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Tree Traversal.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
IS 2610: Data Structures Recursion, Divide and conquer Dynamic programming, Feb 2, 2004.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
IS 2610: Data Structures Discuss HW 2 problems Binary Tree (continued) Introduction to Sorting Feb 9, 2004.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Chapter 12 – Data Structures
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Week 6 - Wednesday CS221.
Trees.
Tree.
Section 8.1 Trees.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
slides created by Alyssa Harding
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Abstract Data Structures
Trees.
Trees Definitions Implementation Traversals K-ary Trees
Principles of Computing – UFCFA3-30-1
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees.
Tree traversals BST properties Search Insertion
Binary Trees, Binary Search Trees
Presentation transcript:

Trees (part 2) CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 4/19/18

Student Self-Review Review the theoretical lecture on trees that was covered earlier in the semester. Review your notes and materials on implementing trees in C.

Defining Nodes for Binary Trees typedef struct node *link; struct node { Item item; link left; link right; }; Other possible fields: link parent; int size; //size of subtree rooted at this node. Useful for balancing trees.

Traversing a Binary Tree Traversing is the process of going through each node of a tree, and doing something with that node. Examples: We can print the contents of the node. We can change the contents of the node. We can otherwise use the contents of the node in computing something. There are four standard choices for the order in which we visit nodes when we traverse a binary tree. Preorder (Root, L, R): we visit the node, then its left subtree, then its right subtree. (depth-first order) Inorder (L, Root, R): we visit the left subtree, then the node, then the right subtree. (depth-first order) Postorder (L, R, Root): we visit the left subtree, then the right subtree, then the node. (depth-first order) Level order: all the nodes on the level going from 0 to the last level. (breadth-first)

Examples 1 1 12 12 1 1 12 12 5 6 7 3 10 2 4 8 preorder inorder (E.g. if printing the nodes, the bullet indicates that you would print at that time.) preorder inorder postorder 1 1 12 12 1 1 12 12 List the nodes of the tree in: Preorder (___, ___, ___): _________________________ Inorder (___, ___, ___): __________________________ Postorder (___, ___, ___): ________________________ Level-order: __________________________ 5 6 7 3 10 2 4 8

Examples 1 7 12 6 5 3 10 2 4 8 1 12 1 1 12 12 preorder inorder (E.g. if printing the nodes, the bullet indicates that you would print at that time.) preorder inorder postorder 1 7 12 6 5 3 10 2 4 8 1 12 1 1 12 12 List the nodes of the tree in: Preorder (Root, Left, Right): 0, 1, 5, 3, 12, 6, 7, 10, 4, 8, 2. Inorder (Left, Root, Right): 5, 3, 1, 0, 6, 12, 4, 10, 8, 7, 2. Postorder (Left, Right, Root): 3, 5, 1, 6, 4, 8, 10, 2, 7, 12, 0. Level-order: 0, 1, 12, 5, 6, 7, 3, 10, 2, 4, 8.

1 12 inorder 1 7 12 6 5 3 10 2 4 8 Inorder (___, ___, ___): ________________________ 1 12 preorder 1 12 postorder 1 7 12 6 5 3 10 2 4 8 1 7 12 6 5 3 10 2 4 8 Preorder (___, ___, ___): _________________________ Postorder (___, ___, ___): ________________________

Recursive Tree Traversal void traverse_preorder(link h){ if (h == NULL) return; do_something_with(h); traverse_preorder (h->left); traverse_preorder (h->right); } For a tree with N nodes: Time complexity: Space complexity: void traverse_inorder(link h){ if (h == NULL) return; traverse_inorder (h->left); do_something_with(h); traverse_inorder (h->right); } void traverse_postorder(link h){ if (h == NULL) return; traverse_postorder(h->left); traverse_postorder(h->right); do_something_with(h); }

Class Practice Write the following (recursive or not) functions, in class: Count the number of nodes in a tree Compute the height of a tree Level-order traversal – discuss/implement Print the tree in a tree-like shape – discuss/implement Which functions are “similar” to the traversals discussed previously and to each other?

These slides contain code from the Sedgewick book.

Recursive Examples Computing the height Counting the number of the tree: Counting the number of nodes in the tree: int count(link h){ if (h == NULL) return 0; int c1 = count(h->left); int c2 = count(h->right); return c1 + c2 + 1; } int height(link h){ if (h == NULL) return -1; int u = height(h->left); int v = height(h->right); if (u > v) return u+1; else return v+1; }

Recursive Examples: print tree Print the contents of each node (assuming that the items in the nodes are characters) How will the output look like? What type of tree traversal is this? void printnode(char c, int h) { int i; for (i = 0; i < h; i++) printf(" "); printf("%c\n", c); } void show(link x, int h) { if (x == NULL) { printnode("*", h); return; printnode(x->item, h); show(x->left, h+1); show(x->right, h+1); Directory-like display.

Recursive and Iterative Preorder Traversal (Sedgewick) void traverse(link h, void (*visit)(link)) { if (h == NULL) return; (*visit)(h); traverse(h->left, visit); traverse(h->right, visit); } ----- void traverse(link h, void (*visit)(link)) { STACKinit(max); STACKpush(h); while (!STACKempty()) { (*visit)(h = STACKpop()); if (h->right != NULL) STACKpush(h->right); if (h->left != NULL) STACKpush(h->left); 1 12 5 6 7 3 10 2 4 8 Stack: Print:

Level-Order Traversal (for printing) // Adapted from Sedgewick void traverse(link h) { Queue Q = new_Queue(); put(Q,h); while (!empty(Q)) { h = get(Q); //gets first node printItem(h->item); if (h->left != NULL) put(Q,h->left); if (h->right != NULL) put(Q,h->right); } Queue:_______________________________ Print:_________________________________ 1 12 5 6 7 3 10 2 4 8

Level-Order Traversal (with function arguments) // Adapted from Sedgewick void traverse(link h, void (*visit)(link)) { Queue Q = new_Queue(); put(Q,h); while (!empty(Q)) { (*visit)(h = get(Q)); //gets first node if (h->left != NULL) put(Q,h->left); if (h->right != NULL) put(Q,h->right); } Queue:_______________________________ Print:_________________________________ 1 12 5 6 7 3 10 2 4 8

General Trees In a general tree, a node can have any number of children. How would you implement a general tree?

General Trees In a general tree, a node can have any number of children. Left-child - right-sibling implementation Draw tree and show example (There is a one-to-one correspondence between ordered trees and binary trees)