Binary search tree level order traversal

The layer-order traversal of the binary search tree, that is, traversal layer by layer, that is, the nodes of each layer are stored in the queue, and then the operations of dequeue (take out nodes) and queue (store nodes in the next layer) are performed to achieve traversal the goal of.

Support layer-order traversal by introducing a queue:

  • If the root node is empty, there is no traversal;

  • If the root node is not empty:

    • Put the root node into the queue first;

    • As long as the queue is not empty:

      • Dequeue the first node of the team and traverse;
      • If the head node of the team has a left child, add the left child to the team;
      • If the head node of the team has a right child, add the right child to the team;

The following steps are demonstrated in turn:

(1) First take out the root node and put it into the queue

(2) Take out 29, the left and right child nodes join the team

(3) Team head 17 leaves the team, and child nodes 14 and 23 join the team.

(4) 31 is dequeued, child nodes 30 and 43 are enqueued

(5) Finally all out of the team

Core code example:

...
// Level order traversal of binary search tree
public void levelOrder(){     // We use LinkedList as our queue     LinkedList<Node> q = new LinkedList<Node>();     q.add(root);     while ( !q.isEmpty() ){         Node node = q.remove();         System.out.println(node.key);         if( node.left != null )             q.add( node.left );         if( node .right != null )             q.add( node.right );     } } ...
















Java example code

src/runoob/binary/LevelTraverse.java file code:

package runoob.binary;

import java.util.LinkedList;

/**
 * Level order traversal
 */
public class LevelTraverse<Key extends Comparable<Key>, Value>{     // The nodes in the tree are private classes, the outside world does not need to know The concrete realization of binary search tree node     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, an empty binary search tree is constructed by default     public LevelTraverse() {



















        root = null;
        count = 0;
    }

    // Return the number of nodes in the binary search tree
    public int size() {         return count;     }     // Return whether the binary search tree is empty     public boolean isEmpty() {         return count == 0;     }     // 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 binary search tree is There is a key key     public boolean contain(Key key){         return contain(root, key);     }     // Search 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 );





















    }

    // Pre-order traversal of binary search tree
    public void preOrder(){         preOrder(root);     }     // In-order traversal of binary search tree     public void inOrder(){         inOrder(root);     }     // Post-order of binary search tree Order traversal     public void postOrder(){         postOrder(root);     }     // Level order traversal of binary search tree     public void levelOrder(){         // We use LinkedList as our queue         LinkedList<Node> q = new LinkedList<Node> ();         q.add(root);         while( !q.isEmpty() ){             Node node = q.remove();             System.out.println(node.key);             if( node.left != null )


























                q.add( node.left );
            if( node.right != null )
                q.add( node.right );
        }
    }

    //****************** *
    //* Auxiliary function of binary search tree
    //********************

    // Insert node (key, value), using a recursive algorithm
    // return the root of the binary search tree after inserting a new node
    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;
    }

    // View rooted at node Whether the binary search tree contains a node whose key value is key, use the recursive 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 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 );     }     // for node Preorder traversal of the root binary search tree, 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);         }     }     // 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);         }     } }

















   

Guess you like

Origin blog.csdn.net/unbelievevc/article/details/131320490