温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用JavaScrip怎么为二叉树添加或删除节点

发布时间:2021-05-12 18:07:15 来源:亿速云 阅读:183 作者:Leah 栏目:web开发

今天就跟大家聊聊有关使用JavaScrip怎么为二叉树添加或删除节点,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

具体如下:

function Node(data,left,right) {
  this.data = data;
  this.left = left;
  this.right = right;
  this.show = show;
}
function show() {
  return this.data;
}
function BST() {
  this.root = null;
  this.insert = insert;
  this.inOrder = inOrder;
  this.getMin = getMin;
  this.getMax = getMax;
  this.find = find;
  this.remove = remove;
}
function insert(data) {
  var n = new Node(data,null,null);
  if(this.root == null) {
    this.root = n;
  }else {
    var current = this.root;
    var parent;
    while(current) {
      parent = current;
      if(data < current.data) {
        current = current.left;
        if(current == null) {
          parent.left = n;
          break;
        }
      }else {
        current = current.right;
        if(current == null) {
          parent.right = n;
          break;
        }
      }
    }
  }
}
// 中序遍历
function inOrder(node) {
  if(!(node == null)) {
    inOrder(node.left);
    console.log(node.show());
    inOrder(node.right);
  }
}
// 先序遍历 
function preOrder(node) {
  if(!(node == null)) {
    console.log(node.show());
    preOrder(node.left);
    preOrder(node.right);
  }
}
// 后序遍历
function postOrder(node) {
  if(!(node == null)) {
    postOrder(node.left);
    postOrder(node.right);
    console.log("后序遍历"+node.show());
  }
}
// 二叉树查找最小值
function getMin(){
  var current = this.root;
  while(!(current.left == null)) {
    current = current.left;
  }
  return current.data;
}
// 二叉树上查找最大值
function getMax() {
  var current = this.root;
  while(!(current.right == null)) {
    current = current.right;
  }
  return current.data;
}
// 查找给定值
function find(data) {
  var current = this.root;
  while(current != null) {
    if(current.data == data) {
      return current;
    }else if(data < current.data) {
      current = current.left;
    }else {
      current = current.right;
    }
  }
  return null;
}
function remove(data) {
  root = removeNode(this.root,data);
}
function getSmallest(node) {
  if (node.left == null) {
   return node;
  }
  else {
   return getSmallest(node.left);
  }
}
function removeNode(node,data) {
  if(node == null) {
    return null;
  }
  if(data == node.data) {
    // 没有子节点的节点
    if(node.left == null && node.right == null) {
      return null;
    } 
    // 没有左子节点的节点
    if(node.left == null) {
      return node.right;
    }
    // 没有右子节点的节点
    if(node.right == null) {
      return node.left;
    }
    // 有2个子节点的节点
    var tempNode = getSmallest(node.right);
    node.data = tempNode.data;
    node.right = removeNode(node.right,tempNode.data);
    return node;
  }else if(data < node.data) {
    node.left = removeNode(node.left,data);
    return node;
  }else {
    node.right = removeNode(node.right,data);
    return node;
  }
}
//代码初始化如下:
var nums = new BST();
nums.insert(23);
nums.insert(45);
nums.insert(16);
nums.insert(37);
nums.insert(3);
nums.insert(99);
nums.insert(22);
var min = nums.getMin();
console.log(min);
var max = nums.getMax();
console.log(max);
var value = nums.find("45");
console.log(value);
nums.remove(23);

运行结果:

使用JavaScrip怎么为二叉树添加或删除节点

JavaScript的特点

1.JavaScript主要用来向HTML页面添加交互行为。 2.JavaScript可以直接嵌入到HTML页面,但写成单独的js文件有利于结构和行为的分离。 3.JavaScript具有跨平台特性,在绝大多数浏览器的支持下,可以在多种平台下运行。

看完上述内容,你们对使用JavaScrip怎么为二叉树添加或删除节点有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI