数据结构与算法——JS实现二叉树前序、中序、后序遍历以及根据前中、中后获得二叉树

JavaScript实现二叉树前序、中序、后序遍历

  1. 实现前序遍历
  2. 实现中序遍历
  3. 实现后序遍历
  4. 根据前序遍历、中序遍历获得二叉树
  5. 根据中序遍历、后序遍历获得二叉树

二叉树
在这里插入图片描述

二叉树的前序遍历

function Node(value) {
    this.value = value;
    this.leftChild = null;
    this.rightChild = null;
}

var a = new Node("a");
var b = new Node("b");
var c = new Node("c");
var d = new Node("d");
var e = new Node("e");
var f = new Node("f");
var g = new Node("g");
a.leftChild = c;
a.rightChild = b;
c.leftChild = f;
c.rightChild = g;
b.leftChild = d;
b.rightChild = e;

function qianxubianli(Node) {
    if (Node === null) return;
    console.log(Node.value)
    qianxubianli(Node.leftChild)
    qianxubianli(Node.rightChild)
}
qianxubianli(a)

中序遍历

前面相同的Node设置就不重复写了。

function zhongxubianli(Node) {
    if (Node === null) return;
    zhongxubianli(Node.leftChild);
    console.log(Node.value);
    zhongxubianli(Node.rightChild)
}

zhongxubianli(a)

后序遍历

function houxubianli(Node) {
    if (Node === null) return;
    houxubianli(Node.leftChild);
    houxubianli(Node.rightChild)
    console.log(Node.value);
}

houxubianli(a)

根据前序遍历、中序遍历获得二叉树

function Node(value) {
    this.value = value;
    this.leftChild = null;
    this.rightChild = null;
}
const qianxu = ["a", "c", "f", "g", "b", "d", "e"];
const zhongxu = ["f", "c", "g", "a", "d", "b", "e"];

function fn(qianxu, zhongxu) {
    if (qianxu == null || zhongxu == null || qianxu.length == 0 || zhongxu.length == 0 || qianxu.length !== zhongxu.length) return;
    var root = new Node(qianxu[0])
    var index = zhongxu.indexOf(root.value);
    var qianxuLeft = qianxu.slice(1, index + 1);
    var qianxuRight = qianxu.slice(index + 1, qianxu.length);
    var zhongxuLeft = zhongxu.slice(0, index);
    var zhongxuRight = zhongxu.slice(index + 1, zhongxu.length);
    root.leftChild = fn(qianxuLeft, zhongxuLeft);
    root.rightChild = fn(qianxuRight, zhongxuRight);
    return root;
}
var root = fn(qianxu, zhongxu);
console.log(root.leftChild)
console.log(root.rightChild)

根据中序遍历、后序遍历获得二叉树

function Node(value) {
    this.value = value;
    this.leftChild = null;
    this.rightChild = null;
}
const zhongxu = ["f", "c", "g", "a", "d", "b", "e"];
const houxu = ["f", "g", "c", "d", "e", "b", "a"];

function fn(houxu, zhongxu) {
    if (houxu == null || zhongxu == null || houxu.length == 0 || zhongxu.length == 0 || houxu.length !== zhongxu.length) return;
    var root = new Node(houxu[houxu.length - 1]);
    var index = zhongxu.indexOf(root.value);
    var houLeft = houxu.slice(0, index);
    var houRight = houxu.slice(index, houxu.length - 1);
    var zhongLeft = zhongxu.slice(0, index);
    var zhongRight = zhongxu.slice(index + 1, zhongxu.length);
    root.leftChild = fn(houLeft, zhongLeft);
    root.rightChild = fn(houRight, zhongRight);
    return root;
}
var root = fn(houxu, zhongxu);
console.log(root.leftChild)
console.log(root.rightChild)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值