当前位置:首页>开发>正文

java如何实现二叉树的删除算法 怎样用JAVA实现二叉树的建立,遍历和节点删除

2023-04-25 01:22:43 互联网 未知 开发

 java如何实现二叉树的删除算法 怎样用JAVA实现二叉树的建立,遍历和节点删除

java如何实现二叉树的删除算法

用递归很简单了。

BTNode类就自己定义吧,属性为名字,和左右孩子以及set、get方法

前序遍历:
protected static void preorder(BTNode node) {
if (node != null) {
visit(node)//随便写点打印p的名字之类的语句
preorder(node.getLeft())
preorder(node.getRight())
}
}
中序:
protected static void inorder(BTNode node) {
if (node != null) {
inorder(node.getLeft())
visit(node)
inorder(node.getRight())
}
}
后序:
protected static void postorder(BTNode node) {
if (node != null) {
postorder(node.getLeft())
postorder(node.getRight())
visit(node)
}
}
非递归的就麻烦了,得用到栈。我就偷个懒不做了...

怎样用JAVA实现二叉树的建立,遍历和节点删除

回答:smile2me
学妹
6月13日 13:32 //BinaryTree.h

/* 二叉树的二叉链表结点定义 */
typedef char datatype
typedef struct BiTNode
{
datatype data
struct BiTNode * LChild , * RChild
} BiTNode , * BiTree

//数叶子结点的数目
/*
Author: WadeFelix RenV
*/
#include <stdio.h>
#include <stdlib.h>
#include "BinaryTree.h"
int countLeaf( BiTree BT )
{
if( BT == NULL ) return 0
if( BT->LChild==NULL && BT->RChild==NULL ) return 1
return(countLeaf(BT->LChild) countLeaf(BT->RChild))
}

//数非叶子结点的数目

int countNotLeaf( BiTree BT )
{
if( BT == NULL ) return 0
if( BT->LChild==NULL && BT->RChild==NULL ) return 0
return(1 countNotLeaf(BT->LChild) countNotLeaf(BT->RChild))
}

//判断是否是排序二叉树
#include <stdio.h>
#include <stdlib.h>
#include "BinaryTree.h"
int isPaiXu( BiTree BT )
{
if( BT == NULL )return 1
if( BT->LChild && (BT->LChild->data > BT->data) )return 0
if( BT->RChild && (BT->RChild->data < BT->data) )return 0

return( isPaiXu(BT->LChild)&&isPaiXu(BT->RChild) )
}

java程序怎么样实现删除

import java.util.*

public class ArrayListTest {
public static void main(String [] args) {
List st = new ArrayList()
st.add("ni hao")
st.add("java")
st.add("中国")
st.remove(0)
System.out.println(st)
}
}
其实API就是最好的参考资料

数据结构 java编写二叉树的增加删除与修改

亲,我没写出来,但是给你找了一个,感觉人家写的还不错,你参考下吧!~~~

package com.algorithm.tree

import java.io.File
import java.io.FileNotFoundException
import java.util.Queue
import java.util.Scanner
import java.util.Stack
import java.util.concurrent.LinkedBlockingQueue

public class Tree {

private Node root

public Tree() {
}

public Tree(Node root) {
this.root = root
}

//创建二叉树
public void buildTree() {

Scanner scn = null
try {
scn = new Scanner(new File("input.txt"))
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
root = createTree(root,scn)
}
//先序遍历创建二叉树
private Node createTree(Node node,Scanner scn) {

String temp = scn.next()

if (temp.trim().equals("#")) {
return null
} else {
node = new Node((T)temp)
node.setLeft(createTree(node.getLeft(), scn))
node.setRight(createTree(node.getRight(), scn))
return node
}

}

//中序遍历(递归)
public void inOrderTraverse() {
inOrderTraverse(root)
}

public void inOrderTraverse(Node node) {
if (node != null) {
inOrderTraverse(node.getLeft())
System.out.println(node.getValue())
inOrderTraverse(node.getRight())
}
}

//中序遍历(非递归)
public void nrInOrderTraverse() {

Stack> stack = new Stack>()
Node node = root
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node)
node = node.getLeft()
}
node = stack.pop()
System.out.println(node.getValue())
node = node.getRight()

}

}
//先序遍历(递归)
public void preOrderTraverse() {
preOrderTraverse(root)
}

public void preOrderTraverse(Node node) {
if (node != null) {
System.out.println(node.getValue())
preOrderTraverse(node.getLeft())
preOrderTraverse(node.getRight())
}
}

//先序遍历(非递归)
public void nrPreOrderTraverse() {

Stack> stack = new Stack>()
Node node = root

while (node != null || !stack.isEmpty()) {

while (node != null) {
System.out.println(node.getValue())
stack.push(node)
node = node.getLeft()
}
node = stack.pop()
node = node.getRight()
}

}

//后序遍历(递归)
public void postOrderTraverse() {
postOrderTraverse(root)
}

public void postOrderTraverse(Node node) {
if (node != null) {
postOrderTraverse(node.getLeft())
postOrderTraverse(node.getRight())
System.out.println(node.getValue())
}
}

//后续遍历(非递归)
public void nrPostOrderTraverse() {

Stack> stack = new Stack>()
Node node = root
Node preNode = null//表示最近一次访问的节点

while (node != null || !stack.isEmpty()) {

while (node != null) {
stack.push(node)
node = node.getLeft()
}

node = stack.peek()

if (node.getRight() == null || node.getRight() == preNode) {
System.out.println(node.getValue())
node = stack.pop()
preNode = node
node = null
} else {
node = node.getRight()
}

}

}

//按层次遍历
public void levelTraverse() {
levelTraverse(root)
}

public void levelTraverse(Node node) {

Queue> queue = new LinkedBlockingQueue>()
queue.add(node)
while (!queue.isEmpty()) {

Node temp = queue.poll()
if (temp != null) {
System.out.println(temp.getValue())
queue.add(temp.getLeft())
queue.add(temp.getRight())
}

}

}

}

//树的节点

class Node {

private Node left
private Node right
private T value

public Node() {
}
public Node(Node left,Node right,T value) {
this.left = left
this.right = right
this.value = value
}

public Node(T value) {
this(null,null,value)
}
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 T getValue() {
return value
}
public void setValue(T value) {
this.value = value
}

}

Java中怎样实现批量删除操作

Java中实现批量删除操作的方法如下:
示例代码如下:
public PreparedStatement pstmt=null
/**
* 得到连接对象
*/
public void getConnection(){
String driver="com.mysql.jdbc.Driver"
String url="jdbc:mysql://localhost:3306/zufang?user=root&password=root&useUnicode=true&characterEncoding=GB2312"
try {
Class.forName(driver)
con=DriverManager.getConnection(url,"root","root")
} catch (ClassNotFoundException e) {
e.printStackTrace()
} catch (SQLException e) {
e.printStackTrace()
}
}
public Connection con=null
public PreparedStatement pstmt=null
/**
* 得到连接对象
*/
public void getConnection(){
String driver="com.mysql.jdbc.Driver"
String url="jdbc:mysql://localhost:3306/zufang?user=root&password=root&useUnicode=true&characterEncoding=GB2312"
try {
Class.forName(driver)
con=DriverManager.getConnection(url,"root","root")
} catch (ClassNotFoundException e) {
e.printStackTrace()
} catch (SQLException e) {
e.printStackTrace()
}
}
/ * 批量删除信息表中的信息
* @param sql
* @param param
* @return
*/
public boolean updateBatchDel(String sql,String[] param){
boolean flag = false
getConnection()
try {
con.setAutoCommit(false)
pstmt = con.prepareStatement(sql)
for(int i =0 i pstmt.setString(1,param[i].trim())
pstmt.addBatch()
}
pstmt.executeBatch() //批量执行
con.commit()//提交事务
flag = true
} catch (SQLException e) {
try {
con.rollback() //进行事务回滚
} catch (SQLException ex) {
ex.printStackTrace()
}
}finally {
closeAll(null,pstmt,con)
}
return flag
}
/**
* 批量删除信息表中的信息
* @param sql
* @param param
* @return
*/
public boolean updateBatchDel(String sql,String[] param){
boolean flag = false
getConnection()
try {
con.setAutoCommit(false)
pstmt = con.prepareStatement(sql)
for(int i =0 i pstmt.setString(1,param[i].trim())
pstmt.addBatch()
}
pstmt.executeBatch() //批量执行
con.commit()//提交事务
flag = true
} catch (SQLException e) {
try {
con.rollback() //进行事务回滚
} catch (SQLException ex) {
ex.printStackTrace()
}
}finally {
closeAll(null,pstmt,con)
}
return flag
}
删除后同步提交就可以了。

最新文章