Introduction to Trees Chapter 6 Objectives

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Advertisements

Introduction to Trees Chapter 6 Objectives
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.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Search Trees Chapter 7 Objectives
Binary Trees Chapter 6.
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.
COSC2007 Data Structures II
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.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
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.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
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.
Discrete Mathematics Chapter 5 Trees.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
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.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Binary Search Trees Chapter 7 Objectives
Chapter 12 Abstract Data Type.
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.
Tree.
Chapter 5 : Trees.
Tree.
Tree traversal from Introduction to Trees Chapter 6 Objectives
Lecture 18. Basics and types of Trees
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 7 TREES.
TREES General trees Binary trees Binary search trees AVL trees
Trees and Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
Trees.
Dr.Surasak Mungsing CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 07: การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้างข้อมูลแบบ.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
การวิเคราะห์และออกแบบขั้นตอนวิธี
Binary Trees.
Binary Trees, Binary Search Trees
8.2 Tree Traversals Chapter 8 - Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Introduction to Trees Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and concepts Recognize and define the basic attributes of a binary tree Process trees using depth-first and breadth-first traversals Parse expressions using a binary tree Design and implement Huffman trees Understand the basic use and processing of general trees Data Structures: A Pseudocode Approach with C

6-1 Basic Tree Concepts Terminology User Representation We begin the Chapter with a discussion of the terminology used with trees. Once the terminology is understood, we discuss three user-oriented tree representations: general trees, indented parts lists, and parenthetical trees. Terminology User Representation Data Structures: A Pseudocode Approach with C

Basic Tree Concepts A tree consists of a finite set of elements, called nodes and a finite set of directed lines, called branches, that connect the nodes the number of branches associated with a node is the degree of the node Data Structures: A Pseudocode Approach with C

Basic Tree Concepts (2) indegree branch is a branch that directs toward the node outdegree branch is a branch that directs away from the node the sum of the indegree and outdegree branches equals the degree of the node Data Structures: A Pseudocode Approach with C

Basic Tree Concepts (3) if the tree is not empty, then the first node is called the root the indegree of the root is zero all of the nodes in a tree (except root) must have an indegree of exactly one all nodes in the tree can have zero, one, or more ourdegree(s) Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Terminology Name Meaning Leaf node Any node with an outdegree of zero Internal node Nodes that are not a root or a leaf Parent node A node that has successor (has outdegree >= 0) Child node A node has an indegree of one Siblings Two or more nodes with the same parent Ancestor Any node in the path from the root to the node Data Structures: A Pseudocode Approach with C

Terminology (2) Name Meaning Descendent Any node in the path below the parent node (all nodes in the paths from a given node to a leaf) Path A sequence of nodes in which each node is adjacent to the next one Level of a node Its distance from the root Height of a tree (depth of a tree) The level of the leaf in the longest path from the root plus one Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Subtrees A subtree is any connected structure below the root The first node in a subtree is known as the root of the subtree Subtrees can be subdivided into subtrees By this definition, a single node is a subtree Data Structures: A Pseudocode Approach with C

Subtrees (2) A recursive definition of a tree can be defined as : A tree is a set of nodes that is 1. Either empty, or 2. Has a designated node called the root from which hierarchically descendent zero or more subtrees, which are also trees Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Tree Representation a tree is generally implemented in the computer using pointers outside the computer there are 3 different representations for trees Organization chart format term notation is a general tree (discuss in section 6-5) Indented list often used in bill-of-materials systems Parenthetical listing uses with algebraic expression from figure 6-1 root ( B ( C D ) E F ( G H I ) ) Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

6-2 Binary Trees Properties Binary Tree Traversals Expression Trees A binary tree can have no more than two descendents. In this section we discuss the properties of binary trees, four different binary tree traversals, and two applications, expression trees and Huffman Code. Properties Binary Tree Traversals Expression Trees Huffman Code Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Properties: Height of binary trees we need to store N nodes in a binary tree the maximum height (Hmax) and the minimum height (Hmin) are Hmax = N Hmin = [log2 N] + 1 Data Structures: A Pseudocode Approach with C

Height of Binary Trees (2) a height of the binary tree is H minimum and maximum number of nodes in the tree Nmin = H Nmax = 2H - 1 Data Structures: A Pseudocode Approach with C

Properties: Balance the distance of a node from the root determines how efficiently it can be located therefore, the shorter we can make the tree, the easier it is to locate any desire node in the tree to determine if a tree is balanced, we calculate its balance factor Data Structures: A Pseudocode Approach with C

Balance (2) the balance factor (B) of a binary tree is the different in height between its left and right subtrees B = HL – HR HL = height of the left subtree HR = height of the right subtree what are values of B in figure 7-6 Data Structures: A Pseudocode Approach with C

Balance (3) a binary tree is balanced if the height of its subtrees differs by no more than one (it balance factor is –1, 0, 1) and its subtrees are also balanced this definition was created by Adelson-Veskii and Landis in the definition of an AVL tree Data Structures: A Pseudocode Approach with C

Complete Binary Trees a complete tree has the maximum number of entries for its height (Nmax) this occurs when the last level is full a tree is considered nearly complete if it has the minimum height for its node (Hmin) and all nodes in the last level are found on the left Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Binary Tree Structure each node in the structure must contain the data to be stored and two pointers to the left subtree to the right subtree Example: Node leftSubTree <pointer to Node> data <dataType> rightSubTree <pointer to Node> End Node Data Structures: A Pseudocode Approach with C

Binary Tree Traversals a binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence there are 2 general approaches to traversal sequence depth-first traversal proceeds along a path from the root through one child to the most distant descendent of that first child before processing a second child breadth-first traversal proceeds horizontally from the root to all of its children, then to its children’s children, and so forth until al nodes have been processed Data Structures: A Pseudocode Approach with C

Depth-First Traversals there are 3 different depth-first traversal sequences in binary trees preordered traversal (NLR) inordered traversal (LNR) postorder traversal (LRN) Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Breadth Traversal all of the children of a node before proceeding with the next level that is, given a root at level n, we process all nodes at level n before processing with the nodes at level n + 1 to traverse a tree in depth-first order, we use a stack to traverse a tree in breadth-first order, we use a queue Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Expression Trees one interesting series of binary tree applications are expression trees an expression is a sequence of tokens that follow prescribed rules a token may be either an operand or a operator the standard arithmetic operators are +, - , *, / Data Structures: A Pseudocode Approach with C

Expression Trees (2) an expression tree is a binary tree with the following properties 1. each leaf is an operand 2. the root and internal nodes are operators 3. subtrees are subexpressions with the root being an operator Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

6-3 General Trees Insertions into General Trees General Tree Deletions A general tree can have an unlimited number of descendents. We discuss three topics in this section: general tree insertions, deletions, and converting a general tree to a binary tree. Insertions into General Trees General Tree Deletions Changing a General Tree to a Binary Tree Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Changing General Tree to Binary Tree it is easier to represent binary trees in programs than it is to represent general trees therefore, we should represent general trees with a binary tree format in a general tree, there are 2 relationships that we can use: parent to child sibling to sibling Data Structures: A Pseudocode Approach with C

Changing General Tree to Binary Tree (2) to change the general tree to a binary tree, we need three steps identify the branches from the parents to their first or leftmost child (these branches become left pointers in the binary tree) : figure 6-17 (b) connect siblings, staring with the leftmost child, using a branch for each sibling to is right sibling, figure 6-17 (c) remove all unneeded branches from the parent to its children, figure 6-17 (d) the tree is redrawed in a binary tree format as shown in figure 6-24(e) Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

General Tree Deletions a node may be deleted only if it is a leaf if the user tries to delete that has children, the application could be programmed to delete the children first and then delete the requested node when a node is deleted, siblings must be checked, does it have a right subtree pointer? if the first node is deleted, then the parent’s left pointer must be updated if any other node is deleted, then its predecessor’s right pointer must be updated to point to the deleted node’s successor Data Structures: A Pseudocode Approach with C