JS实现的数据结构(二)--二叉树

392 阅读1分钟
// 节点构造函数
function Node(value, left, right) {
  this.value = value;
  this.left = left;
  this.right = right;
}

Node.prototype.show = function() {
  console.log(this.value);
}

// 二叉树构造函数
function Tree() {
  this.root = null;
}

// 插入节点
Tree.prototype.insert = function(data) {
  const node = new Node(data, null, null);
  if(!this.root) {
    this.root = node;
  } else {
    let currentNode = this.root;
    let parent = null;
    while(currentNode) {
      parent = currentNode;
      if(currentNode.value > data) {
        currentNode = currentNode.left;
        if(!currentNode) {
          parent.left = node;
          return;
        }
      } else {
        currentNode = currentNode.right;
        if(!currentNode) {
          parent.right = node;
          return;
        }
      }
    }
  }
}

// 前序遍历
Tree.prototype.preOrderTraversal = function(node = this.root) {
  if(node) {
    node.show();
    this.preOrderTraversal(node.left);
    this.preOrderTraversal(node.right);
  }
}

// 中序遍历
Tree.prototype.middleOrderTraversal = function(node = this.root) {
  if(node) {
    this.middleOrderTraversal(node.left);
    node.show();
    this.middleOrderTraversal(node.right);
  }
}

// 后序遍历
Tree.prototype.laterOrderTraversal = function(node = this.root) {
  if(node) {
    this.laterOrderTraversal(node.left);
    this.laterOrderTraversal(node.right);
    node.show();
  }
}

// 求树的深度
Tree.prototype.getDeep = function(node = this.root, deep = 0) {
  if(!node) {
    return deep;
  }
  deep ++;
  const leftDeep = this.getDeep(node.left, deep);
  const rightDeep = this.getDeep(node.right, deep);
  return Math.max(leftDeep, rightDeep);
}

// 查找二叉树
Tree.prototype.findNode = function(value, node = this.root) {
  if(node) {
    if (node.value === value) {
      return node;
    } else if (node.value < value) {
      return this.findNode(value, node.right);
    } else {
      return this.findNode(value, node.left);
    }
  } else {
    return null;
  }
}

// 判断两棵树是否是相同的树
function isSameTree(node1, node2) {
  if(!node1 && !node2) {
    return true;
  }
  if(!node1 || !node2) {
    // 这里已经把node1和node2都为null的情况排除了,因为前面已经return了。
    // 而如果在node1和node2中有一个为null的情况下,另一个无论是什么,只要不是null,那就不可能是同一棵树。
    return false;
  }
  if(node1.value !== node2.value) {
    // 如果node1.value === node2.value 递归不能停止,要继续递归子节点
    return false;
  }
  return isSameTree(node1.left, node2.left) && isSameTree(node1.right, node2.right)
}

// 判断一棵树是否是对称二叉树
function isSymmetric(root) {
  return nodeIsSymmetric(root, root)
  function nodeIsSymmetric(node1, node2) {
      if(!node1 && !node2) {
          return true;
      }
      if(!node1 || !node2) {
          return false;
      }
      if(node1.value !== node2.value) {
          return false;
      }
      return nodeIsSymmetric(node1.left, node2.right) && nodeIsSymmetric(node1.right, node2.left)
  }
};

// 输出所有叶子节点
function findAllLeafs(root) {
  if(root) {
    if(!root.left && !root.right) {
      root.show()
    } else {
      findAllLeafs(root.left);
      findAllLeafs(root.right);
    }
  }
}

// 判断一棵树(t)是否是另一棵树(s)的子树
function isSubtree(s, t) {
  if (!s) {
    return false;
  }
  if (isSameTree(s, t)) {
    return true;
  }
  return isSubtree(s.left, t) || isSubtree(s.right, t);
}