Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现二叉树 #1010

Open
pwstrick opened this issue Jul 6, 2020 · 0 comments
Open

实现二叉树 #1010

pwstrick opened this issue Jul 6, 2020 · 0 comments
Labels
一句话算法 一句话算法系列中的代码实现

Comments

@pwstrick
Copy link
Owner

pwstrick commented Jul 6, 2020

class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}
class Tree {
  constructor(datas) {
    this.root = null;
    datas.forEach((value) => {
      const node = new Node(value);
      if (this.root == null) {
        this.root = node;
        return;
      }
      this.insert(this.root, node);
    });
  }
  insert(parent, child) {
    if (parent.data > child.data) {
      parent.left === null
        ? (parent.left = child)
        : this.insert(parent.left, child);
      return;
    }
    parent.right === null
      ? (parent.right = child)
      : this.insert(parent.right, child);
  }
}
@pwstrick pwstrick added the 一句话算法 一句话算法系列中的代码实现 label Jul 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
一句话算法 一句话算法系列中的代码实现
Projects
None yet
Development

No branches or pull requests

1 participant