删除子父节点中的某个节点

6 篇文章 0 订阅
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class deleteNoteMysql {


public static void main(String args[]) throws SQLException {
Connection conn = DB.getConn();
Statement stmt = conn.createStatement();
ResultSet rs = null;
int id = 33;
rs = stmt.executeQuery("select * from category where id=33");
rs.next();
int a = rs.getInt(7);
int pidd = rs.getInt(2);
if (pidd == 0) {
de(id, a);
} else {
de(id, a);
rs = stmt.executeQuery("select count(*)  from category where pid ="
+ pidd);
System.out.println("pidd=" + pidd);
rs.next();
int count = rs.getInt(1);
if (count <= 0) {
stmt.executeUpdate("update category set isLeaf=0 where id= "
+ pidd);
}
}
System.out.println(a);
}


private static void de(int id, int isleaf) throws SQLException {//如果删除的节点的父节点没有其他节点需要设置父节点的idLeaf为0
// TODO Auto-generated method stub
Connection conn = DB.getConn();
Statement st = conn.createStatement();
if (isleaf == 0) {


st.execute("delete from category where id =" + id);
} else {
ResultSet rs = st
.executeQuery("select *  from category where pid =" + id);
while (rs.next()) {
System.out.println(rs.getInt(1));
de(rs.getInt(1), rs.getInt(7));
}
st.execute("delete from category where id =" + id);
}
}
}








/*
import java.sql.*;


public class DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/shopping?user=root&password=admin");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}

return conn;
}
}
*/
/*
Navicat MySQL Data Transfer


Source Server         : root
Source Server Version : 50527
Source Host           : localhost:3306
Source Database       : shopping


Target Server Type    : MYSQL
Target Server Version : 50527
File Encoding         : 65001


Date: 2013-02-23 10:53:44




SET FOREIGN_KEY_CHECKS=0;


-- ----------------------------
-- Table structure for `category`
-- ----------------------------
DROP TABLE IF EXISTS `category`;
CREATE TABLE `category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pid` int(11) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `descr` varchar(255) DEFAULT NULL,
  `cno` int(11) DEFAULT NULL,
  `grade` int(11) DEFAULT NULL,
  `isLeaf` int(1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8;


-- ----------------------------
-- Records of category
-- ----------------------------
INSERT INTO `category` VALUES ('31', '0', 'a', 'a', '2147483647', '1', '1');
INSERT INTO `category` VALUES ('32', '0', 'b', 'b', '-2', '1', '1');
INSERT INTO `category` VALUES ('33', '32', 'aa', 'aa', '2147483645', '2', '1');
INSERT INTO `category` VALUES ('34', '33', 'aaa', 'fahdgjhdfjkgjkl', '-4', '3', '1');
INSERT INTO `category` VALUES ('35', '34', 'aaaa', 'aaaa', '2147483643', '4', '0');
INSERT INTO `category` VALUES ('36', '31', 'afgas', 'asg', '-2', '2', '1');
INSERT INTO `category` VALUES ('37', '36', 'asg', 'asfhsdh', '2147483645', '3', '0');
INSERT INTO `category` VALUES ('38', '36', '', '', '-4', '3', '0');
INSERT INTO `category` VALUES ('39', '31', 'dh', 'fdgjhfd', '2147483645', '2', '0');
INSERT INTO `category` VALUES ('40', '31', 'dfgh', 'dfjhdfj', '-4', '2', '0');
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
删除邻接矩阵的某个节点并将其父节点子节点相连,需要先根据该节点的索引找到其所有的父节点子节点,然后将它们之间的边进行重新连接。具体操作如下: 1. 找到该节点所在的行和列索引值; 2. 根据该节点的行和列索引值找到其所有的父节点子节点; 3. 将该节点所对应的行和列从邻接矩阵删除; 4. 将父节点子节点之间的边进行重新连接; 5. 对于删除节点后的邻接矩阵,需要将其余节点的行和列索引值重新编号。 以下是一个示例代码: ```python # 定义一个邻接矩阵 adj_matrix = [[0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]] # 要删除节点 node_idx = 2 # 找到该节点父节点子节点 parents = [i for i in range(len(adj_matrix)) if adj_matrix[i][node_idx] == 1] children = [j for j in range(len(adj_matrix[node_idx])) if adj_matrix[node_idx][j] == 1] # 删除节点及其对应的行和列 adj_matrix.pop(node_idx) for row in adj_matrix: row.pop(node_idx) # 连接父节点子节点 for p in parents: for c in children: adj_matrix[p][c] = 1 # 重新编号 for i in range(len(adj_matrix)): if i >= node_idx: for j in range(len(adj_matrix[i])): if j >= node_idx: adj_matrix[i][j] = adj_matrix[i+1][j+1] ``` 需要注意的是,如果该节点与其他节点存在边相连,删除节点会将这些边也一并删除。因此,在连接父节点子节点时,需要确保它们之间不存在重复的边。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值