前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >二叉树算法(java)

二叉树算法(java)

作者头像
全栈程序员站长
发布2022-07-25 09:29:51
2260
发布2022-07-25 09:29:51
举报
文章被收录于专栏:全栈程序员必看

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

一,在有序数组中插入删除数据太慢

1插入或者删除一条数据会移动后面的所有数据

二,在链表中查找数据太慢

2查找只能从头或者尾部一条一条的找

用树解决问题

有没有一种插入和删除像链表那么快,查询可以向有序数组一样查得快那样就好了。

数实现了这些特点,称为了最有意思的数据结构之一

树的术语

如下图

二叉树算法(java)
二叉树算法(java)

树分平衡树和非平衡树

二叉树算法(java)
二叉树算法(java)

二叉树的类

代码语言:javascript
复制
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)
二叉树算法(java)
代码语言:javascript
复制
/**
	 * 插入节点
	 * 
	 * @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)
二叉树算法(java)
代码语言:javascript
复制
	/**
	 * 查找节点
	 * 
	 * @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)
二叉树算法(java)
代码语言:javascript
复制
	private void show(Node node) {
		if (node != null) {
			this.show(node.leftChildNode);
			System.out.println(node.key + ":" + node.value);
			this.show(node.rightChildNode);

		}
	}

测试

代码语言:javascript
复制
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)
二叉树算法(java)

完整代码

代码语言:javascript
复制
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.cn/127080.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月9,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档