Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.

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

Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Trees Briana B. Morrison Adapted from Alan Eugenio.
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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,
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Compiled by: Dr. Mohammad Omar Alhawarat
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 By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
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.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Foundation of Computing Systems Lecture 4 Trees: Part I.
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 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Trees Chapter 15.
Recursive Objects (Part 4)
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
8.2 Tree Traversals Chapter 8 - Trees.
Find in a linked list? first last 7  4  3  8 NULL
Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Design and Analysis of Algorithms
Trees Chapter 6.
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
8.2 Tree Traversals Chapter 8 - Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh

2 Tree Terminology A tree consists of a collection of elements or nodes Each node can contain data and (parent and child) links to other nodes Each node has one parent, except for the root node, which has no parent Each node can have any number of children

3 Tree Terminology (continued) Nodes that have the same parent are siblings A node that has no (non-empty) children is called a leaf node A generalization of the parent-child relationship is the ancestor-descendent relationship

4 Tree Terminology (continued) A subtree of a node is a tree whose root is a child of that node The level of a node is a measure of its distance from the root The height of a tree is the maximum level of any node in the tree the empty tree has height zero You will also hear depth used interchangably for height

5 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is a binary tree if either of the following is true T is empty Its root node has two subtrees, TL and TR, such that TL and TR are binary trees

6 Some Types of Binary Trees Expression tree Each node contains an operator or an operand Huffman tree Represents Huffman codes for characters that might appear in a text file Huffman code uses different numbers of bits to encode letters as opposed to ASCII or Unicode Binary search trees All elements in the left subtree precede those in the right subtree

7 Expression tree

8 Huffman Trees

9 Binary Search Tree

10 Full Binary Trees A binary tree is full if each node has zero or two (non- empty) children I’m going to stop saying non-empty children now; assume that whenever I say that a node has k children, I mean k non-empty children

11 Is this tree full? Book is a little ambiguous book doesn’t actually give a definition of full binary trees But this tree is full but not perfect

12 Perfect Binary Trees A binary tree is perfect it is a full binary tree and all leaf nodes are at the same level. Recursively, a binary tree is perfect if it is empty, or it has two perfect children with equal height

13 Complete Binary Trees A binary tree of height h is complete if it is filled up at level h-1, and at height h, any unfilled nodes are on the right

14 Nodes in a complete tree can be numbered Root is numbered 0 Children of a node numbered x are numbered 2x+1 and 2x+2 The nodes of a complete tree with n nodes are numbered 0..n-1

15 General Trees Nodes of a general tree can have any number of subtrees A general tree can be represented using a binary tree

16 Tree Traversals Often we want to determine the nodes of a tree and their relationship Can do this by walking through the tree in a prescribed order and visiting the nodes as they are encountered This process is called tree traversal Three kinds of tree traversal Inorder Preorder Postorder

17 Tree Traversals (continued) Preorder: Visit root node, traverse TL, traverse TR Inorder: Traverse TL, visit root node, traverse TR Postorder: Traverse TL, Traverse TR, visit root node

18 Visualizing Tree Traversals You can visualize a tree traversal by imagining a mouse that walks along the edge of the tree If the mouse always keeps the tree to the left, it will trace a route known as the Euler tour Preorder traversal if we record each node as the mouse first encounters it Inorder if each node is recorded as the mouse returns from traversing its left subtree Postorder if we record each node as the mouse last encounters it

19 Visualizing Tree Traversals (continued)