zoukankan      html  css  js  c++  java
  • javascript

    都是些简单的东西,所以直接上代码了。

    /**
     * Created by huangjacky on 14-10-3.
     */
    function Node(element, left, right) {
        this.element = element;
        this.level = 0;
        this.left = left;
        this.right = right;
    }
    
    function BST() {
        this.root = null;
    }
    BST.prototype = {
        insert: function (element) {
            var n = new Node(element, null, null);
            if (this.root == null) {
                this.root = n;
                n.level = 0;
                return true;
            } else {
                var current = this.root;
                var parent = null;
                while (current != null) {
                    if (element < current.element) {
                        parent = current;
                        current = current.left;
                    } else if (element > current.element) {
                        parent = current;
                        current = current.right;
                    } else {
                        return false;
                    }
                }
                n.level = parent.level + 1;
                if (element < parent.element) {
                    parent.left = n;
    
                } else {
                    parent.right = n;
                }
                return true;
            }
        }, toString: function () {
            return this.inOrder(this.root, function (n) {
                return "	" + n.element + '(' + n.level + ")	";
            });
        }, inOrder: function (n, fn) {// 中序遍历
            if (!n) {
                return '';
            } else {
                return this.inOrder(n.left, fn) + fn(n) + this.inOrder(n.right, fn);
            }
        }, preOrder: function (n, fn) { // 先序遍历
            if (!n) {
                return '';
            } else {
                return fn(n) + this.preOrder(n.left, fn) + this.preOrder(n.right, fn);
            }
        }, postOrder: function (n, fn) {// 后序遍历
            if (!n) {
                return '';
            } else {
                return this.postOrder(n.left, fn) + this.postOrder(n.right, fn) + fn(n);
            }
        }
    };
    
    var a = new BST();
    a.insert(3);
    a.insert(1);
    a.insert(5);
    a.insert(2);
    a.insert(4);
    console.log("inorder: " + a.toString());
    
    var fn = function (n) {
        return "	" + n.element + '(' + n.level + ")	";
    };
    var s1 = a.preOrder(a.root, fn);
    var s2 = a.postOrder(a.root, fn);
    console.log("pre order:" + s1);
    console.log("post order:" + s2);
    
  • 相关阅读:
    初识 Mysql
    Python之协程
    crm 动态一级二级菜单
    admin 后台操作表格
    crm 权限设计
    crm 公户变私户的问题 班级管理 课程管理 学习记录初始化
    crm 添加用户 编辑用户 公户和私户的展示,公户和私户的转化
    crm 数据展示 和分页思想(一)
    python django(forms组件)
    python Django 中间件介绍
  • 原文地址:https://www.cnblogs.com/huangjacky/p/4005317.html
Copyright © 2011-2022 走看看