init commit.

This commit is contained in:
Licsber 2022-03-03 18:03:39 +08:00
commit a8748389d7
4 changed files with 95 additions and 0 deletions

6
README.md Normal file
View File

@ -0,0 +1,6 @@
# Algorithm
## 说明
记录自己实现的一些算法题.

View 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);
}
}

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

View File

@ -0,0 +1,6 @@
# 三叉链表 带parent链 深拷贝方法
## 来源
刘家可的课设