Добавлен Clamp,
опубликован
Наткнулся среди своих файлов на следующий код, когда и зачем писал - в упор не помню. Публикую из-за долбанутости задумки, хех.
На работоспособность не проверял, за пару минут разглядывания каких-нибудь очевидных косяков не обнаружил.
На работоспособность не проверял, за пару минут разглядывания каких-нибудь очевидных косяков не обнаружил.
код
type Object extends int;
struct Vector {
private Object data[162];
private int size = 0;
static Vector createFrom(Vector another) {
Vector this = Vector.allocate();
this = another;
return this;
}
void operator=(Vector another) {
size = 0;
while (another.size != size++) {
data[size - 1] = another.data[size - 1];
}
}
int operator[](int index) {
if (index < size) {
return data[index];
}
return 0;
}
void operator[]=(int index, Object o) {
data[index] = o;
}
void insert(int index, Object o) {
int i = size;
while (index < i--) {
data[i + 1] = data[i];
}
data[index] = o;
size++;
}
void swap(Vector another) {
Vector temp = Vector.createFrom(another);
another = this;
this = temp;
temp.destroy();
}
void pushBack(Object o) {
data[size++] = o;
}
void popBack() {
data[--size] = 0;
}
void clear() {
size = 0;
}
int size() {
return size;
}
Object front() {
return data[0];
}
Object back() {
return data[size - 1];
}
}
constant int LEAFS_NUM = 2;
struct TreeNodeList {
private TreeNode data[LEAFS_NUM];
private int size = 0;
public int size() {
return size;
}
public TreeNode get(int index) {
if (index < this.size) {
return data[index];
}
return 0;
}
public void add(TreeNode o) {
data[size++] = o;
}
public void addAt(int index, TreeNode o) {
data[index] = o;
}
public void remove(int index) {
data[index] = 0;
}
}
struct TreeNode {
private Object data;
private TreeNodeList children;
private TreeNode parent;
public static TreeNode createEmpty() {
TreeNode this = TreeNode.allocate();
this.children = TreeNodeList.create();
return this;
}
public static TreeNode create(Object data) {
TreeNode this = TreeNode.createEmpty();
this.setData(data);
return this;
}
public void destroy() {
this.children.destroy();
this.deallocate();
}
public TreeNode getParent() {
return parent;
}
public TreeNodeList getChildren() {
return children;
}
public int getNumberOfChildren() {
return children.size();
}
public bool hasChildren() {
return getNumberOfChildren() > 0;
}
public TreeNode getChildAt(int index) {
return children.get(index);
}
public Object getData() {
return data;
}
public void setData(Object data) {
data = data;
}
public bool equals(TreeNode other) {
return this == other;
}
public void setChildren(TreeNodeList children) {
int i = children.size;
while (0 != i--) {
children.get(i).parent = this;
}
this.children.destroy();
this.children = children;
}
public void addChild(TreeNode child) {
child.parent = this;
children.add(child);
}
public void addChildAt(int index, TreeNode child) {
child.parent = this;
children.addAt(index, child);
}
}
struct Tree {
private TreeNode root;
public static Tree create() {
Tree this = Tree.allocate();
this.root = TreeNode.createEmpty();
return this;
}
public TreeNode getRoot() {
return root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
public int getNumberOfNodes() {
int numberOfNodes = 0;
if (root != null) {
numberOfNodes = auxiliaryGetNumberOfNodes(root) + 1; // 1 for the root
}
return numberOfNodes;
}
private int auxiliaryGetNumberOfNodes(TreeNode node) {
int numberOfNodes = node.getNumberOfChildren();
int i = 0;
while (currentNode.getNumberOfChildren() > i++) {
numberOfNodes += auxiliaryGetNumberOfNodes(node.getChildAt(i - 1));
}
return numberOfNodes;
}
public bool contains(Object toFind) {
return find(toFind) != null;
}
public TreeNode find(Object toFind) {
if (root == 0) {
return 0;
}
return auxiliaryFind(root, toFind);
}
private TreeNode auxiliaryFind(TreeNode currentNode, Object toFind) {
TreeNode returnNode = null;
int i = 0;
if (currentNode.getData().equals(toFind)) {
return currentNode;
}
if (currentNode.hasChildren()) {
while (returnNode == null && currentNode.getNumberOfChildren() > i++) {
returnNode = auxiliaryFind(currentNode.getChildAt(i - 1), toFind);
}
}
return returnNode;
}
public bool isEmpty() {
return root == null;
}
public Vector build(TreeNode node, bool preOrder) {
Vector traversalResult = Vector.create();
if (preOrder) {
buildPreOrder(node, traversalResult);
} else {
buildPostOrder(node, traversalResult);
}
return traversalResult;
}
private void buildPreOrder(TreeNode node, Vector traversalResult) {
traversalResult.pushBack(node);
int i = 0;
while (node.getNumberOfChildren() > i++) {
buildPreOrder(node.getChildAt(i - 1), traversalResult);
}
}
private void buildPostOrder(TreeNode node, Vector traversalResult) {
int i = 0;
while (node.getNumberOfChildren() > i++) {
buildPostOrder(node.getChildAt(i - 1), traversalResult);
}
traversalResult.pushBack(node);
}
}
Чтобы оставить комментарий, пожалуйста, войдите на сайт.
Отредактирован Clamp