erlang的非平衡的二叉树的操作
-module(tree1). -export([test1/0]). lookup(Key,nil) ->
not_found;
lookup(Key,{Key,Value,_,_}) ->
{found,Value};
lookup(Key,{Key1,_,Smaller,_}) when Key < Key1 ->
lookup(Key,Smaller);
lookup(Key,{Key1,_,_,Bigger}) when Key > Key1 ->
lookup(Key,Bigger). insert(Key,Value ,nil) ->
{Key,Value,nil,nil};
insert(Key,Value,{Key,_,Smaller,Bigger}) ->
{Key,Value,Smaller,Bigger};
insert(Key,Value,{Key1,V,Smaller,Bigger}) when Key < Key1 ->
{Key1,V,insert(Key,Value,Smaller),Bigger};
insert(Key,Value,{Key1,V,Smaller,Bigger}) when Key > Key1 ->
{Key1,V,Smaller,insert(Key,Value,Bigger)}.
write_tree(T) ->
write_tree(0,T).
write_tree(D,nil) ->
% io:tab(D),
io:format('nil',[]);
write_tree(D,{Key,Value,Smaller,Bigger}) ->
D1 = D +4 ,
write_tree(D1,Bigger),
io:format('~n',[]),
%io:tab(D),
io:format('~w ==> ~w~n',[Key,Value]),
write_tree(D1,Smaller).
test1() ->
S1=nil,
S2=insert(4,joe,S1),
S3=insert(10,fred,S2),
S4=insert(3,jane,S3),
S5=insert(7,kalle,S4),
S6=insert(6,thomes,S5),
S7=insert(5,rickard,S6),
S8=insert(9,susan,S7),
S9=insert(2,tobbe,S8),
S10=insert(8,dan,S9),
write_tree(S10).
erlang的非平衡的二叉树的操作的更多相关文章
- 数据结构与算法系列研究五——树、二叉树、三叉树、平衡排序二叉树AVL
树.二叉树.三叉树.平衡排序二叉树AVL 一.树的定义 树是计算机算法最重要的非线性结构.树中每个数据元素至多有一个直接前驱,但可以有多个直接后继.树是一种以分支关系定义的层次结构. a.树是n ...
- 非递归遍历二叉树Java实现
2018-10-03 20:16:53 非递归遍历二叉树是使用堆栈来进行保存,个人推荐使用双while结构,完全按照遍历顺序来进行堆栈的操作,当然在前序和后序的遍历过程中还有其他的压栈流程. 一.Bi ...
- JAVA递归、非递归遍历二叉树(转)
原文链接: JAVA递归.非递归遍历二叉树 import java.util.Stack; import java.util.HashMap; public class BinTree { priva ...
- ARM 非对齐的数据访问操作
I’m confused about unaligned memory accesses on ARM. My understanding was that they’re not allowed — ...
- 算法:非平衡二叉搜索树(UnBalanced Binary Search Tree)
背景 很多场景下都需要将元素存储到已排序的集合中.用数组来存储,搜索效率非常高: O(log n),但是插入效率比较低:O(n).用链表来存储,插入效率和搜索效率都比较低:O(n).如何能提供插入和搜 ...
- 非递归遍历二叉树Java版的实现代码(没写层次遍历)
直接上代码呵呵,里面有注解 package www.com.leetcode.specificProblem; import java.util.ArrayList; import java.util ...
- 二叉树的操作--C语言实现
树是一种比较复杂的数据结构,它的操作也比较多.常用的有二叉树的创建,遍历,线索化,线索化二叉树的遍历,这些操作又可以分为前序,中序和后序.其中,二叉树的操作有递归与迭代两种方式,鉴于我个人的习惯,在这 ...
- C中二叉排序树的非递归和递归插入操作以及中序遍历代码实现【可运行】
C中二叉排序树的非递归和递归插入操作以及中序遍历代码实现[可运行] #include <stdio.h> #include <stdlib.h> typedef int Key ...
- 非递归实现二叉树的三种遍历操作,C++描述
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
随机推荐
- static静态变量-投票案例
public class Voter { String name; //名字 private static int count; //投票数 public Voter() {} public Vote ...
- Java多线程设计模式(4)线程池模式
前序: Thread-Per-Message Pattern,是一种对于每个命令或请求,都分配一个线程,由这个线程执行工作.它将“委托消息的一端”和“执行消息的一端”用两个不同的线程来实现.该线程模式 ...
- ife2015-task2-javascript-util.js
util.js/** * Created by Administrator on 2016/12/14. *///判断是否为数组function isArray(arr){ return (arr i ...
- selenium _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0
在关闭driver时,如果用close,而不是用quit,会出现如下错误: Exception ignored in: <bound method Popen.__del__ of <su ...
- iOS:自定义模态动画 --UIPresentationController
UIPresentationController :展示控制器,是iOS8的一个新特性,用来展示模态窗口的.它是所有模态控制器的管理者. 即: 1> 管理所有Modal出来的控制器 2> ...
- docker开发之pyudev模块用法
一.实现功能:获取docker_id #docker数据源: [root@docker scripts]# docker ps -a CONTAINER ID IMAGE COMMAND CREATE ...
- python virtualenv virtualenvwrapper
python中的virtualenv模块能够将项目环境分隔开,而不是使用全局的环境,非常实用. 首先pip install virtualenv 如何创建一个环境virtualenv testvir ...
- 线性判别分析(Linear Discriminant Analysis, LDA)算法初识
LDA算法入门 一. LDA算法概述: 线性判别式分析(Linear Discriminant Analysis, LDA),也叫做Fisher线性判别(Fisher Linear Discrimin ...
- Git 历险记(三)——创建一个自己的本地仓库
如果我们要把一个项目加入到Git的版本管理中,可以在项目所在的目录用git init命令建立一个空的本地仓库,然后再用git add命令把它们都加入到Git本地仓库的暂存区(stage or inde ...
- Spring MVC 解读——<mvc:annotation-driven/>
Spring MVC 解读——<mvc:annotation-driven/> 一.AnnotationDrivenBeanDefinitionParser 通常如果我们希望通过注解的方式 ...