二叉树的二叉链实现及遍历

建立如图的二叉树并遍历:
[img]http://dl.iteye.com/upload/attachment/0072/3033/114e5346-bf3e-3bf5-b696-7bfbf260a382.gif[/img]

import java.util.*;   

public class BinaryTree {
protected Node root;

public BinaryTree(Node root) {
this.root = root;
}

public Node getRoot() {
return root;
}

/** 构造树 */
public static Node init() {
Node a = new Node('A');
Node b = new Node('B', null, a);
Node c = new Node('C');
Node d = new Node('D', b, c);
Node e = new Node('E');
Node f = new Node('F', e, null);
Node g = new Node('G', null, f);
Node h = new Node('H', d, g);
return h;// root
}

/** 访问节点 */
public static void visit(Node p) {
System.out.print(p.getKey() + " ");
}

/** 递归实现前序遍历 */
static void preorder(Node p) {
if (p != null) {
visit(p);
preorder(p.getLeft());
preorder(p.getRight());
}
}

/** 递归实现中序遍历 */
static void inorder(Node p) {
if (p != null) {
inorder(p.getLeft());
visit(p);
inorder(p.getRight());
}
}

/** 递归实现后序遍历 */
static void postorder(Node p) {
if (p != null) {
postorder(p.getLeft());
postorder(p.getRight());
visit(p);
}
}

/** 层次遍历*/
static void levelorder(Node p){
if(p==null) return;
Queue<Node> queue=new LinkedList<Node>();
queue.offer(p);
while(queue.size()>0){
Node temp=queue.poll();
visit(temp);
if(temp.getLeft()!=null){
queue.offer(temp.getLeft());
}
if(temp.getRight()!=null){
queue.offer(temp.getRight());
}
}

}


/** 非递归实现前序遍历 */
static void iterativePreorder(Node p) {
Stack<Node> stack = new Stack<Node>();
if (p != null) {
stack.push(p);
while (!stack.empty()) {
p = stack.pop();
visit(p);
if (p.getRight() != null)
stack.push(p.getRight());
if (p.getLeft() != null)
stack.push(p.getLeft());
}
}
}



/** 非递归实现后序遍历 单栈法*/
static void iterativePostorder(Node p) {
Stack<Node> stack = new Stack<Node>();
Node node = p, prev = p;
while (node != null || stack.size() > 0) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
if (stack.size() > 0) {
Node temp = stack.peek().getRight();
if (temp == null || temp == prev) {
node = stack.pop();
visit(node);
prev = node;
node = null;
} else {
node = temp;
}
}

}
}





/** 非递归实现中序遍历 */
static void iterativeInorder(Node p) {
Stack<Node> stack = new Stack<Node>();
Node node = p;
while (node != null || stack.size() > 0) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
if (stack.size() > 0) {
node = stack.pop();
visit(node);
node = node.getRight();
}
}
}

// 求二叉树的高度
static int height(Node tree) {
if (tree == null)
return 0;
else {
int leftTreeHeight = height(tree.getLeft());
int rightTreeHeight = height(tree.getRight());;
return leftTreeHeight > rightTreeHeight ? leftTreeHeight + 1: rightTreeHeight + 1;
}
}

// 求二叉树的结点总数
static int nodes(Node tree) {
if (tree == null)
return 0;
else {
int left = nodes(tree.getLeft());
int right = nodes(tree.getRight());
return left + right + 1;
}
}


// 求二叉树叶子节点的总数
static int leaf(Node tree) {
if (tree == null)
return 0;
else {
int left = leaf(tree.getLeft());
int right = leaf(tree.getRight());
if (tree.getLeft() == null && tree.getRight() == null)
return left + right + 1;
else
return left + right;
}
}


/**
* @param args
*/
public static void main(String[] args) {
BinaryTree tree = new BinaryTree(init());
System.out.print(" 前序遍历:");
preorder(tree.getRoot());
System.out.println();
System.out.print(" 中序遍历:");
inorder(tree.getRoot());
System.out.println();
System.out.print(" 后序遍历:");
postorder(tree.getRoot());
System.out.println();
System.out.println();

System.out.print(" 非递归前序遍历:");
iterativePreorder(tree.getRoot());
System.out.println();
System.out.print(" 非递归中序遍历:");
iterativeInorder(tree.getRoot());
System.out.println();


System.out.print(" 非递归后序遍历:");
iterativePostorder(tree.getRoot());
System.out.println();

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

System.out.println("叶子结点数");
System.out.println(leaf(tree.getRoot()));
System.out.println("总结点数");
System.out.println(nodes(tree.getRoot()));
System.out.println("树的高度");
System.out.println(height(tree.getRoot()));



}

}

public class Node {
private char key;
private Node left, right;

public Node(char key) {
this(key, null, null);
}

public Node(char key, Node left, Node right) {
this.key = key;
this.left = left;
this.right = right;
}

public char getKey() {
return key;
}

public void setKey(char key) {
this.key = key;
}

public Node getLeft() {
return left;
}

public void setLeft(Node left) {
this.left = left;
}

public Node getRight() {
return right;
}

public void setRight(Node right) {
this.right = right;
}
}


运行:
前序遍历:H D B A C G F E
中序遍历:B A D C H G E F
后序遍历:A B C D E F G H

非递归前序遍历:H D B A C G F E
非递归中序遍历:B A D C H G E F
非递归后序遍历:A B C D E F G H
层次遍历
H D G B C F A E

叶子结点数
3
总结点数
8
树的高度
4

下载源码:
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉实现二叉树先序中序后序遍历操作的具体步骤如下: 1. 定义二叉树节点类,包含节点值、左右子节点等属性。 2. 根据输入的前序序列和中序序列构建二叉树。具体步骤如下: 1. 前序序列的第一个元素为根节点,将其在中序序列中找到对应位置,左边为左子树的中序序列,右边为右子树的中序序列。 2. 根据左子树的中序序列长度,在前序序列中找到左子树的前序序列,右边为右子树的前序序列。 3. 递归构建左子树和右子树。 3. 实现先序遍历、中序遍历和后序遍历函数。具体步骤如下: 1. 先序遍历:先输出当前节点的值,再递归遍历左子树和右子树。 2. 中序遍历:先递归遍历左子树,再输出当前节点的值,最后递归遍历右子树。 3. 后序遍历:先递归遍历左子树和右子树,最后输出当前节点的值。 4. 统计二叉树的叶子数。具体步骤如下: 1. 如果当前节点为空,则返回0。 2. 如果当前节点为叶子节点,则返回1。 3. 否则,递归统计左子树和右子树的叶子数,并将它们相加。 下面是Python代码实现: ```python class TreeNode: 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_val = preorder[0] root = TreeNode(root_val) root_index = inorder.index(root_val) root.left = buildTree(preorder[1:root_index+1], inorder[:root_index]) root.right = buildTree(preorder[root_index+1:], inorder[root_index+1:]) return root def preorderTraversal(root): if not root: return [] res = [] res.append(root.val) res += preorderTraversal(root.left) res += preorderTraversal(root.right) return res def inorderTraversal(root): if not root: return [] res = [] res += inorderTraversal(root.left) res.append(root.val) res += inorderTraversal(root.right) return res def postorderTraversal(root): if not root: return [] res = [] res += postorderTraversal(root.left) res += postorderTraversal(root.right) res.append(root.val) return res def countLeaves(root): if not root: return 0 if not root.left and not root.right: return 1 return countLeaves(root.left) + countLeaves(root.right) # 示例 preorder = [1, 2, 4, 5, 3, 6, 7] inorder = [4, 2, 5, 1, 6, 3, 7] root = buildTree(preorder, inorder) print("先序遍历:", preorderTraversal(root)) print("中序遍历:", inorderTraversal(root)) print("后序遍历:", postorderTraversal(root)) print("叶子数:", countLeaves(root)) --相关问题--:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值