Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Описание интерфейса метода с использованием тест-файла Mocha (https://javascript.ru/forum/misc/66381-opisanie-interfejjsa-metoda-s-ispolzovaniem-test-fajjla-mocha.html)

SergeyShatter 12.12.2016 06:47

Описание интерфейса метода с использованием тест-файла Mocha
 
К сожалению я недостаточно хорошо понимаю ООП в JS и как следствие я застрял на написании очередного метода. Речь идет об алгоритме Max Heap. Ниже приведена лишь часть, которой достаточно для реализации метода. Суть задания: Дан набор методов. Необходимо реализовать их интерфейс используя код теста Mocha. В данный момент я застрял на методе swapWithParent(). Подскажите пожалуйста как его реализовать.
Заранее благодарю всех, кто примет участие!

Основной файл:

"use strict";

class Node {
constructor(data, priority) {
this.data = data;
this.priority = priority;
this.parent = null;
this.left = null;
this.right = null;
}

appendChild(node) {
node.parent = this;

if (this.left && this.right) {
return;
}

if (this.left) {
this.right = node;
return;
}

this.left = node;
}

removeChild(node) {
if (node == this.left) {
this.left = null;
} else if(node == this.right) {
this.right = null;
} else return error;

node.parent = null;
}

remove() {
if (!this.parent) {
return;
} else {
this.parent.removeChild(this);
}
}

swapWithParent() {
if (!this.parent) {
return;
}

}
}

module.exports = Node;


Описание работы метода swapWithParent() в тест файле Mocha:

describe('#swapWithParent', () => {
it('does nothing if node does not have parent', () => {
const node = new Node(15, 42);

expect(() => {
node.swapWithParent();
}).not.to.throw();
});

it('updates parent.parent', () => {
const parent = new Node(15, 42);
const child = new Node(42, 15);

parent.appendChild(child);
child.swapWithParent();

expect(parent.parent).to.equal(child);
});

it('updates parent.parent.parent', () => {
const root = new Node(8, 8);
const child = new Node(4, 4);
const grandson = new Node(2, 2);

root.appendChild(child);
child.appendChild(grandson);

grandson.swapWithParent();

expect(child.parent).to.equal(grandson);
expect(grandson.parent).to.equal(root);
});

it('updates child.parent', () => {
const parentOfParent = new Node(100, 500);
const parent = new Node(15, 42);
const child = new Node(42, 15);

parentOfParent.appendChild(parent);
parent.appendChild(child);
child.swapWithParent();

expect(child.parent).to.equal(parentOfParent);
});

it('updates parent.child.parent', () => {
const root = new Node(1, 2);
const left = new Node(3, 4);
const right = new Node(5, 6);

root.appendChild(left);
root.appendChild(right);

right.swapWithParent();

expect(left.parent).to.equal(right);
})

it('updates children of node and parent node', () => {
const root = new Node(42, 15);
const left = new Node(13, 42);
const right = new Node(0, 1);
const childOfLeft = new Node(0, 15);

root.appendChild(left);
root.appendChild(right);
left.appendChild(childOfLeft);

left.swapWithParent();

expect(left.right).to.equal(right);
expect(left.left).to.equal(root);
expect(root.left).to.equal(childOfLeft);
});

it('maintains correct state of parent.parent.left and parent.parent.right', () => {
const root = new Node(15, 42);
const left = new Node(42, 15);
const right = new Node(13, 42);
const childOfLeft = new Node(13, 34);
const childOfRight = new Node(0, 1);

root.appendChild(left);
root.appendChild(right);
left.appendChild(childOfLeft);
right.appendChild(childOfRight);

childOfLeft.swapWithParent();
childOfRight.swapWithParent();

expect(root.left).to.equal(childOfLeft);
expect(root.right).to.equal(childOfRight);
});
});

sashaKazakevich 24.01.2017 02:35

Привет! я новичек и решаю тоже это задание вот моя реализация этого метода все тесты проходят возможно реализация далека от идеала но зато есть почва для размышлений

swapWithParent() {
		if (!this.parent) return

		var thisChildLeft = this.left;
		var thisChildRight = this.right;
		var thisParent = this.parent;

		var parentChildRight = this.parent.right;
		var parentChildLeft = this.parent.left;
		var parentParent = this.parent.parent;

		if(parentParent){
			var parentParentLeft = this.parent.parent.left;
			var parentParentRight = this.parent.parent.right;
		}

		this.parent.parent = this;
		this.parent = parentParent;

		if (this === parentChildRight){
			if (parentChildLeft){
				this.left = parentChildLeft;
				parentChildLeft.parent = this;
			}

			this.right = thisParent;
			thisParent.rigth = thisChildRight;
			thisParent.left = thisChildLeft;
		}

		if (this === parentChildLeft){
			if (parentChildRight){
				this.right = parentChildRight;
				parentChildRight.parent = this;
			}

			this.left = thisParent;
			thisParent.rigth = thisChildRight;
			thisParent.left = thisChildLeft;
		}


		if (thisParent === parentParentLeft){
			this.parent.left = this;
		}

		if (thisParent === parentParentRight){
			this.parent.right = this;
		}
	}


Часовой пояс GMT +3, время: 05:32.