二叉树算法(java)

为什么实用二叉树一,在有序数组中插入删除数据太慢   1插入或者删除一条数据会移动后面的所有数据 二,在链表中查找数据太慢  2查找只能从头或者尾部一条一条的找用树解决问题   有没有一种插入和删除像链表那么快,查询可以向有序数组一样查得快那样就好了。 数实现了这些特点,称为了最有意思的数据结构之一树的术语如下图树分平衡树和非平衡树二叉树的类publicclassTree{ …

大家好,又见面了,我是你们的朋友全栈君。为什么实用二叉树

一,在有序数组中插入删除数据太慢
     1插入或者删除一条数据会移动后面的所有数据  
二,在链表中查找数据太慢
    2查找只能从头或者尾部一条一条的找
用树解决问题
     有没有一种插入和删除像链表那么快,查询可以向有序数组一样查得快那样就好了。
 数实现了这些特点,称为了最有意思的数据结构之一
树的术语
如下图
二叉树算法(java)

树分平衡树和非平衡树
二叉树算法(java)

二叉树的类
public class Tree {
	/**
	 * 跟节点
	 */
	private Node root;

	/**
	 * 构造方法
	 */
	public Tree() {

	}

	/**
	 * 构造方法
	 * 
	 * @param root
	 *            跟节点
	 */
	public Tree(Node root) {
		this.root = root;
	}
}
class Node {
    /* key */
    int key;
    /* 值 */
    Object value;
    /* 左节点 */
    Node leftChildNode;
    /* 右节点 */
    Node rightChildNode;

    /**
     * 构造方法
     * 
     * @param key
     *            关键字
     * @param value
     *            值
     */
    public Node(int key, Object value) {
        super();
        this.key = key;
        this.value = value;
    }

}



二叉树插入功能
二叉树算法(java)

/**
	 * 插入节点
	 * 
	 * @param key
	 *            key
	 * @param value
	 *            值
	 */
	public void insert(int key, Object value) {
		Node node = new Node(key, value);
		if (this.root == null) {
			this.root = node;
		} else {
			Node currentNode = this.root;
			while (true) {
				if (key > currentNode.key) {
					if (currentNode.rightChildNode == null) {
						currentNode.rightChildNode = node;
						return;
					} else {
						currentNode = currentNode.rightChildNode;
					}
				} else {
					if (currentNode.leftChildNode == null) {
						currentNode.leftChildNode = node;
						return;
					} else {
						currentNode = currentNode.leftChildNode;
					}
				}
			}
		}

	}



二叉树的查找功能
二叉树算法(java)

	/**
	 * 查找节点
	 * 
	 * @param key
	 * @return
	 */
	public Node find(int key) {
		if (this.root != null) {
			Node currentNode = this.root;
			while (currentNode.key != key) {
				if (key > currentNode.key) {
					currentNode = currentNode.rightChildNode;
				} else {
					currentNode = currentNode.leftChildNode;
				}
				if (currentNode == null) {
					return null;
				}
			}
		}
		return currentNode ;
	}


二叉树的展示功能(中序遍历)
二叉树算法(java)

	private void show(Node node) {
		if (node != null) {
			this.show(node.leftChildNode);
			System.out.println(node.key + ":" + node.value);
			this.show(node.rightChildNode);

		}
	}

测试

public static void main(String[] args) {
		Node root = new Node(50, 24);
		Tree tree = new Tree(root);
		tree.insert(20, 530);
		tree.insert(540, 520);
		tree.insert(4, 540);
		tree.insert(0, 550);
		tree.insert(8, 520);
		tree.show();
	}


二叉树算法(java)

完整代码

package tree;

/**
 * 二叉树
 * 
 * @author JYC506
 * 
 */
public class Tree {
	/**
	 * 跟节点
	 */
	private Node root;

	/**
	 * 构造方法
	 */
	public Tree() {

	}

	/**
	 * 构造方法
	 * 
	 * @param root
	 *            跟节点
	 */
	public Tree(Node root) {
		this.root = root;
	}

	/**
	 * 查找节点
	 * 
	 * @param key
	 * @return
	 */
	public Node find(int key) {
		if (this.root != null) {
			Node currentNode = this.root;
			while (currentNode.key != key) {
				if (key > currentNode.key) {
					currentNode = currentNode.rightChildNode;
				} else {
					currentNode = currentNode.leftChildNode;
				}
				if (currentNode == null) {
					return null;
				}
			}
		}
		return null;
	}

	/**
	 * 插入节点
	 * 
	 * @param key
	 *            key
	 * @param value
	 *            值
	 */
	public void insert(int key, Object value) {
		Node node = new Node(key, value);
		if (this.root == null) {
			this.root = node;
		} else {
			Node currentNode = this.root;
			while (true) {
				if (key > currentNode.key) {
					if (currentNode.rightChildNode == null) {
						currentNode.rightChildNode = node;
						return;
					} else {
						currentNode = currentNode.rightChildNode;
					}
				} else {
					if (currentNode.leftChildNode == null) {
						currentNode.leftChildNode = node;
						return;
					} else {
						currentNode = currentNode.leftChildNode;
					}
				}
			}
		}

	}

	/**
	 * 展示
	 */

	public void show() {
		this.show(root);
	}

	/**
	 * 中序遍历
	 * 
	 * @param node
	 */
	private void show(Node node) {
		if (node != null) {
			this.show(node.leftChildNode);
			System.out.println(node.key + ":" + node.value);
			this.show(node.rightChildNode);

		}
	}

	public static void main(String[] args) {
		Node root = new Node(50, 24);
		Tree tree = new Tree(root);
		tree.insert(20, 530);
		tree.insert(540, 520);
		tree.insert(4, 540);
		tree.insert(0, 550);
		tree.insert(8, 520);
		tree.show();
	}
}

class Node {
	/* key */
	int key;
	/* 值 */
	Object value;
	/* 左节点 */
	Node leftChildNode;
	/* 右节点 */
	Node rightChildNode;

	/**
	 * 构造方法
	 * 
	 * @param key
	 *            关键字
	 * @param value
	 *            值
	 */
	public Node(int key, Object value) {
		super();
		this.key = key;
		this.value = value;
	}

}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/127080.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 大白话图解:什么是 CDN[通俗易懂]

    大白话图解:什么是 CDN[通俗易懂]  618电商节、双十一购物狂欢节,到底是什么在支撑数以万计的秒杀活动?这就不得不提一直隐姓埋名的CDN了,注意不是CSDN,而是CDN,CDN,CDN!  那到底CDN是什么鬼,这还要从西天取经说起……  1300年前,唐僧师徒取经要跋涉十万八千里,历经九九八十一难,一路打怪升级,最终才能修成正果,悟空加冕“斗战胜佛”。  1300年后,西游互联网已经开通,雷音寺…

    2025年6月9日
    4
  • Java语言实现hello world代码[通俗易懂]

    Java语言实现hello world代码[通俗易懂]参考https://blog.csdn.net/yilovexing/article/details/53256713 24种编程语言的HelloWorld程序  Java文档注释Java支持三种注释方式。前两种分别是 // 和 /**/,第三种被称作说明注释,它以 /** 开始,以 */结束。说明注释允许你在程序中嵌入关于程序的信息。你可以使用javadoc工…

    2022年5月8日
    52
  • Okio库的使用

    Okio库的使用Okio库是一个由square公司开发的,其官方简介为,Okiocomplementsjava.ioandjava.niotomakeitmucheasiertoaccess,store,andprocessyourdata.。它补充了java.io和java.nio的不足以更方便的访问、存储及处理数据。1.最新版本及Gradle引用     comp

    2022年6月10日
    47
  • 2.session.setAttribute()和session.getAttribute()区别和联系

    2.session.setAttribute()和session.getAttribute()区别和联系2.session.setAttribute和session.getAttribute()区别和联系在web开发的时候,使用的都是B/S架构,浏览器与服务器直接连接,在服务端就会自动创建一个session对象.。session.setAttribute(“username”,username);》》是将username保存在session中!session的key值为“usern…

    2022年10月17日
    2
  • IIC通信协议详解

    IIC通信协议详解IIC通信协议详解IIC的概述IIC分为软件IIC和硬件IICIIC通信协议空闲状态开始信号与停止信号开始信号程序:IIC的概述IIC:两线式串行总线,它是由数据线SDA和时钟线SCL构成的串行总线,可发送和接收数据。在CPU与被控IC之间、IC与IC之间进行双向传送,高速IIC总线一般可达400kbs以上。时钟线SCL:在通信过程起到控制作用。数据线SDA:用来一位一位的传送数据。IIC分为软件IIC和硬件IIC软件IIC:软件IIC通信指的是用单片机的两个I/O端口模拟出来的IIC,用

    2022年6月10日
    155
  • S3C2440 之SPI

    S3C2440 之SPI概述:S3C2440有两个串行外设SPI接口,SPI具有全双工通信SPI方框图 SPI操作:通过使用SPI接口,S3C2440可以与外部器件同时发送、接收8位数据。当SPI接口为主机时,可以通过设置SPPREn寄存器来设置发送频率,当SPI为从机时,由其它主机提供时钟频率。当程序员写字节数据到SPTDATn寄存器,将同时开始发送和接受,在一些情况下,应该在写字节数据到SPT

    2022年5月2日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号