javascript---数据结构(代码篇)

数据结构

二叉树

二叉树的中序遍历

给定一个二叉树,返回它的 中序 遍历。

输入: [1,null,2,3]
   1
    \
     2
    /
   3
输出: [1,3,2]

解:首先复习一下

先序遍历:根节点,左子树,右子树;

中序遍历:左子树,根节点,右子树

后序遍历:左子树,右子树,根节点

递归

var inorderTraversal = function (root, array = []) {
      if (root) {
        inorderTraversal(root.left, array);
        array.push(root.val);
        inorderTraversal(root.right, array);
      }
      return array;
    };

 

非递归实现

  • 取跟节点为目标节点,开始遍历
  • 1.左孩子入栈 -> 直至左孩子为空的节点
  • 2.节点出栈 -> 访问该节点
  • 3.以右孩子为目标节点,再依次执行1、2、3
var inorderTraversal = function (root) {
      const result = [];
      const stack = [];
      let current = root;
      while (current || stack.length > 0) {
        while (current) {
          stack.push(current);
          current = current.left;
        }
        current = stack.pop();
        result.push(current.val);
        current = current.right;
      }
      return result;
    };

单链表的实现

function LinkedList () {
  //Node类表示要加入列表的项
  var Node = function (element) {
      this.element = element;;//要添加到链表的值
      this.next = null;//指向链表中下一个节点项的指针
  };

  var length = 0;//链表项的数量
  var head = null;//存储第一个节点的引用

  //向链表尾部追加元素
  this.append = function (element) {
       var node = new Node(element),   //创建Node项
           current;

       if (head === null) {//若head元素为null,意味着向链表添加第一个元素
           head = node;
       } else {
           current = head; //查询链表中的元素,需要从起点开始迭代
           while (current.next) {  
           //当current.next元素为null时,就知道已经到达链表的尾部了
               current = current.next;
           }
           current.next = node;
       }
   length++;
  };

  //从链表中移除元素
 this.removeAt = function (position) {
     if (position > -1 && position < length) { //验证位置有效
        var current = head,   //用current变量创建链表中第一个元素引用
           previous, index = 0;
        if (position === 0) {
           head = current.next;  //如果想要移除第一个,就让head指向第二个元素
        } else {
           while (index++ < position) {//迭代直到到达目标位置
               previous = current;
               current = current.next;
           }
           //将previous与current的下一项链接起来:跳过current,从而移除它
           previous.next = current.next;  
        }
        length--;
        return current.element;
     } else {
         return null;
     }   
 };

 //在任意位置插入一个元素
 this.insert = function (positon, element) {
     if (position >= 0 && position <= length) {//检查是否越界
          var node = new Node(element);
          var index = 0, previous, current = head;
          if (position === 0) {//在第一个位置添加
               node.next = current;
               head = node;
          } else {
               while (index++ < position) {
                   previous = current;
                   current = current.next;
               }
               node.next = current;
               previous.next = node;
          }
          length++;//更新链表长度
          return true;
     } else {  
         return false;//如果越界返回false
     }
 }; 

 //把LinkedList对象转换成一个字符串
 this.toString = function () {
     var current = head, //起点
         string = '';
     while (current) {//检查元素是否存在
         string += ', ' + current.element;//拼接到字符串
         current = current.next;     
     }
     return string.slice(1);
 };

 //indexOf方法
 this.indexOf = function (element) {
     var current = head,
         index = 0;//计算位置数
     while (current) {//循环访问元素
        if (element === current.element) {//检查当前元素是否是我们要找的
           return index;
        }
        index++;
        current = current.next;
     }   
     return -1;
 };

 //实现了indexOf方法后可以根据元素的值来移除元素
 this.remove = function (element) {
     var index = this.indexOf(element);
     return this.removeAt(element);
 };

 //isEmpty方法和size方法与栈和队列中实现一样
 this.isEmpty = function () {
     return length === 0;//如果列表中没有元素,isEmpty方法就返回true,否则返回false
 };
 this.size = function () {
     return length;
 };

 /**
  *head变量是LinkedList类的私有变量
  *意味着它不能在LinkedList实例外部访问和更改,只有通过LinkedList实例才可以
  */
 this.getHead = function () {
     return head;
 };
 //反转链表
 this.reverse=function(){
  let tempNode=null;
  let ansNode=head;
  while(head&&head.next){
      tempNode=head.next;
      head.next=tempNode.next;
      tempNode.next=ansNode;
      ansNode=tempNode;
      
  }
  return ansNode;
 }
}
let list=new LinkedList();
list.append(9);
list.append(8);
list.append(3);
list.append(5);
console.log(list.toString());
console.log(list.reverse());

 

你可能感兴趣的:(前端,面试算法刷题记录)