Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
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
5
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
6
Data Structures: A Pseudocode Approach with C
7
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
8
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
9
Data Structures: A Pseudocode Approach with C
10
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
11
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
12
Data Structures: A Pseudocode Approach with C
13
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
14
Data Structures: A Pseudocode Approach with C
15
Data Structures: A Pseudocode Approach with C
16
Data Structures: A Pseudocode Approach with C
17
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
18
Data Structures: A Pseudocode Approach with C
19
Data Structures: A Pseudocode Approach with C
20
Data Structures: A Pseudocode Approach with C
21
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
22
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
23
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
24
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
25
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
26
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
27
Data Structures: A Pseudocode Approach with C
28
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
29
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
30
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
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
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
41
Data Structures: A Pseudocode Approach with C
42
Data Structures: A Pseudocode Approach with C
43
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
44
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
45
Data Structures: A Pseudocode Approach with C
46
Data Structures: A Pseudocode Approach with C
47
Data Structures: A Pseudocode Approach with C
48
Data Structures: A Pseudocode Approach with C
49
Data Structures: A Pseudocode Approach with C
50
Data Structures: A Pseudocode Approach with C
51
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
52
Data Structures: A Pseudocode Approach with C
53
Data Structures: A Pseudocode Approach with C
54
Data Structures: A Pseudocode Approach with C
55
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
56
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
57
Data Structures: A Pseudocode Approach with C
58
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.