二叉树查找 java_java实现二叉树算法 - 查找

/** 二叉树遍历 */

public

class

BinTree {

protected

BTNode root;

public

BinTree(BTNode root) {

this

.root = root;

}

public

BTNode getRoot() {

return

root;

}

/** 构造树 */

public

static

BTNode init() {

BTNode a =

new

BTNode('A');

BTNode b =

new

BTNode('B',

null

, a);

BTNode c =

new

BTNode('C');

BTNode d =

new

BTNode('D', b, c);

BTNode e =

new

BTNode('E');

BTNode f =

new

BTNode('F', e,

null

);

BTNode g =

new

BTNode('G',

null

, f);

BTNode h =

new

BTNode('H', d, g);

return

h;

// root

}

/** 访问节点 */

public

static

void

visit(BTNode p) {

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

" "

);

}

/** 递归实现前序遍历 */

protected

static

void

preorder(BTNode p) {

if

(p !=

null

) {

visit(p);

preorder(p.getLeft());

preorder(p.getRight());

}

}

/** 递归实现中序遍历 */

protected

static

void

inorder(BTNode p) {

if

(p !=

null

) {

inorder(p.getLeft());

visit(p);

inorder(p.getRight());

}

}

/** 递归实现后序遍历 */

protected

static

void

postorder(BTNode p) {

if

(p !=

null

) {

postorder(p.getLeft());

postorder(p.getRight());

visit(p);

}

}

/** 非递归实现前序遍历 */

protected

static

void

iterativePreorder(BTNode 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(BTNode p) {

BTNode 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(BTNode 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

;

}

}

public

static

void

main(String[] args) {

BinTree tree =

new

BinTree(init());

System.out.print(

" Pre-Order:"

);

preorder(tree.getRoot());

System.out.println();

System.out.print(

"  In-Order:"

);

inorder(tree.getRoot());

System.out.println();

System.out.print(

"Post-Order:"

);

postorder(tree.getRoot());

System.out.println();

System.out.print(

" Pre-Order:"

);

iterativePreorder(tree.getRoot());

System.out.println();

System.out.print(

"  In-Order:"

);

iterativeInorder(tree.getRoot());

System.out.println();

System.out.print(

"Post-Order:"

);

iterativePostorder(tree.getRoot());

System.out.println();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值