java树结构_Java 实现树结构

树是一种非常重要的数据结构,其中二叉树是最常用到的,之前学的时候用的都是c++,很长时间没有用了也忘得差不多了,最近一直都在用Java,所以总结一下怎样用java来实现二叉树的数据结构,用二叉树来存一个数组。

二叉树得特点有以下几个:1. 每个节点最多有两棵子树。2. 左子树和右子树是有顺序的,次序不能任意颠倒。3. 即使树中只有一课子树,也要区分他是左子树还是右子树;

二叉树的遍历:是指从根结点出发,按照某种次序,依次访问二叉树中所有结点,使得每个结点被访问一次且仅被访问一次;二叉树的遍历方式有好多种,如果我们限制了从左到右的习惯方式,那么主要就有以下几种:前序遍历,中序遍历,后序遍历和层序遍历。

二叉树的实现:二叉树也可以通过顺序存储和链式存储来实现;

二叉树的顺序存储就是用一维数组存储二叉树中的结点,并且结点的存储位置,也就是数组的下标要能体现结点之间的逻辑关系,比如父结点与子结点的逻辑关系,子结点与子结点之间的关系;但顺序存储的实用性不强;所以一般采用链式存储;

在Java中,采用类来声明树的节点 如下所示

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;

}

}

接下来就是创建一个二叉树,以及对二叉树进行前序遍历,中序遍历,后序遍历和层序遍历。

public class Tree

{

protected

Node root;

public

Tree(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;  //

根结点

}

//

访问节点

public

static void visit(Node p)

{

System.out.print(p.getKey() +

" ");

}

//

递归实现前序遍历

protected

static void preorder(Node p)

{

if (p != null)

{

visit(p);

preorder(p.getLeft());

preorder(p.getRight());

}

}

//

递归实现中序遍历

protected

static void inorder(Node p)

{

if (p != null)

{

inorder(p.getLeft());

visit(p);

inorder(p.getRight());

}

}

//

递归实现后序遍历

protected

static void postorder(Node p)

{

if (p != null)

{

postorder(p.getLeft());

postorder(p.getRight());

visit(p);

}

}

//

非递归实现前序遍历

protected

static void iterativePreorder(Node p)

{

Stack stack = new Stack();

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());

}

}

}

//

非递归实现后序遍历

protected

static void iterativePostorder(Node p)

{

Node q = p;

Stack stack = new Stack();

while (p != null)

{

// 左子树入栈

for (; p.getLeft() != null; p = p.getLeft())

stack.push(p);

// 当前结点无右子结点或右子结点已经输出

while (p != null && (p.getRight() ==

null || p.getRight() == q))

{

visit(p);

q = p;

// 记录上一个已输出结点

if

(stack.empty())

return;

p =

stack.pop();

}

// 处理右子结点

stack.push(p);

p = p.getRight();

}

}

//

非递归实现中序遍历

protected

static void iterativeInorder(Node p)

{

Stack stack = new Stack();

while (p != null)

{

while (p != null)

{

if

(p.getRight() != null)

stack.push(p.getRight());

// 当前结点右子结点入栈

stack.push(p);  // 当前结点入栈

p =

p.getLeft();

}

p = stack.pop();

while (!stack.empty() && p.getRight() ==

null)

{

visit(p);

p =

stack.pop();

}

visit(p);

if (!stack.empty())

p =

stack.pop();

else

p = null;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值