Показать сообщение отдельно
  #2 (permalink)  
Старый 24.01.2017, 02:35
Новичок на форуме
Отправить личное сообщение для sashaKazakevich Посмотреть профиль Найти все сообщения от sashaKazakevich
 
Регистрация: 24.01.2017
Сообщений: 2

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

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;
		}
	}
Ответить с цитированием