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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access