Binary Trees in ES5 vs ES6?

There Is a good FCC YouTube lecture about binary space partitioning/trees using es6 classes, but I’m wondering how it could be implemented without classes?

can you post the link?

I’d imagine it’d have something to do with defining prototype functions, but why do it in ES5 when ES6 provides a better implementation?

I’ll get the link but the reason I’d like to know how to do it without classes is so I can better understand the mechanics and structure of it without language prototypes.

I guess I don’t want to rely on the language or rote ocpying of beau’s code here to implement it.

Anyway, understanding MORE and knowing how & when to use each skill is better than knowing less. I’m sure people wrote code for binary trees in ES5 (just like you can write react in ES vs ES6).

Just use objects.
let node = { value: null, left: {}, right: {} }
I haven’t watched the video, but the rest is implementation (moving through the tree, deleting/adding nodes etc) and shouldn’t change based on classes or lack of classes, just create methods that receive a node as input and return the new tree.

Left side is another node, right side is another node. To start the tree, just create a “root” object with the same properties.

Greg thanks, that makes perfect sense. The corrolary to this question then is, why use classes??

The root of this is wanting to know when to use a class vs an object I guess. If I knew exactly what questions to ask then I would be understanding it well, so looking to understand the choices for data structure representation better.

I would advise you not to learn data structures through javascript, it can be quite complicated as too much is abstracted away. Computer science subjects are often difficult to understand in languages like JS.
But the reason to use classes is simplicity, you can just instantiate each new node, instead of doing hacky things like copying the whole object.

instead of
let root = { value: null, left: {}, right: {} }

you can do
let root = new Node();

Also, you can attach the control methods into the class, so they become intrinsic to the node itself. instead of dependent upon outside functions. You don’t have to use classes, life will just be easier by using them.

In JavaScript, class is just a syntactic sugar implemented to look and work kind of like classes in OO languages like c++ or Java. underneath, it still works the same way it always has, with object and prototypes.

ES6 just makes it easier to work with

Do you know why Eric Elliot writes to try and avoid using classes in Javascript?

I can’t find the original article but he may have edited the title, or removed it or I just can’t find it. I don’t understand a lot of what he writes about but I don’t think it’s because I don’t understand coding, yet somehow he writes above my level of understanding …for the past 2 1/2 years. I’d think that sooner or later I would “get” what he is saying

That reads like a somewhat opinionated take. I don’t know if some of the pitfall can be avoided by simply not use class or specific to JavaScript. Like deep class inheritances, you can do that in just regular old Java if you didn’t plan well.

from a practical sense, I’m using React, there is no getting away from using extend, new, this… etc in my code. Angular is encouraging me to use class. If your team as a collective decides to use class, then all you can do is to make sure you enforce good practices to avoid the pitfalls he mentions

1 Like

The tl;dr, as I understand it, is that we should abide by the mantra of “composition over inheritance” when using one object to create another, but most OO languages promote an unhealthy amount of inheritance. Prototypal inheritance and plain old objects are JavaScript features that make composition extremely easy. Other language features, like the class keyword, encourage bad practice.

For what it’s worth, I think he’s said elsewhere that using class for React components isn’t so bad because we generally don’t go creating subclasses of the components we write, and the component class itself doesn’t come with a whole lot. Maybe that was someone else, though.

Here’s a good talk he did a while back:

1 Like

not sure about good videos from binary trees but I can just let you know it can get very tricky sometimes. binary trees can be very useful but also get very complicated which can lead to errors. Those errors better be detected quickly. so be aware. If you need any help detecting and solving errors there are a lot of programs like checkmarx or others.
just practice as much as possible and you’re good to go.
Good luck!
Michael.

1 Like

Specifically I want to implement binary space partitioning for my roguelike game. I understand it conceptually but between tracking the partitioned siblings and the recursion I haven’t cracked how to implement it.