tree 先序遍历 叶子结点_二叉树的先序创建,先序,中序,后序的递归与非递归遍历,层次遍历,叶子结点数及树的深度...

本文介绍了如何使用Java实现二叉树的先序、中序、后序以及层次遍历,并提供了计算二叉树深度和叶子节点数量的方法。通过递归和非递归方式展示了不同遍历方法的代码实现。
摘要由CSDN通过智能技术生成

二叉树的先序创建,先序,中序,后序的递归与非递归遍历,层次遍历,叶子结点数及树的深度计算

输入格式:如   abd###ce##f##*

package tree;

//二叉树的二叉链表实现

import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;

import java.util.Stack;

public class BTree {

BTNode rootNode=new BTNode();

class BTNode{

char data;

BTNode leftChildNode;

BTNode rightChildNode;

public BTNode(){

data=0;

leftChildNode=rightChildNode=null;

}

public BTNode(char data){

this.data=data;

leftChildNode=rightChildNode=null;

}

public BTNode(char data,BTNode leftChildNode,BTNode rightChildNode){

this.data=data;

leftChildNode=leftChildNode;

rightChildNode=rightChildNode;

}

}

//先序创建二叉树

char d[]=new char[100];

int i=0;

public BTNode creatBTree(){

BTNode node=null;

if(d[i]!='*'){

if(d[i]=='#'){

node=null;

i++;

}

else{

node=new BTNode(d[i]);

i++;

node.leftChildNode=creatBTree();

node.rightChildNode=creatBTree();

}

}

return node;

}

//先序递归遍历

public void preOrder(BTNode t){

if(t!=null){

System.out.print(t.data);

preOrder(t.leftChildNode);

preOrder(t.rightChildNode);

}

}

//先序非递归遍历

public void preStackOrder(BTNode t){

Stack s=new Stack();

BTNode p=t;

while(p!=null&&s.isEmpty()!=true){

if(p!=null){

System.out.print(p.data);

s.push(p);

p=p.leftChildNode;

}

if(s.isEmpty()){

s.pop();

p=p.rightChildNode;

}

}

}

//中序递归遍历

public void inOrder(BTNode t){

if(t!=null){

inOrder(t.leftChildNode);

System.out.print(t.data);

inOrder(t.rightChildNode);

}

}

//中序非递归遍历

public void inStackOrder(BTNode t){

Stack s=new Stack();

BTNode p=t;

while(p!=null&&s.isEmpty()){

if(p!=null){

s.push(p);

p=p.leftChildNode;

}

if(s.isEmpty()!=true){

p=s.pop();

System.out.print(p.data);

p=p.rightChildNode;

}

}

}

//后序递归遍历

public void postOrder(BTNode t){

if(t!=null){

postOrder(t.leftChildNode);

postOrder(t.rightChildNode);

System.out.print(t.data);

}

}

//后序非递归遍历

public void postStackOrder(BTNode t){

Stack s=new Stack();

Stack ss=new Stack();

Integer i=new Integer(1);

BTNode p=t;

BTNode q=t;

while(p!=null||s.isEmpty()!=true){

while(p!=null){

s.push(p);

ss.push(new Integer(0));

p=p.leftChildNode;

}

while(s.isEmpty()!=true&&ss.peek().equals(i)){

ss.pop();

q=s.pop();

System.out.print(q.data);

}

if(s.isEmpty()!=true){

ss.pop();

ss.push(i);

p=s.peek();

p=p.rightChildNode;

}

}

}

//层次非递归遍历

public void levelQueueOrder(BTNode t){

Queue q=new LinkedList();

q.add(t);

while(q.isEmpty()!=true){

BTNode step=q.remove();

System.out.print(step.data);

if(step.leftChildNode!=null){

q.add(step.leftChildNode);

}

if(step.rightChildNode!=null){

q.add(step.rightChildNode);

}

}

}

//计算二叉树深度

public int depth(BTNode t){

int leftDepth,rightDepth;

if(t==null)

return 0;

leftDepth=depth(t.leftChildNode);

rightDepth=depth(t.rightChildNode);

return Math.max(leftDepth,rightDepth)+1;

}

//叶子结点个数

int num=0;

public int leaf(BTNode t){

if(t!=null){

if(t.leftChildNode==null&&t.rightChildNode==null)

num++;

leaf(t.leftChildNode);

leaf(t.rightChildNode);

}

return num;

}

public static void main(String[] args) {

BTree bt=new BTree();

Scanner sc=new Scanner(System.in);

String a=sc.next();

char c[]=a.toCharArray();

for(int i=0;i

bt.d[i]=c[i];

}

bt.rootNode=bt.creatBTree();

System.out.println("先序遍历");

bt.preOrder(bt.rootNode);

System.out.println();

System.out.println("中序遍历");

bt.inOrder(bt.rootNode);

System.out.println();

System.out.println("后序遍历");

bt.postOrder(bt.rootNode);

System.out.println();

System.out.println("后序非递归遍历");

bt.postStackOrder(bt.rootNode);

System.out.println("层次遍历");

bt.levelQueueOrder(bt.rootNode);

System.out.println();

System.out.println("二叉树深度");

System.out.println(bt.depth(bt.rootNode));

System.out.println("叶子结点个数");

System.out.println(bt.leaf(bt.rootNode));

}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

好的,我可以为您提供一个Python实现的例子。如下: ```python class Node: def __init__(self, val): self.val = val self.left = None self.right = None def buildTree(preorder, inorder): if not preorder or not inorder: return None root = Node(preorder[0]) idx = inorder.index(root.val) root.left = buildTree(preorder[1:idx+1], inorder[:idx]) root.right = buildTree(preorder[idx+1:], inorder[idx+1:]) return root def preorderTraversal(root): if not root: return print(root.val, end=' ') preorderTraversal(root.left) preorderTraversal(root.right) def inorderTraversal(root): if not root: return inorderTraversal(root.left) print(root.val, end=' ') inorderTraversal(root.right) def postorderTraversal(root): if not root: return postorderTraversal(root.left) postorderTraversal(root.right) print(root.val, end=' ') def getLeafNodes(root): if not root: return [] elif not root.left and not root.right: return [root.val] else: return getLeafNodes(root.left) + getLeafNodes(root.right) preorder = [1, 2, 4, 5, 3, 6, 7] inorder = [4, 2, 5, 1, 6, 3, 7] root = buildTree(preorder, inorder) print('先序遍历:', end='') preorderTraversal(root) print() print('中序遍历:', end='') inorderTraversal(root) print() print('后序遍历:', end='') postorderTraversal(root) print() print('叶子节点:', getLeafNodes(root)) ``` 其中,我们定义了一个 `Node` 类作为二叉树的节点,包含节点值、左子节点和右子节点。`buildTree` 函数根据先序遍历中序遍历结果构建二叉树。`preorderTraversal`、`inorderTraversal` 和 `postorderTraversal` 分别表示先序遍历中序遍历后序遍历,通过递归实现。`getLeafNodes` 函数用于返回二叉树的叶子节点值。 你可以运行代码,看一下输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值