Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Data Structures.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
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.
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 Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Topic 17 Introduction to Trees
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Trees Saurav Karmakar
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Week 6 - Wednesday CS221.
Trees.
Tree.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
CMSC 341 Introduction to Trees.
Data Structures & Algorithm Design
CSC 172– Data Structures and Algorithms
Binary Trees, Binary Search Trees
Data Structures and Database Applications Binary Trees in C#
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
COSC2100: Data Structures and Algorithms
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Data Structures and Algorithms for Information Processing
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Trees (part 2) CSE 2320 – Algorithms and Data Structures
Trees Definitions Implementation Traversals K-ary Trees
Trees CMSC 202, Version 5/02.
Trees (Part 1, Theoretical)
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Tree.
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Trees Trees are a natural data structure for representing several types of data. Family trees. Organizational chart of a corporation, showing who supervises who. Folder (directory) structure on a hard drive.

Terminology Root: 0 Path: 0-2-6, 1-3, 1-3-4 Parent vs child Ascendants vs descendants: ascendants of 3: 1,0 // descendants of 1: 5, 3,4 Internal vs external nodes (non-terminal vs terminal nodes) Leaves: 5,4,6,7 M-ary trees (Binary trees) General trees Subtree 1 2 5 3 6 7 4

Terminology If Y is the parent of X, then X is called a child of Y. The root has no parents. Every other node, except for the root, has exactly one parent. A node can have 0, 1, or more children. Nodes that have children are called internal nodes or non-terminal nodes. Nodes that have no children are called terminal nodes, external nodes, or leaves. leaves

Terminology The level of the root is defined to be 0. The level of each node is defined to be 1+ the level of its parent. The depth of a node is the number of edges from the root to the node. (It is equal to the level of that node.) The height of a node is the number of edges from the node to the deepest leaf. picture

M-ary Trees An M-ary tree is a tree where every node is either a leaf or it has exactly M children. Example: binary trees, ternary trees, ... 1 7 2 5 3 4 6 1 7 2 6 Is this a binary tree? Is this a binary tree?

M-ary Trees An M-ary tree is a tree where every node is either a leaf or it has exactly M children. Example: binary trees, ternary trees, ... 1 7 2 5 3 4 6 1 7 2 6 This is a binary tree. This is not a binary tree, node 3 has 1 child.

General Trees Review this topic at the end. In a general tree a node can have any number of children. How would you implement a general tree?

Types of binary trees Perfect – each internal node has exactly 2 children and all the leaves are on the same level. E.g. ancestry tree (anyone will have exactly 2 parents). Full – every node has exactly 0 or 2 children. E.g. tree generated by the Fibonacci recursive calls. Binary tree. Complete tree – every level, except for possibly the last one is completely filled and on the last level, all the nodes are as far on the left as possible. E.g. the heap tree. Height: and it can be stored as an array. Reference: wikipedia (https://en.wikipedia.org/wiki/Binary_tree)

Perfect Binary Trees A perfect binary tree with N nodes has: +1 levels height leaves (half the nodes are on the last level) internal nodes (half the nodes are internal) 𝑘=0 𝑛 2 𝑘 = 2 𝑛+1 −1 Level Nodes per level Sum of nodes from root up to this level 20 (=1) 21 – 1 (=1) 1 21 (=2) 22 – 1 (=3) 2 22 (=4) 23 – 1 (=7) … i 2i 2i+1 – 1 n 2n 2n+1 – 1 1 2 3 4 5 6 7 . . . . . . . . . . . . . . . . . . . . . . . .

Properties of Full Trees A full binary tree (0/2 children) with N internal nodes has: N+1 external nodes. 2N edges (links). height at least lg N and at most N: lg N if all external nodes are at the same level (perfect tree) N if each internal node has one external child. 8 3 1 2 1 5 7 8 6 7

Number of nodes per level 1 2 5 8 6 7

Defining Nodes for Binary Trees typedef struct node *link; struct node { Item item; link left; link right; }; Other possible fields: parent (type link) Size (of subtree rooted at this node) (type int)

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. We have 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 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 1 12 12 1 1 12 12 5 6 7 3 10 2 4 8 preorder inorder postorder 1 1 12 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. 5 6 7 3 10 2 4 8

Recursive Tree Traversal void traverse_preorder(link h){ if (h == NULL) return; do_something_with(h); traverse_preorder (h->left); traverse_preorder (h->right); } 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/impelment 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 // Adapted from Sedgewick void traverse(link h, void (*visit)(link)) { Queue Q = new_Queue(max); 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)