课程: Java Algorithms

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Implement tree traversals in Java

Implement tree traversals in Java - Java教程

课程: Java Algorithms

Implement tree traversals in Java

- [Instructor] In a tree traversal algorithm, each node in a tree is accessed in a particular order. In a pre-order traversal, we visit the root first. With the in-order traversal, we recursively visit the left tree, and then the root. With the postorder, we visit the root last. With each traversal, we apply the same pattern to parts of the tree, and then continue that pattern throughout the tree. The concept of recursion can become a very useful tool in this type of algorithm because we're doing the same operation with slight modifications to the inputs. Let's look at how we can apply it to tree traversal algorithms. We have some sample code here that we've carried over from previous videos with a few modifications. There is the node class, as well as the binary tree class. The binary tree class will hold our traversal. In the main function, we create a small binary tree. It's important to note that this is not a binary search tree, it does not meet the ordering constraint. It's only…

内容