There are two types of binary search tree traversals, depth-first traversal and level-order traversal.
There are three types of depth-first traversal: preorder tree walk, inorder tree walk, and postorder tree walk, respectively:
- 1. Preorder traversal: visit the current node first, and then recursively visit the left and right subtrees in turn.
- 2. In-order traversal : first recursively visit the left subtree, then visit itself, and then recursively visit the right subtree.
- 3. Post-order traversal : first recursively visit the left and right subtrees, and then visit its own node.
Preorder traversal results diagram:
Corresponding code example:
...
// Preorder traversal of the binary search tree rooted at node, recursive algorithm
private void preOrder(Node node){ if( node != null ){ System.out.println(node.key); preOrder (node. left); preOrder(node. right); } } ...
In-order traversal results diagram:
Corresponding code example:
...
// Inorder traversal of the binary search tree rooted at node, recursive algorithm
private void inOrder(Node node){ if( node != null ){ inOrder(node.left); System.out.println (node. key); inOrder(node. right); } } ...
Post-order traversal result illustration:
Corresponding code example:
...
// Post-order traversal of the binary search tree rooted at node, recursive algorithm
private void postOrder(Node node){ if( node != null ){ postOrder(node.left); postOrder(node.right ); System.out.println(node.key); } } ...
Java example code
src/runoob/binary/Traverse.java file code:
package runoob.binary;
/**
* Priority traversal
*/
public class Traverse<Key extends Comparable<Key>, Value> { // The nodes in the tree are private classes, and the outside world does not need to know the specific implementation of binary search tree nodes private class Node { private Key key; private Value value; private Node left, right; public Node(Key key, Value value) { this.key = key; this.value = value; left = right = null; } } private Node root ; // Root node private int count; // The number of nodes in the tree type // Constructor, construct an empty binary search tree by default public Traverse() { root = null;
count = 0;
}
// returns the number of nodes in the binary search tree
public int size() { return count; } // returns whether the binary search tree is empty public boolean isEmpty() { return count == 0; } // to Insert a new (key, value) data pair into the binary search tree public void insert(Key key, Value value){ root = insert(root, key, value); } // Check whether the key key exists in the binary search tree public boolean contain(Key key){ return contain(root, key); } // Search for the value corresponding to the key key in the binary search tree. If the value does not exist, return null public Value search(Key key){ return search( root , key ); }
// Preorder traversal of binary search tree
public void preOrder(){ preOrder(root); } // Inorder traversal of binary search tree public void inOrder(){ inOrder(root); } // Postorder of binary search tree Traverse public void postOrder(){ postOrder(root); } //********************** //* Auxiliary function of binary search tree //**** ***************** // Insert a node (key, value) into the binary search tree rooted at node, and use a recursive algorithm // to return the binary search after inserting the new node The root of the tree private Node insert(Node node, Key key, Value value){ if( node == null ){ count ++; return new Node(key, value); }
if( key.compareTo(node.key) == 0 )
node.value = value;
else if( key.compareTo(node.key) < 0 )
node.left = insert( node.left , key, value);
else // key > node->key
node.right = insert( node.right, key, value);
return node;
}
// Check whether the binary search tree rooted at node contains a node whose key is key, using recursion Algorithm
private boolean contain(Node node, Key key){ if( node == null ) return false; if( key.compareTo(node.key) == 0 ) return true; else if( key.compareTo(node.key) < 0 ) return contain( node. left , key );
else // key > node->key
return contain( node.right , key );
}
// Find the value corresponding to the key in the binary search tree rooted at node, recursive algorithm
// If the value does not exist, return NULL
private Value search(Node node, Key key){ if( node == null ) return null; if( key.compareTo(node.key) == 0 ) return node.value; else if( key.compareTo(node. key) < 0 ) return search( node.left , key ); else // key > node->key return search( node.right, key ); } // Preorder the binary search tree rooted at node Traversal, recursive algorithm private void preOrder(Node node){ if( node != null ){
System.out.println(node.key);
preOrder(node.left);
preOrder(node.right);
}
}
// Inorder traversal of the binary search tree rooted at node, recursive algorithm
private void inOrder( Node node){ if( node != null ){ inOrder(node.left); System.out.println(node.key); inOrder(node.right); } } // binary search rooted at node The tree performs post-order traversal, recursive algorithm private void postOrder(Node node){ if( node != null ){ postOrder(node.left); postOrder(node.right); System.out.println(node.key); }
}
}