SlideShare a Scribd company logo
1 of 78
Chapter 12 Trees
Chapter Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Trees ,[object Object],[object Object]
Definitions ,[object Object],[object Object],[object Object],[object Object]
Definitions ,[object Object],[object Object],[object Object],[object Object],[object Object]
FIGURE 12.1   Tree terminology
Definitions ,[object Object],[object Object],[object Object],[object Object]
Definitions ,[object Object],[object Object],[object Object],[object Object]
Definitions ,[object Object],[object Object],[object Object],[object Object]
FIGURE 12.2   Path length and level
Definitions ,[object Object],[object Object],[object Object]
FIGURE 12.3   Balanced  and unbalanced trees
Definitions ,[object Object],[object Object],[object Object],[object Object]
FIGURE 12.4   Some complete trees
Implementing Trees with Links ,[object Object],[object Object],[object Object]
Implementing Trees with Links ,[object Object],[object Object]
Implementing Trees with Arrays ,[object Object],[object Object]
Implementing Trees with Arrays ,[object Object],[object Object],[object Object]
FIGURE 12.5   Computational strategy  for array implementation of trees
Implementing Trees with Arrays ,[object Object],[object Object],[object Object]
Implementing Trees with Arrays ,[object Object],[object Object],[object Object]
FIGURE 12.6   Simulated link strategy for array implementation of trees
Analysis of Trees ,[object Object],[object Object],[object Object]
Analysis of Trees ,[object Object],[object Object],[object Object],[object Object]
Tree Traversals ,[object Object],[object Object],[object Object],[object Object],[object Object]
Preorder traversal ,[object Object],[object Object],[object Object]
FIGURE 12.7   A complete tree
Preorder traversal ,[object Object],[object Object],[object Object],[object Object]
Inorder traversal ,[object Object],[object Object],[object Object]
Inorder traversal ,[object Object],[object Object],[object Object],[object Object]
Postorder traversal ,[object Object],[object Object],[object Object]
Postorder traversal ,[object Object],[object Object],[object Object],[object Object]
Levelorder traversal ,[object Object],[object Object],[object Object]
Levelorder traversal ,[object Object]
Implementing Binary Trees ,[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object],[object Object]
LinkedBinaryTree ,[object Object],[object Object],[object Object],[object Object]
LinkedBinaryTree - constructors
LinkedBinaryTree - constructors
The BinaryTreeNode Class ,[object Object],[object Object]
Listing 12.2
Listing 12.2  (cont.)
Listing 12.2  (cont.)
The LinkedBinaryTree Class ,[object Object],[object Object]
The removeLeftSubtree Method
The find and findagain Methods ,[object Object],[object Object],[object Object]
The find Method
The findagain Method
The iteratorInOrder Method ,[object Object],[object Object]
The iteratorInOrder Method
The inorder Method
Expression Trees ,[object Object],[object Object],[object Object]
FIGURE 12.10   An example expression tree
Expression Trees ,[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object]
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

Viewers also liked

Viewers also liked (11)

Rtai
RtaiRtai
Rtai
 
Digital Communication Principle
Digital Communication PrincipleDigital Communication Principle
Digital Communication Principle
 
Computer Network - OSI model
Computer Network - OSI modelComputer Network - OSI model
Computer Network - OSI model
 
RADIO FREQUENCY COMMUNICATION SYSTEMS, ANTENNA THEORY AND MICROWAVE DEVICES
RADIO FREQUENCY COMMUNICATION SYSTEMS, ANTENNA THEORY AND MICROWAVE DEVICESRADIO FREQUENCY COMMUNICATION SYSTEMS, ANTENNA THEORY AND MICROWAVE DEVICES
RADIO FREQUENCY COMMUNICATION SYSTEMS, ANTENNA THEORY AND MICROWAVE DEVICES
 
Photomask Fabrication
Photomask FabricationPhotomask Fabrication
Photomask Fabrication
 
Computational Logic
Computational LogicComputational Logic
Computational Logic
 
MSc_thesis
MSc_thesisMSc_thesis
MSc_thesis
 
Computer networks--osi model
Computer networks--osi modelComputer networks--osi model
Computer networks--osi model
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 
Chapter 4 - Digital Transmission
Chapter 4 - Digital TransmissionChapter 4 - Digital Transmission
Chapter 4 - Digital Transmission
 
Chapter 3 - Data and Signals
Chapter 3 - Data and SignalsChapter 3 - Data and Signals
Chapter 3 - Data and Signals
 

Similar to Ch12 Tree

Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Javageeksrik
 
Trees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsTrees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsBHARATH KUMAR
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data StructureOm Prakash
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxRajitha Reddy Alugati
 
data structures and algorithms Unit 2
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2infanciaj
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationnakulvarshney371
 
Tree data structure in java
Tree data structure in javaTree data structure in java
Tree data structure in javaIrfan CH
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binarySSE_AndyLi
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptxAbirami A
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptxRahulAI
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structuresASairamSairam1
 

Similar to Ch12 Tree (20)

Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Java
 
Trees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and AlgorithmsTrees and Graphs in data structures and Algorithms
Trees and Graphs in data structures and Algorithms
 
Lecture 5 trees
Lecture 5 treesLecture 5 trees
Lecture 5 trees
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
 
Trees
TreesTrees
Trees
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Tree
TreeTree
Tree
 
Trees
TreesTrees
Trees
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
data structures and algorithms Unit 2
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
 
Tree data structure in java
Tree data structure in javaTree data structure in java
Tree data structure in java
 
Tree
TreeTree
Tree
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptx
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structures
 

More from leminhvuong

More from leminhvuong (20)

Proxy
ProxyProxy
Proxy
 
Lession2 Xinetd
Lession2 XinetdLession2 Xinetd
Lession2 Xinetd
 
Module 7 Sql Injection
Module 7   Sql InjectionModule 7   Sql Injection
Module 7 Sql Injection
 
Iptables
IptablesIptables
Iptables
 
Lession1 Linux Preview
Lession1 Linux PreviewLession1 Linux Preview
Lession1 Linux Preview
 
Http
HttpHttp
Http
 
Dns
DnsDns
Dns
 
Net Admin Intro
Net Admin IntroNet Admin Intro
Net Admin Intro
 
Lession4 Dhcp
Lession4 DhcpLession4 Dhcp
Lession4 Dhcp
 
Lession3 Routing
Lession3 RoutingLession3 Routing
Lession3 Routing
 
Module 1 Introduction
Module 1   IntroductionModule 1   Introduction
Module 1 Introduction
 
Wire Less
Wire LessWire Less
Wire Less
 
Net Security Intro
Net Security IntroNet Security Intro
Net Security Intro
 
Module 10 Physical Security
Module 10   Physical SecurityModule 10   Physical Security
Module 10 Physical Security
 
Module 9 Dos
Module 9   DosModule 9   Dos
Module 9 Dos
 
Module 8 System Hacking
Module 8   System HackingModule 8   System Hacking
Module 8 System Hacking
 
Module 6 Session Hijacking
Module 6   Session HijackingModule 6   Session Hijacking
Module 6 Session Hijacking
 
Module 5 Sniffers
Module 5  SniffersModule 5  Sniffers
Module 5 Sniffers
 
Module 4 Enumeration
Module 4   EnumerationModule 4   Enumeration
Module 4 Enumeration
 
Module 3 Scanning
Module 3   ScanningModule 3   Scanning
Module 3 Scanning
 

Recently uploaded

JAYNAGAR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
JAYNAGAR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLJAYNAGAR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
JAYNAGAR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLkapoorjyoti4444
 
Business Model Canvas (BMC)- A new venture concept
Business Model Canvas (BMC)-  A new venture conceptBusiness Model Canvas (BMC)-  A new venture concept
Business Model Canvas (BMC)- A new venture conceptP&CO
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with CultureSeta Wicaksana
 
RSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataRSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataExhibitors Data
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon investment
 
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...Sheetaleventcompany
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxWorkforce Group
 
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Anamikakaur10
 
Phases of Negotiation .pptx
 Phases of Negotiation .pptx Phases of Negotiation .pptx
Phases of Negotiation .pptxnandhinijagan9867
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableSeo
 
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...allensay1
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPanhandleOilandGas
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayNZSG
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...amitlee9823
 
Call Girls In Noida 959961⊹3876 Independent Escort Service Noida
Call Girls In Noida 959961⊹3876 Independent Escort Service NoidaCall Girls In Noida 959961⊹3876 Independent Escort Service Noida
Call Girls In Noida 959961⊹3876 Independent Escort Service Noidadlhescort
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...Aggregage
 
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai KuwaitThe Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwaitdaisycvs
 
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...amitlee9823
 

Recently uploaded (20)

JAYNAGAR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
JAYNAGAR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLJAYNAGAR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
JAYNAGAR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
 
Business Model Canvas (BMC)- A new venture concept
Business Model Canvas (BMC)-  A new venture conceptBusiness Model Canvas (BMC)-  A new venture concept
Business Model Canvas (BMC)- A new venture concept
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
RSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataRSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors Data
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business Growth
 
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptx
 
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
 
Phases of Negotiation .pptx
 Phases of Negotiation .pptx Phases of Negotiation .pptx
Phases of Negotiation .pptx
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation Final
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
 
Call Girls In Noida 959961⊹3876 Independent Escort Service Noida
Call Girls In Noida 959961⊹3876 Independent Escort Service NoidaCall Girls In Noida 959961⊹3876 Independent Escort Service Noida
Call Girls In Noida 959961⊹3876 Independent Escort Service Noida
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
 
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai KuwaitThe Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
 
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
 

Ch12 Tree

Editor's Notes

  1. 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
  2. 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.
  3. 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.
  4. 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 .
  5. 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 .
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. 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.
  11. 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.
  12. 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.
  13. 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.
  14. 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.
  15. 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.
  16. 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.
  17. 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.