-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarycopytree.java
More file actions
49 lines (38 loc) · 1.33 KB
/
Binarycopytree.java
File metadata and controls
49 lines (38 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.copytree;
/**
*
* @author HP
*/
public class Binarycopytree<T> {
T info;
Binarycopytree<T> llink;
Binarycopytree<T> rlink;
public Binarycopytree(T data) {
this.info = data;
this.llink = null;
this.rlink = null;
}
public Binarycopytree(Binarycopytree<T> otherTreeRoot) {
if (otherTreeRoot == null) {
throw new IllegalArgumentException("Cannot copy from a null root node");
}
this.info = otherTreeRoot.info;
this.llink = copyTree(otherTreeRoot.llink);
this.rlink = copyTree(otherTreeRoot.rlink);
}
// Recursive method to copy the tree
private Binarycopytree<T> copyTree(Binarycopytree<T> otherTreeRoot) {
if (otherTreeRoot == null) {
return null;
} else {
Binarycopytree<T> newNode = new Binarycopytree<>(otherTreeRoot.info);
newNode.llink = copyTree(otherTreeRoot.llink);
newNode.rlink = copyTree(otherTreeRoot.rlink);
return newNode;
}
}
}