init commit.
This commit is contained in:
commit
a8748389d7
61
java/三叉链表parent链深拷贝/BiTree.java
Normal file
61
java/三叉链表parent链深拷贝/BiTree.java
Normal file
@ -0,0 +1,61 @@
|
||||
package site.licsber.bitree;
|
||||
|
||||
public class BiTree<T> {
|
||||
public static <T> Node<T> deepCopy(Node<T> srcRoot) {
|
||||
if (srcRoot == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Node<T> p = srcRoot;
|
||||
Node<T> res = new Node<>(p.data);
|
||||
Node<T> q = res;
|
||||
|
||||
while (p != null) {
|
||||
while (p.left != null && q.left == null) {
|
||||
p = p.left;
|
||||
q = new Node<>(p.data, q);
|
||||
System.out.println(q);
|
||||
q.parent.left = q;
|
||||
}
|
||||
|
||||
if (p.right != null && q.right == null) {
|
||||
p = p.right;
|
||||
q = new Node<>(p.data, q);
|
||||
System.out.println(q);
|
||||
q.parent.right = q;
|
||||
} else {
|
||||
while (p.parent != null && p.parent.right == p) {
|
||||
p = p.parent;
|
||||
q = q.parent;
|
||||
}
|
||||
p = p.parent;
|
||||
q = q.parent;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static <T> void walk(Node<T> node) {
|
||||
if (node != null) {
|
||||
System.out.print(node);
|
||||
walk(node.left);
|
||||
walk(node.right);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Node<String> testRoot = new Node<>("A");
|
||||
testRoot.left = new Node<>("B", testRoot);
|
||||
testRoot.right = new Node<>("C", testRoot);
|
||||
testRoot.left.left = new Node<>("D", testRoot.left);
|
||||
testRoot.left.left.right = new Node<>("G", testRoot.left.left);
|
||||
testRoot.right.left = new Node<>("E", testRoot.right);
|
||||
testRoot.right.right = new Node<>("F", testRoot.right);
|
||||
testRoot.right.right.left = new Node<>("H", testRoot.right.right);
|
||||
walk(testRoot);
|
||||
|
||||
System.out.println();
|
||||
Node<String> copyRoot = deepCopy(testRoot);
|
||||
walk(copyRoot);
|
||||
}
|
||||
}
|
22
java/三叉链表parent链深拷贝/Node.java
Normal file
22
java/三叉链表parent链深拷贝/Node.java
Normal file
@ -0,0 +1,22 @@
|
||||
package site.licsber.bitree;
|
||||
|
||||
public class Node<T> {
|
||||
public Node(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Node(T data, Node<T> parent) {
|
||||
this.data = data;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public T data;
|
||||
public Node<T> left;
|
||||
public Node<T> right;
|
||||
public Node<T> parent;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return data.toString();
|
||||
}
|
||||
}
|
6
java/三叉链表parent链深拷贝/README.md
Normal file
6
java/三叉链表parent链深拷贝/README.md
Normal file
@ -0,0 +1,6 @@
|
||||
# 三叉链表 带parent链 深拷贝方法
|
||||
|
||||
## 来源
|
||||
|
||||
刘家可的课设
|
||||
|
Loading…
Reference in New Issue
Block a user