SlideShare a Scribd company logo
Chapter 12 Trees
Chapter Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze tree implementations of collections Discuss methods for traversing trees Examine a binary tree example
Trees A  Tree  is a non-linear structure defined by the concept that each  node  in the tree, other than the first node or  root  node, has exactly one parent For trees, the operations are dependent upon the type of tree and it’s use
Definitions In order to discuss trees, we must first have a common vocabulary We have already introduced a couple of terms:  node  which refers to a location in the tree where an element is stored, and  root  which refers to the node at the base of the tree or the one node in the tree that does not have a parent
Definitions Each node of the tree points to the nodes that are directly beneath it in the tree These nodes are referred to as its  children A child of a child is then called a  grandchild , a child of a grandchild called a  great-grandchild A node that does not have at least one child is called a  leaf A node that is not the root and has at least one child is called an  internal node
FIGURE 12.1   Tree terminology
Definitions Any node below another node and on a path from that node is called a  descendant  of that node  Any node above another node on a connecting path from the root to that node is called an  ancestor  of that node All children of the same node are called  siblings A tree that limits each node to no more than n children is called an n-ary tree
Definitions Each node of the tree is at a specific  level  or  depth  within the tree The level of a node is the length of the path from the root to the node This  pathlength  is determined by counting the number of links that must be followed to get from the root to the node The root is considered to be level 0, the children of the root are at level 1, the grandchildren of the root are at level 2, and so on
Definitions The  height  or  order  of a tree is the length of the longest path from the root to a leaf  Thus the height or order of the tree in the next slide is 3 The path from the root (A) to leaf (F) is of length 3 The path from the root (A) to leaf (C) is of length 1
FIGURE 12.2   Path length and level
Definitions A tree is considered to be  balanced  if all of the leaves of the tree are at roughly the same depth While the use of the term “roughly” may not be intellectually (tri tue)  satisfying, the actual definition is dependent upon the algorithm being used Some algorithms define balanced as all of the leaves being at level h or h-1 where h is the height of the tree (where h = log N n for an N-ary tree)
FIGURE 12.3   Balanced  and unbalanced trees
Definitions The concept of a  complete  tree is related to the balance of a tree A tree is considered  complete  if it is balanced and all of the leaves at level h are on the left side of the tree While a seemingly arbitrary concept, as we will discuss in later chapters, this definition has implications for how the tree is stored in certain implementations Trees a and c on the next slide are complete while tree b is not
FIGURE 12.4   Some complete trees
Implementing Trees with Links While it is not possible to discuss the details of an implementation of a tree without defining the type of tree and its use, we can look at general strategies for implementing trees The most obvious implementation of tree is a linked structure Each node could be defined as a TreeNode class, as we did with the LinearNode class for linked lists
Implementing Trees with Links Each node would contain a pointer to the element to be stored in that node as well as pointers for each of the possible children of the node Depending on the implementation, it may also be useful to store a pointer in each node to its parent
Implementing Trees with Arrays For certain types of trees, specifically binary trees, a computational strategy can be used for storing a tree using an array  For any element stored in position n of the array, that elements left child will be stored in position ((2*n) + 1) and that elements right child will be stored in position (2*(n+1))
Implementing Trees with Arrays This strategy can be managed in terms of capacity in much the same way that we did for other array-based collections Despite the conceptual elegance of this solution, it is not without drawbacks For example, if the tree that we are storing is not complete or relatively complete, we may be wasting large amounts of memory allocated in the array for positions of the tree that do not contain data
FIGURE 12.5   Computational strategy  for array implementation of trees
Implementing Trees with Arrays A second possible array implementation of trees is modeled after the way operating systems manage memory  Instead of assigning elements of the tree to array position by location in the tree, array positions are allocated contiguously on a first come first served basis Each element of the array will be a node class similar to the TreeNode class that we discussed earlier
Implementing Trees with Arrays However, instead of storing object reference variables as pointers to its children (and perhaps its parent), each node would store the array index of each child (and perhaps its parent) This approach allows elements to be stored contiguously in the array so that space is not wasted  However, this approach increases the overhead for deleting elements in the tree since either remaining elements will have to be shifted to maintain contiguity or a free-list will have to be maintained
FIGURE 12.6   Simulated link strategy for array implementation of trees
Analysis of Trees Trees are a useful and efficient way to implement other collections In our analysis of list implementations in Chapter 8, we described the find operation as expected case n/2 or O(n) However, if we implemented an ordered list using a balanced  binary search tree , a binary tree with the added property that the left child is always less than the parent which is always less than or equal to the right child, then we could improve the efficiency of the find operation to O(log n)
Analysis of Trees This is due to the fact that the height or order of such a tree will always be log 2 n where n is the number of elements in the tree This is very similar to our discussion of binary search in Chapter 11 In fact, for any balanced N-ary tree with n elements, the tree’s height will be log N n With the added ordering property of a binary search tree, you are guaranteed to at worst search one path from the root to a leaf
Tree Traversals There are four basic algorithms for traversing a tree: Preorder traversal Inorder traversal Postorder traversal Levelorder traversal
Preorder traversal Preorder traversal is accomplished by visiting each node, followed by its children, starting with the root Given the complete binary tree on the next slide, a preorder traversal would produce the order: A  B  D  E  C
FIGURE 12.7   A complete tree
Preorder traversal Stated in pseudocode, the algorithm for a preorder traversal of a binary tree is: Visit node Traverse(left child) Traverse(right child)
Inorder traversal Inorder traversal is accomplished by visiting the left child of the node, then the node, then any remaining child nodes starting with the root An inorder traversal of the previous tree produces the order: D  B  E  A  C
Inorder traversal Stated in pseudocode, the algorithm for an inorder traversal of a binary tree is: Traverse(left child) Visit node Traverse(right child)
Postorder traversal Postorder traversal is accomplished by visiting the children, then the node starting with the root Given the same tree, a postorder traversal produces the following order: D  E  B  C  A
Postorder traversal Stated in pseudocode, the algorithm for a postorder traversal of a binary tree is: Traverse(left child) Traverse(right child)  Visit node
Levelorder traversal Levelorder traversal is accomplished by visiting all of the nodes at each level, one level at at time, starting with the root Given the same tree, a levelorder traversal produces the order: A  B  C  D  E
Levelorder traversal Stated in pseudocode, the algorithm for a preorder traversal of a binary tree is:
Implementing Binary Trees As an example of possible implementations of trees, lets explore a simple implementation of a binary tree Having specified that we are implementing a binary tree, we can identify a set of possible operations that would be common for all binary trees Notice however, that other than the constructors, none of these operations add any elements to the tree It is not possible to define an operation to add an element to the tree until we know more about how the tree is to be used
FIGURE 12.8   The operations on a binary tree
FIGURE 12.9   UML description  of the BinaryTreeADT interface
Listing 12.1
Listing 12.1  (cont.)
Listing 12.1  (cont.)
LinkedBinaryTree Lets examine a linked implementation of a binary tree Our implementation will need to keep track of the node that is the root of the tree as well as the count of elements in the tree protected int count; protected BinaryTreeNode<T> root;
LinkedBinaryTree We will provide three constructors One to create a null tree One to create a tree containing a single element One to create a new tree with the given element at the root and combining two existing trees
LinkedBinaryTree - constructors
LinkedBinaryTree - constructors
The BinaryTreeNode Class We will also need a class to represent each node in the tree Since this is a binary tree, we will create a BinaryTreeNode class that contain a reference to the element stored in the node as well as references for each of the children
Listing 12.2
Listing 12.2  (cont.)
Listing 12.2  (cont.)
The LinkedBinaryTree Class Lets examine some of the methods of the LinkedBinaryTree Class Keep in mind that each node of a tree represents a sub-tree
The removeLeftSubtree Method
The find and findagain Methods The find method provides an excellent example of the recursion that is possible given the nature of a tree We use the private method findagain to support the public find method This allows us to distinguish between the original invocation of the method and the recursive calls
The find Method
The findagain Method
The iteratorInOrder Method Like the find method, the iteratorInOrder method uses a private method, inorder, to support recursion The traversals for a tree might be implemented as toString methods or iterators or both
The iteratorInOrder Method
The inorder Method
Expression Trees Now lets look at an example using a binary tree In Chapter 6, we used a stack to evaluate postfix expressions Now we modify that algorithm to construct an expression tree
FIGURE 12.10   An example expression tree
Expression Trees Our expression tree class will extend our LinkedBinaryTree class This class provides constructors that reference the constructors of the LinkedBinaryTree class The class also provides an evaluate method to recursively evaluate an expression tree once it has been constructed The ExpressionTreeObj class represents the expression tree objects to be stored in the binary tree
Listing 12.3
Listing 12.3  (cont.)
Listing 12.3  (cont.)
Listing 12.3  (cont.)
Listing 12.3  (cont.)
Listing 12.4
Listing 12.4  (cont.)
Listing 12.4  (cont.)
Listing 12.4  (cont.)
FIGURE 12.11   Building an Expression Tree from a postfix expression
The Postfix and PostfixEvaluator Classes The Postfix and PostfixEvaluator classes are modifications of those presented in chapter 6 The solve method of the PostfixEvaluator class uses a pair of stacks to create an expression tree from a valid post-fix expression It then outputs the result using the evaluate method of the ExpressionTree class
Listing 12.5
Listing 12.6
Listing 12.6  (cont.)
Listing 12.6  (cont.)
Listing 12.6  (cont.)
Listing 12.6  (cont.)
Listing 12.6  (cont.)
FIGURE 12.12   UML description of the Postfix example

More Related Content

PDF
Pattern Mining in large time series databases
PPTX
K nearest neighbor
PDF
Binary Trees
PPTX
Unit 1 LINEAR DATA STRUCTURES
DOCX
Mca2020 advanced data structure
PPT
similarity measure
PDF
Feed forward neural network for sine
PDF
Lesson 35
Pattern Mining in large time series databases
K nearest neighbor
Binary Trees
Unit 1 LINEAR DATA STRUCTURES
Mca2020 advanced data structure
similarity measure
Feed forward neural network for sine
Lesson 35

Viewers also liked (11)

PPT
Digital Communication Principle
PPT
Computer Network - OSI model
PPT
RADIO FREQUENCY COMMUNICATION SYSTEMS, ANTENNA THEORY AND MICROWAVE DEVICES
PPT
Photomask Fabrication
PPTX
MSc_thesis
PPT
Computer networks--osi model
PPT
(Binary tree)
PPT
Chapter 4 - Digital Transmission
PPT
Chapter 3 - Data and Signals
Digital Communication Principle
Computer Network - OSI model
RADIO FREQUENCY COMMUNICATION SYSTEMS, ANTENNA THEORY AND MICROWAVE DEVICES
Photomask Fabrication
MSc_thesis
Computer networks--osi model
(Binary tree)
Chapter 4 - Digital Transmission
Chapter 3 - Data and Signals
Ad

Similar to Ch12 Tree (20)

PPTX
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
PPT
Tree 11.ppt
DOCX
Unit 3,4.docx
PPTX
non linear data structure -introduction of tree
PPTX
Tree.pptx
PDF
CPSC 125 ch 5sec 2
PDF
Cpsc125 ch5sec2
PDF
Binary tree
PDF
CS1027-Trees-2020 lecture one .pdf
PPTX
Introduction to tree ds
PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
PPT
data_structures_and_applications_-_module-4.ppt
PPT
PDF
Chapter 5_Trees.pdf
PPTX
Saikat techhnology of techtechhnology of techGhorai.pptx
PPTX
PPTX
Lecture 8 data structures and algorithms
Data Structures and Algorithms - Lecture 10 - Thushapan.pptx
Tree 11.ppt
Unit 3,4.docx
non linear data structure -introduction of tree
Tree.pptx
CPSC 125 ch 5sec 2
Cpsc125 ch5sec2
Binary tree
CS1027-Trees-2020 lecture one .pdf
Introduction to tree ds
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
data_structures_and_applications_-_module-4.ppt
Chapter 5_Trees.pdf
Saikat techhnology of techtechhnology of techGhorai.pptx
Lecture 8 data structures and algorithms
Ad

More from leminhvuong (20)

PPTX
PPT
Lession2 Xinetd
PPT
Module 7 Sql Injection
PPT
Iptables
PPT
Lession1 Linux Preview
PPT
PPT
PPT
Net Admin Intro
PPT
Lession4 Dhcp
PPT
Lession3 Routing
PPT
Module 1 Introduction
PPT
Wire Less
PPT
Net Security Intro
PPT
Module 10 Physical Security
PPT
Module 9 Dos
PPT
Module 8 System Hacking
PPT
Module 6 Session Hijacking
PPT
Module 5 Sniffers
PPT
Module 4 Enumeration
PPT
Module 3 Scanning
Lession2 Xinetd
Module 7 Sql Injection
Iptables
Lession1 Linux Preview
Net Admin Intro
Lession4 Dhcp
Lession3 Routing
Module 1 Introduction
Wire Less
Net Security Intro
Module 10 Physical Security
Module 9 Dos
Module 8 System Hacking
Module 6 Session Hijacking
Module 5 Sniffers
Module 4 Enumeration
Module 3 Scanning

Recently uploaded (20)

PDF
Unit 1 Cost Accounting - Cost sheet
PPT
Chapter four Project-Preparation material
PPTX
AI-assistance in Knowledge Collection and Curation supporting Safe and Sustai...
PDF
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
PDF
Nidhal Samdaie CV - International Business Consultant
PDF
SIMNET Inc – 2023’s Most Trusted IT Services & Solution Provider
PPTX
Probability Distribution, binomial distribution, poisson distribution
PDF
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
DOCX
Euro SEO Services 1st 3 General Updates.docx
PPTX
CkgxkgxydkydyldylydlydyldlyddolydyoyyU2.pptx
PDF
Power and position in leadershipDOC-20250808-WA0011..pdf
PDF
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
PDF
20250805_A. Stotz All Weather Strategy - Performance review July 2025.pdf
PPTX
Belch_12e_PPT_Ch18_Accessible_university.pptx
PDF
DOC-20250806-WA0002._20250806_112011_0000.pdf
PPTX
HR Introduction Slide (1).pptx on hr intro
DOCX
Business Management - unit 1 and 2
PPTX
5 Stages of group development guide.pptx
PDF
Ôn tập tiếng anh trong kinh doanh nâng cao
PPTX
Lecture (1)-Introduction.pptx business communication
Unit 1 Cost Accounting - Cost sheet
Chapter four Project-Preparation material
AI-assistance in Knowledge Collection and Curation supporting Safe and Sustai...
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
Nidhal Samdaie CV - International Business Consultant
SIMNET Inc – 2023’s Most Trusted IT Services & Solution Provider
Probability Distribution, binomial distribution, poisson distribution
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
Euro SEO Services 1st 3 General Updates.docx
CkgxkgxydkydyldylydlydyldlyddolydyoyyU2.pptx
Power and position in leadershipDOC-20250808-WA0011..pdf
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
20250805_A. Stotz All Weather Strategy - Performance review July 2025.pdf
Belch_12e_PPT_Ch18_Accessible_university.pptx
DOC-20250806-WA0002._20250806_112011_0000.pdf
HR Introduction Slide (1).pptx on hr intro
Business Management - unit 1 and 2
5 Stages of group development guide.pptx
Ôn tập tiếng anh trong kinh doanh nâng cao
Lecture (1)-Introduction.pptx business communication

Ch12 Tree

  • 2. Chapter Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze tree implementations of collections Discuss methods for traversing trees Examine a binary tree example
  • 3. Trees A Tree is a non-linear structure defined by the concept that each node in the tree, other than the first node or root node, has exactly one parent For trees, the operations are dependent upon the type of tree and it’s use
  • 4. Definitions In order to discuss trees, we must first have a common vocabulary We have already introduced a couple of terms: node which refers to a location in the tree where an element is stored, and root which refers to the node at the base of the tree or the one node in the tree that does not have a parent
  • 5. Definitions Each node of the tree points to the nodes that are directly beneath it in the tree These nodes are referred to as its children A child of a child is then called a grandchild , a child of a grandchild called a great-grandchild A node that does not have at least one child is called a leaf A node that is not the root and has at least one child is called an internal node
  • 6. FIGURE 12.1 Tree terminology
  • 7. Definitions Any node below another node and on a path from that node is called a descendant of that node Any node above another node on a connecting path from the root to that node is called an ancestor of that node All children of the same node are called siblings A tree that limits each node to no more than n children is called an n-ary tree
  • 8. Definitions Each node of the tree is at a specific level or depth within the tree The level of a node is the length of the path from the root to the node This pathlength is determined by counting the number of links that must be followed to get from the root to the node The root is considered to be level 0, the children of the root are at level 1, the grandchildren of the root are at level 2, and so on
  • 9. Definitions The height or order of a tree is the length of the longest path from the root to a leaf Thus the height or order of the tree in the next slide is 3 The path from the root (A) to leaf (F) is of length 3 The path from the root (A) to leaf (C) is of length 1
  • 10. FIGURE 12.2 Path length and level
  • 11. Definitions A tree is considered to be balanced if all of the leaves of the tree are at roughly the same depth While the use of the term “roughly” may not be intellectually (tri tue) satisfying, the actual definition is dependent upon the algorithm being used Some algorithms define balanced as all of the leaves being at level h or h-1 where h is the height of the tree (where h = log N n for an N-ary tree)
  • 12. FIGURE 12.3 Balanced and unbalanced trees
  • 13. Definitions The concept of a complete tree is related to the balance of a tree A tree is considered complete if it is balanced and all of the leaves at level h are on the left side of the tree While a seemingly arbitrary concept, as we will discuss in later chapters, this definition has implications for how the tree is stored in certain implementations Trees a and c on the next slide are complete while tree b is not
  • 14. FIGURE 12.4 Some complete trees
  • 15. Implementing Trees with Links While it is not possible to discuss the details of an implementation of a tree without defining the type of tree and its use, we can look at general strategies for implementing trees The most obvious implementation of tree is a linked structure Each node could be defined as a TreeNode class, as we did with the LinearNode class for linked lists
  • 16. Implementing Trees with Links Each node would contain a pointer to the element to be stored in that node as well as pointers for each of the possible children of the node Depending on the implementation, it may also be useful to store a pointer in each node to its parent
  • 17. Implementing Trees with Arrays For certain types of trees, specifically binary trees, a computational strategy can be used for storing a tree using an array For any element stored in position n of the array, that elements left child will be stored in position ((2*n) + 1) and that elements right child will be stored in position (2*(n+1))
  • 18. Implementing Trees with Arrays This strategy can be managed in terms of capacity in much the same way that we did for other array-based collections Despite the conceptual elegance of this solution, it is not without drawbacks For example, if the tree that we are storing is not complete or relatively complete, we may be wasting large amounts of memory allocated in the array for positions of the tree that do not contain data
  • 19. FIGURE 12.5 Computational strategy for array implementation of trees
  • 20. Implementing Trees with Arrays A second possible array implementation of trees is modeled after the way operating systems manage memory Instead of assigning elements of the tree to array position by location in the tree, array positions are allocated contiguously on a first come first served basis Each element of the array will be a node class similar to the TreeNode class that we discussed earlier
  • 21. Implementing Trees with Arrays However, instead of storing object reference variables as pointers to its children (and perhaps its parent), each node would store the array index of each child (and perhaps its parent) This approach allows elements to be stored contiguously in the array so that space is not wasted However, this approach increases the overhead for deleting elements in the tree since either remaining elements will have to be shifted to maintain contiguity or a free-list will have to be maintained
  • 22. FIGURE 12.6 Simulated link strategy for array implementation of trees
  • 23. Analysis of Trees Trees are a useful and efficient way to implement other collections In our analysis of list implementations in Chapter 8, we described the find operation as expected case n/2 or O(n) However, if we implemented an ordered list using a balanced binary search tree , a binary tree with the added property that the left child is always less than the parent which is always less than or equal to the right child, then we could improve the efficiency of the find operation to O(log n)
  • 24. Analysis of Trees This is due to the fact that the height or order of such a tree will always be log 2 n where n is the number of elements in the tree This is very similar to our discussion of binary search in Chapter 11 In fact, for any balanced N-ary tree with n elements, the tree’s height will be log N n With the added ordering property of a binary search tree, you are guaranteed to at worst search one path from the root to a leaf
  • 25. Tree Traversals There are four basic algorithms for traversing a tree: Preorder traversal Inorder traversal Postorder traversal Levelorder traversal
  • 26. Preorder traversal Preorder traversal is accomplished by visiting each node, followed by its children, starting with the root Given the complete binary tree on the next slide, a preorder traversal would produce the order: A B D E C
  • 27. FIGURE 12.7 A complete tree
  • 28. Preorder traversal Stated in pseudocode, the algorithm for a preorder traversal of a binary tree is: Visit node Traverse(left child) Traverse(right child)
  • 29. Inorder traversal Inorder traversal is accomplished by visiting the left child of the node, then the node, then any remaining child nodes starting with the root An inorder traversal of the previous tree produces the order: D B E A C
  • 30. Inorder traversal Stated in pseudocode, the algorithm for an inorder traversal of a binary tree is: Traverse(left child) Visit node Traverse(right child)
  • 31. Postorder traversal Postorder traversal is accomplished by visiting the children, then the node starting with the root Given the same tree, a postorder traversal produces the following order: D E B C A
  • 32. Postorder traversal Stated in pseudocode, the algorithm for a postorder traversal of a binary tree is: Traverse(left child) Traverse(right child) Visit node
  • 33. Levelorder traversal Levelorder traversal is accomplished by visiting all of the nodes at each level, one level at at time, starting with the root Given the same tree, a levelorder traversal produces the order: A B C D E
  • 34. Levelorder traversal Stated in pseudocode, the algorithm for a preorder traversal of a binary tree is:
  • 35. Implementing Binary Trees As an example of possible implementations of trees, lets explore a simple implementation of a binary tree Having specified that we are implementing a binary tree, we can identify a set of possible operations that would be common for all binary trees Notice however, that other than the constructors, none of these operations add any elements to the tree It is not possible to define an operation to add an element to the tree until we know more about how the tree is to be used
  • 36. FIGURE 12.8 The operations on a binary tree
  • 37. FIGURE 12.9 UML description of the BinaryTreeADT interface
  • 39. Listing 12.1 (cont.)
  • 40. Listing 12.1 (cont.)
  • 41. LinkedBinaryTree Lets examine a linked implementation of a binary tree Our implementation will need to keep track of the node that is the root of the tree as well as the count of elements in the tree protected int count; protected BinaryTreeNode<T> root;
  • 42. LinkedBinaryTree We will provide three constructors One to create a null tree One to create a tree containing a single element One to create a new tree with the given element at the root and combining two existing trees
  • 45. The BinaryTreeNode Class We will also need a class to represent each node in the tree Since this is a binary tree, we will create a BinaryTreeNode class that contain a reference to the element stored in the node as well as references for each of the children
  • 47. Listing 12.2 (cont.)
  • 48. Listing 12.2 (cont.)
  • 49. The LinkedBinaryTree Class Lets examine some of the methods of the LinkedBinaryTree Class Keep in mind that each node of a tree represents a sub-tree
  • 51. The find and findagain Methods The find method provides an excellent example of the recursion that is possible given the nature of a tree We use the private method findagain to support the public find method This allows us to distinguish between the original invocation of the method and the recursive calls
  • 54. The iteratorInOrder Method Like the find method, the iteratorInOrder method uses a private method, inorder, to support recursion The traversals for a tree might be implemented as toString methods or iterators or both
  • 57. Expression Trees Now lets look at an example using a binary tree In Chapter 6, we used a stack to evaluate postfix expressions Now we modify that algorithm to construct an expression tree
  • 58. FIGURE 12.10 An example expression tree
  • 59. Expression Trees Our expression tree class will extend our LinkedBinaryTree class This class provides constructors that reference the constructors of the LinkedBinaryTree class The class also provides an evaluate method to recursively evaluate an expression tree once it has been constructed The ExpressionTreeObj class represents the expression tree objects to be stored in the binary tree
  • 61. Listing 12.3 (cont.)
  • 62. Listing 12.3 (cont.)
  • 63. Listing 12.3 (cont.)
  • 64. Listing 12.3 (cont.)
  • 66. Listing 12.4 (cont.)
  • 67. Listing 12.4 (cont.)
  • 68. Listing 12.4 (cont.)
  • 69. FIGURE 12.11 Building an Expression Tree from a postfix expression
  • 70. The Postfix and PostfixEvaluator Classes The Postfix and PostfixEvaluator classes are modifications of those presented in chapter 6 The solve method of the PostfixEvaluator class uses a pair of stacks to create an expression tree from a valid post-fix expression It then outputs the result using the evaluate method of the ExpressionTree class
  • 73. Listing 12.6 (cont.)
  • 74. Listing 12.6 (cont.)
  • 75. Listing 12.6 (cont.)
  • 76. Listing 12.6 (cont.)
  • 77. Listing 12.6 (cont.)
  • 78. FIGURE 12.12 UML description of the Postfix example

Editor's Notes

  • #3: This chapter begins our exploration of non-linear data structures and collections. We will discuss the use and implementation of trees, definitions of terms associated with trees, and the analysis of tree implementations of collections we have already discussed. Chapter Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Discuss the analysis of tree implementations of collections
  • #4: A Tree is a non-linear structure defined by the concept that each node in the tree, other than the first node or root node, has exactly one parent. After our earlier discussions, one might immediately ask whether a Tree is a data structure or a collection. In general, a tree is a data structure used to implement collections. Unlike lists where we were able to abstract a set of operations that were common for all lists and then define three specific list collections, it is virtually impossible to define a common set of operations for all trees. For trees, the operations are dependent upon the type of tree and it’s use. Building upon our earlier definitions, a tree can be said to be an abstract data structure because it is a data structure that is not inherently defined within a programming language. Even once we know the specific type and structure of a tree, in most cases, as we will discuss in the next several chapters, a tree is still a data structure used to implement other types of collections. In chapter 17 we will discuss a collection called a hierarchy that is a special kind of tree.
  • #5: In order to discuss trees, we must first have a common vocabulary. We have already introduced a couple of terms: node which refers to a location in the tree where an element is stored, and root which refers to the node at the base of the tree or the one node in the tree that does not have a parent. Trees, in this context, are generally viewed upside down with the root at the top.
  • #6: Each node of the tree points to the nodes that are directly beneath it in the tree. These nodes are referred to as its children . A child of a child is then called a grandchild , a child of a grandchild called a great-grandchild , and so on. A node that does not have children is called a leaf . A node that is not the root and has at least one child is called an internal node .
  • #8: Any node below another node and on a path from that node is called a descendant of that node. Any node above another node with a connecting path to that node is called an ancestor of that node. All children of the same node are called siblings .
  • #9: Each node of the tree is at a specific level or depth within the tree. The level of a node is the length of the path from the root to the node. This pathlength is determined by counting the number of links that must be followed to get from the root to the node. The root is considered to be level 0, the children of the root are at level 1, the grandchildren of the root are at level 2, and so on.
  • #10: The height or order of a tree is the length of the longest path from the root to a leaf. Thus the height or order of the tree in Figure 10.3 is 3. The path from the root (A) to leaf (F) is of length 3. The path from the root (A) to leaf (C) is of length 1.
  • #12: A tree is considered to be balanced if all of the leaves of the tree are at roughly the same depth. While the use of the term “roughly” may not be intellectually satisfying, the actual definition is dependent upon the algorithm being used. Some algorithms define balanced as all of the leaves being at level h or h-1 where h is the height of the tree. Other algorithms use much less restrictive definitions.
  • #14: The concept of a complete tree is related to the balance of a tree. A tree is considered complete if it is balanced and all of the leaves at level h are on the left side of the tree. While a seemingly arbitrary concept, as we will discuss in later chapters, this definition has implications for how the tree is stored in certain implementations.
  • #16: While it is not possible to discuss the details of an implementation of a tree without defining the type of tree and its use, we can look at general strategies for implementing trees. The most obvious implementation of tree is a linked structure. Each node could be defined as a TreeNode class, as we did with the LinearNode class for linked lists. Each node would contain a pointer to the element to be stored in that node as well as pointers for each of the possible children of the node. Depending on the implementation, it may also be useful to store a pointer in each node to its parent. The strategies for array implementations of a tree may be less obvious. There are two principle approaches: a computational strategy and a simulated link strategy.
  • #17: While it is not possible to discuss the details of an implementation of a tree without defining the type of tree and its use, we can look at general strategies for implementing trees. The most obvious implementation of tree is a linked structure. Each node could be defined as a TreeNode class, as we did with the LinearNode class for linked lists. Each node would contain a pointer to the element to be stored in that node as well as pointers for each of the possible children of the node. Depending on the implementation, it may also be useful to store a pointer in each node to its parent. The strategies for array implementations of a tree may be less obvious. There are two principle approaches: a computational strategy and a simulated link strategy.
  • #18: For certain types of trees, specifically binary trees, a computational strategy can be used for storing a tree using an array. For any element stored in position n of the array, that elements left child will be stored in position (2*n) and that elements right child will be stored in position (2*n+1). This strategy is very effective and can be managed in terms of capacity in much the same way that we managed capacity for the array implementations of lists, queues, and stacks. However, despite the conceptual elegance of this solution, it is not without drawbacks. For example, if the tree that we are storing is not complete or relatively complete, we may be wasting large amounts of memory allocated in the array for positions of the tree that do not contain data.
  • #19: For certain types of trees, specifically binary trees, a computational strategy can be used for storing a tree using an array. For any element stored in position n of the array, that elements left child will be stored in position (2*n) and that elements right child will be stored in position (2*n+1). This strategy is very effective and can be managed in terms of capacity in much the same way that we managed capacity for the array implementations of lists, queues, and stacks. However, despite the conceptual elegance of this solution, it is not without drawbacks. For example, if the tree that we are storing is not complete or relatively complete, we may be wasting large amounts of memory allocated in the array for positions of the tree that do not contain data.
  • #21: A second possible array implementation of trees is modeled after the way operating systems manage memory. Instead of assigning elements of the tree to array position by location in the tree, array positions are allocated contiguously on a first come first served basis. Each element of the array will be a node class similar to the TreeNode class that we discussed earlier. However, instead of storing object reference variables as pointers to its children (and perhaps its parent), each node would store the array index of each child (and perhaps its parent). This approach allows elements to be stored contiguously in the array so that space is not wasted. However, this approach increases the overhead for deleting elements in the tree since either remaining elements will have to be shifted to maintain contiguity or a freelist will have to be maintained.
  • #22: A second possible array implementation of trees is modeled after the way operating systems manage memory. Instead of assigning elements of the tree to array position by location in the tree, array positions are allocated contiguously on a first come first served basis. Each element of the array will be a node class similar to the TreeNode class that we discussed earlier. However, instead of storing object reference variables as pointers to its children (and perhaps its parent), each node would store the array index of each child (and perhaps its parent). This approach allows elements to be stored contiguously in the array so that space is not wasted. However, this approach increases the overhead for deleting elements in the tree since either remaining elements will have to be shifted to maintain contiguity or a freelist will have to be maintained. This same strategy can also be used when tree structures need to be stored directly on disk using a direct io approach. In this case, rather than using an array index as a pointer, each node will store the relative position in the file of its children so that an offset can be calculated given the base address of the file.
  • #24: As we discussed earlier, trees are a useful and efficient way to implement other collections. Let’s take an ordered list for example. In our analysis of list implementation in Chapter 9, we described the find operation as expected case n/2 or O(n). However, if we implemented an ordered list using a balanced binary search tree , a binary tree with the added property that the left child is always less than the parent which is always less than the right child, then we could improve the efficiency of the find operation to O(log n). This is due to the fact that the height or order of such a tree will always be log2n where n is the number of elements in the tree. This is very similar to our discussion of binary search in Chapter 5. In fact, for any balanced N-ary tree with n elements, the tree’s height will be logNn. With the added ordering property of a binary searched tree, you are guaranteed to at worst search one path from the root to a leaf.
  • #25: As we discussed earlier, trees are a useful and efficient way to implement other collections. Let’s take an ordered list for example. In our analysis of list implementation in Chapter 9, we described the find operation as expected case n/2 or O(n). However, if we implemented an ordered list using a balanced binary search tree , a binary tree with the added property that the left child is always less than the parent which is always less than the right child, then we could improve the efficiency of the find operation to O(log n). This is due to the fact that the height or order of such a tree will always be log 2 n where n is the number of elements in the tree. This is very similar to our discussion of binary search in Chapter 5. In fact, for any balanced N-ary tree with n elements, the tree’s height will be log N n. With the added ordering property of a binary searched tree, you are guaranteed to at worst search one path from the root to a leaf.