JAVA叉树递归非递归遍历用法.doc
文本预览下载声明
JAVA二叉树递归和非递归遍历
1、递归遍历
[java]// 先序遍历(递归实现)------------------------------------------------------------- /* 1. Visit the node. 1. Call itself to traverse the node’s left subtree. 3. Call itself to traverse the node’s right subtree. 4. base case: there is no node */ private void preOrder(Node localRoot){ if(localRoot != null) { visit(localRoot); // System.out.print(localRoot.iData + ); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); } } // 中序遍历(递归实现)------------------------------------------------------------- private void inOrder(Node localRoot){ if(localRoot != null) { inOrder(localRoot.leftChild); visit(localRoot); //System.out.print(localRoot.iData + ); inOrder(localRoot.rightChild); } } // 后序遍历(递归实现)------------------------------------------------------------- private void postOrder(Node localRoot){ if(localRoot != null) { postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); visit(localRoot); //System.out.print(localRoot.iData + ); } } // -------------------------------------------------------------
// 先序遍历(递归实现)------------------------------------------------------------- /* 1. Visit the node. 1. Call itself to traverse the node’s left subtree. 3. Call itself to traverse the node’s right subtree. 4. base case: there is no node */ private void preOrder(Node localRoot){ if(localRoot != null) { visit(localRoot); // System.out.print(localRoot.iData + ); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); } }// 中序遍历(递归实现)------------------------------------------------------------- private void inOrder(Node localRoot){ if(localRoot != null) { inO
显示全部