树状数据结构是数据结构中较为重要的一个章节。其中关于树状结构的遍历是该章节的核心,也是后面相关内容的基础。自己准备结合相关博主的一些资料,并结合自己在leecote上做题的心得出一期关于树状数据结构的遍历的总结。希望能够给大家带来帮助。
(一):二叉树的遍历
1.1:二叉树的数据结构的定义
public class BiTreeNode {
private Object data;//数据域
private BiTreeNode lchild;//左孩子
private BiTreeNode rchild;//右孩子
public BiTreeNode() {
this.data=0;
this.lchild=null;
this.rchild=null;
}
public BiTreeNode(Object data) {
this.data=data;
this.lchild=null;
this.rchild=null;
}
public BiTreeNode(Object data,BiTreeNode lchild,BiTreeNode rchild) {
this.data=data;
this.lchild=lchild;
this.rchild=rchild;
}
1.2:二叉树的递归遍历
二叉树的前序遍历
public void preRootSearch(BiTreeNode root) {
if(root!=null) {
System.out.print(root.getData());
preRootSearch(root.getLchild());
preRootSearch(root.getRchild());
}
}
二叉树的中序遍历
public void inRootSearch(BiTreeNode root) {
if(root!=null) {
inRootSearch(root.getLchild());
System.out.print(root.getData());
inRootSearch(root.getRchild());
}
}
二叉树的后序遍历
public void postRootSearch(BiTreeNode root) {
if(root!=null) {
postRootSearch(root.getLchild());
postRootSearch(root.getRchild());
System.out.print(root.getData());
}
}
1.3:二叉树的非递归遍历
二叉树的前序遍历
public void preBitreeSearch_1(BiTreeNode root) {
BiTreeNode t=root;
stack st=new stack();
if(root!=null) {
st.push(t);
}
while(!st.isEmpty()) {
t=(BiTreeNode) st.pop();
while(t!=null) {
System.out.print(t.getData());
if(t.getRchild()!=null) {
st.push(t.getRchild());
}
t=t.getLchild();
}
}
}
二叉树的中序遍历
public void inBitreeSearch_1(BiTreeNode root) {
BiTreeNode t=root;
stack st=new stack();
if(t!=null) {
st.push(t);
}
while(!st.isEmpty()) {
while(st.peek()!=null) {
st.push(((BiTreeNode) st.peek()).getLchild());
}
st.pop();
if(!st.isEmpty()) {
t=(BiTreeNode) st.pop();
System.out.print(t.getData());
st.push(t.getRchild());
}
}
}
二叉树的后序遍历
public void postBiTreeSearch_1(BiTreeNode root) {
if(root==null) {
return;
}else {
stack st=new stack();
BiTreeNode t=root;
st.push(root);
BiTreeNode p=null;
boolean flag=true;
while(!st.isEmpty()) {
while(st.peek()!=null) {
st.push(((BiTreeNode)st.peek()).getLchild());
}
st.pop();
t=(BiTreeNode)st.peek();
if(!st.isEmpty()) {
if(t.getRchild()==null||t.getRchild()==p) {
t=(BiTreeNode)st.pop();
System.out.print(t.getData());
p=t;
flag=true;
}else {
st.push(t.getRchild());
flag=false;
}
if(!flag) {
break;
}
}
}
}
}
以上的内容多为千篇一律,没有什么新意。下面介绍一下自己刷题过程中遇到的关于N叉树的遍历的问题。感兴趣
的同学可以看一下。
2.1:N叉树的定义
public class BiTreeNode {
private Object data;
private LinkedList<BiTreeNode> children;
}
根据其数据结构的描述我们可以看到,其用于超过2个子节点,因此为了方便起见,我们使用一个链表将其进行
存储。
2.2:N叉树的递归遍历
先序遍历
class Solution {
List<Integer> array=new LinkedList<Integer>();
public List<Integer> preorder(Node root) {
if(root!=null){
array.add(root.val);
for(Node i:root.children){
preorder(i);
}
}
return array;
}
}
后序遍历
class Solution {
List<Integer> array=new LinkedList<Integer>();
public List<Integer> postorder(Node root) {
if(root!=null){
for(Node i:root.children){
preorder(i);
}
array.add(root.val);
}
return array;
}
}
2.3:N叉树的迭代遍历
先序遍历
class Solution {
public List<Integer> preorder(Node root) {
//非递归版
List<Integer> res = new ArrayList<Integer>();
Stack<Node> stack = new Stack<Node>();
if(root == null)
return res;
stack.push(root);
while(!stack.isEmpty())
{
Node node = stack.pop();
res.add (node.val);
for(int i = node.children.size()-1;i>= 0;i--)
{
stack.add(node.children.get(i));
}
}
return res;
}
}
后序遍历
//迭代实现:直接反转先序结果即可
class Solution {
public List<Integer> postorder(Node root) {
List<Integer> res_pre = new ArrayList<>();
if(root == null)
return res_pre;
Stack<Node> s = new Stack<>();
s.push(root);
while(!s.isEmpty()){
Node n = s.pop();
res_pre.add(n.val);
for(Node node : n.children)
s.push(node);
}
Collections.reverse(res_pre);
return res_pre;
}
}