Overview of Class Tree

Class Tree (Fig. 21.17, lines 45–114) requires its type argument to implement interface Comparable, so that each value inserted in the tree can be compared with the existing values to find the insertion point. The class has private field root (line 47)—a TreeNode reference to the root node of the tree. Tree’s constructor (lines 50–53) initializes root to null to indicate that the tree is empty. The class contains method insertNode (lines 56–62) to insert a new node in the tree and methods preorderTraversal (lines 65–68), inorderTraversal (lines 82–85) and postorderTraversal (lines 99–102) to begin traversals of the tree. Each of these methods calls a recursive utility method to perform the traversal operations on the ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.