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

Slides:



Advertisements
Similar presentations
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
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.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
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.
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 ),
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.
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.
Topic 17 Introduction to Trees
Compiled by: Dr. Mohammad Omar Alhawarat
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Data Structures TREES.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
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.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
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.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
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.
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a 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.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 CMSC 341 Introduction to Trees Textbook sections:
(c) University of Washington20-1 CSC 143 Java Trees.
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.
CSCE 210 Data Structures and Algorithms
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Week 6 - Wednesday CS221.
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Trees.
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Data Structures & Algorithm Design
CS223 Advanced Data Structures and Algorithms
COSC2100: Data Structures and Algorithms
Trees.
Data Structures and Algorithms for Information Processing
Trees (part 2) CSE 2320 – Algorithms and Data Structures
Trees (Part 1, Theoretical)
Binary Trees, Binary Search Trees
Trees.
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

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

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. 2

A Family Tree (from Wikipedia) 3

Terminology Root Path Parent vs child Ascendants vs descendants Internal vs external nodes or non-terminal vs terminal nodes Leaves M-ary trees General trees Binary trees

Terminology We will typically draw trees with the root placed at the top. Each node has exactly one node directly above it, which is called a parent. – If Y is the parent of X, then Y is the node right after X on the path from X to the root. A path in a tree is a list of distinct vertices, in which successive vertices are connected by edges. – No vertex is allowed to appear twice in a path. 5

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, or external nodes. 6

Terminology NOTE: in the textbook external nodes are NULL and a leaf is an internal node that has only external nodes as children. I will use leaf to refer to an external node. Think in terms of internal and external nodes root Internal node External node 5

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. The height of a node is the number of edges from the node to the deepest leaf. 8

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, 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, This is not a binary tree, node 3 has 1 child. This is a binary tree.

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? 11

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 Reference: wikipedia ( 12

Properties of Binary 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 (complete or perfect tree) N if each internal node has one external child

Number of nodes per level

Defining Nodes for Binary Trees typedef struct node *link; struct node { Item item; link left; link right; }; 15

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 choices about the order in which we visit nodes when we traverse a binary tree. – Preorder: we visit the node, then its left subtree, then its right subtree. (depth-first order) – Inorder: we visit the left subtree, then the node, then the right subtree. (depth-first order) – Postorder: 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) 16

Examples In what order will the values of the nodes be printed if we print the tree by traversing it: – Preorder? – Inorder? – Postorder? – Level-order?

Examples In what order will the values of the nodes be printed if we print the tree by traversing it: – Preorder? 0, 1, 5, 2, 6, 7. – Inorder? 5, 1, 0, 6, 2, 7. – Postorder? 5, 1, 6, 7, 2, 0. – Level-order? 0, 1, 2, 5, 6,

Recursive Tree Traversal 19 void traverse_inorder(link h) { if (h == NULL) return; traverse_inorder (h->l); do_something_with(h); traverse_inorder (h->r); } void traverse_preorder(link h) { if (h == NULL) return; do_something_with(h); traverse_preorder (h->l); traverse_preorder (h->r); } void traverse_postorder(link h) { if (h == NULL) return; traverse_postorder (h->l); traverse_postorder (h->r); do_something_with(h); }

Practice Write the following functions: – Count the number of nodes in a tree – Compute the height of a tree – Level-order traversal – Print the tree in a tree-like shape Which functions are “similar”? Recursive or non-recursive functions. 20

Recursive Examples 21 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; } Counting the number of nodes in the tree: Computing the height of the tree:

Recursive Examples: print tree 22 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->l, h+1); show(x->r, h+1); } 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?

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

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) 24