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.

Slides:



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

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Introduction to Trees Chapter 6 Objectives
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.
1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:
Trees Chapter 8.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Binary Search Trees Chapter 7 Objectives
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Binary Trees Chapter 6.
Let G be a pseudograph with vertex set V, edge set E, and incidence mapping f. Let n be a positive integer. A path of length n between vertex v and vertex.
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.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
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 A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
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.
Data Structures TREES.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 12 Abstract Data Type.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Tree.
Chapter 5 : Trees.
Tree traversal from Introduction to Trees Chapter 6 Objectives
Lecture 18. Basics and types of Trees
Binary Tree and General Tree
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Binary Trees.
Introduction to Trees Chapter 6 Objectives
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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 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 Introduction to Trees

Data Structures: A Pseudocode Approach with C 2 Basic Tree Concepts A tree consists of 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 3

4 Basic Tree Concepts When the branch is directed toward the node, it is indegree branch. When the branch is directed away from the node, it is an outdegree branch. The sum of the indegree and outdegree branches is the degree of the node. If the tree is not empty, the first node is called the root.

Data Structures: A Pseudocode Approach with C 5 Basic Tree Concepts The indegree of the root is, by definition, zero. With the exception of the root, all of the nodes in a tree must have an indegree of exactly one; that is, they may have only one predecessor. All nodes in the tree can have zero, one, or more branches leaving them; that is, they may have outdegree of zero, one, or more.

Data Structures: A Pseudocode Approach with C 6

7 Basic Tree Concepts A leaf is any node with an outdegree of zero, that is, a node with no successors. A node that is not a root or a leaf is known as an internal node. A node is a parent if it has successor nodes; that is, if it has outdegree greater than zero. A node with a predecessor is called a child.

Data Structures: A Pseudocode Approach with C 8 Basic Tree Concepts Two or more nodes with the same parents are called siblings. An ancestor is any node in the path from the root to the node. A descendant is any node in the path below the parent node; that is, all nodes in the paths from a given node to a leaf are descendants of that node.

Data Structures: A Pseudocode Approach with C 9 Basic Tree Concepts A path is a sequence of nodes in which each node is adjacent to the next node. The level of a node is its distance from the root. The root is at level 0, its children are at level 1, etc. …

Data Structures: A Pseudocode Approach with C 10 Basic Tree Concepts The height of the tree is the level of the leaf in the longest path from the root plus 1. By definition the height of any empty tree is -1. A subtree is any connected structure below the root. The first node in the subtree is known as the root of the subtree.

Data Structures: A Pseudocode Approach with C 11

Data Structures: A Pseudocode Approach with C 12

Data Structures: A Pseudocode Approach with C 13 Recursive definition of a tree A tree is a set of nodes that either: is empty or has a designated node, called the root, from which hierarchically descend zero or more subtrees, which are also trees.

Data Structures: A Pseudocode Approach with C Binary 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 Properties Binary Tree Traversals Examples: Expression Trees Huffman Code

Data Structures: A Pseudocode Approach with C 15 Binary Trees A binary tree is a tree in which no node can have more than two subtrees; the maximum outdegree for a node is two. In other words, a node can have zero, one, or two subtrees. These subtrees are designated as the left subtree and the right subtree.

Data Structures: A Pseudocode Approach with C 16

Data Structures: A Pseudocode Approach with C 17 A null tree is a tree with no nodes

Data Structures: A Pseudocode Approach with C 18 Some Properties of Binary Trees The height of binary trees can be mathematically predicted Given that we need to store N nodes in a binary tree, the maximum height is A tree with a maximum height is rare. It occurs when all of the nodes in the entire tree have only one successor.

Data Structures: A Pseudocode Approach with C 19 Some Properties of Binary Trees The minimum height of a binary tree is determined as follows: For instance, if there are three nodes to be stored in the binary tree (N=3) then H min =2.

Data Structures: A Pseudocode Approach with C 20 Some Properties of Binary Trees Given a height of the binary tree, H, the minimum number of nodes in the tree is given as follows:

Data Structures: A Pseudocode Approach with C 21 Some Properties of Binary Trees The formula for the maximum number of nodes is derived from the fact that each node can have only two descendents. Given a height of the binary tree, H, the maximum number of nodes in the tree is given as follows:

Data Structures: A Pseudocode Approach with C 22 Some Properties of Binary Trees The children of any node in a tree can be accessed by following only one branch path, the one that leads to the desired node. The nodes at level 1, which are children of the root, can be accessed by following only one branch; the nodes of level 2 of a tree can be accessed by following only two branches from the root, etc. The balance factor of a binary tree is the difference in height between its left and right subtrees:

Data Structures: A Pseudocode Approach with C 23 B=0 B=1 B=-1 B=0 B=1 B=-2 B=2 Balance of the tree

Data Structures: A Pseudocode Approach with C 24 Some Properties of Binary Trees In the balanced binary tree (definition of Russian mathematicians Adelson-Velskii and Landis) the height of its subtrees differs by no more than one (its balance factor is -1, 0, or 1), and its subtrees are also balanced.

Data Structures: A Pseudocode Approach with C 25 Complete and nearly complete binary trees A complete tree has the maximum number of entries for its height. The maximum number is reached when the last level is full. A tree is considered nearly complete if it has the minimum height for its nodes and all nodes in the last level are found on the left

Data Structures: A Pseudocode Approach with C 26

Data Structures: A Pseudocode Approach with C 27 Binary Tree Traversal A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence. In the depth-first traversal processing process along a path from the root through one child to the most distant descendant of that first child before processing a second child.

Data Structures: A Pseudocode Approach with C 28

Data Structures: A Pseudocode Approach with C 29

Data Structures: A Pseudocode Approach with C 30

Data Structures: A Pseudocode Approach with C 31

Data Structures: A Pseudocode Approach with C 32

Data Structures: A Pseudocode Approach with C 33

Data Structures: A Pseudocode Approach with C 34

Data Structures: A Pseudocode Approach with C 35

Data Structures: A Pseudocode Approach with C 36

Data Structures: A Pseudocode Approach with C 37

Data Structures: A Pseudocode Approach with C 38

Data Structures: A Pseudocode Approach with C 39

Data Structures: A Pseudocode Approach with C 40

Data Structures: A Pseudocode Approach with C 41

Data Structures: A Pseudocode Approach with C 42

Data Structures: A Pseudocode Approach with C 43

Data Structures: A Pseudocode Approach with C 44

Data Structures: A Pseudocode Approach with C 45 Huffman Code Huffman code is widely used for data compression; it reduces the number of bits sent or stored.

Data Structures: A Pseudocode Approach with C 46 Huffman Code We used a binary tree to compress data. Data compression is used in many situations. E.g. sending data over internet. Character Code: Each character in a normal uncompressed text file is represented in the computer by one byte or by two bytes. E.g. ASCII code For text, the most common approach is to reduce the number of bits that represent the most-used characters. E.g. E is the most common letter, so few bits can be used to encode it.

Data Structures: A Pseudocode Approach with C 47 Huffman Code No code can be the prefix of any other code. No space characters in binary message, only 0s and 1s. AGOODMARKET

Data Structures: A Pseudocode Approach with C 48 Character weights and assignments for a sample of Huffman code CharacterWeightCharacterWeightCharacterWeight A10I4R7 C3K2S5 D4M3T12 E15N6U5 G2O8

Data Structures: A Pseudocode Approach with C 49 Huffman Tree(1/3 )

Data Structures: A Pseudocode Approach with C 50 Huffman Tree(2/3)

Data Structures: A Pseudocode Approach with C 51 Huffman Tree(3/3)

Data Structures: A Pseudocode Approach with C 52 Huffman code assignment