您现在的位置是:首页 > 后台技术 > 数据结构与算法数据结构与算法
二叉排序树的创建(图文)
第十三双眼睛2023-10-21【数据结构与算法】人已围观
简介二叉排序树的创建
二叉排序树的创建
package com.xingchen.day013; public class BinSortTreeDemo { public static void main(String[] args) { int[] arr = {7,3,10,12,5,1,9}; BinSortTree binSortTree = new BinSortTree(); for (int i = 0; i<arr.length; i++) { binSortTree.add(new Node(arr[i])); } binSortTree.infixOrder(); } } class BinSortTree{ public Node root; public void add(Node node) { if (root == null) { root = node; } else { root.add(node); } } public void infixOrder() { if (root != null) { root.infixOrder(); } else { System.out.println("二叉排序树为空"); } } } class Node { public int value; public Node left; public Node right; public Node(int value) { this.value = value; } public void add(Node node) { if (node == null) { return; } if (node.value < this.value) { if (this.left == null) { this.left = node; } else { this.left.add(node); } } else { if (this.right == null) { this.right = node; } else { this.right.add(node); } } } public void infixOrder() { if (this.left != null) { this.left.infixOrder(); } System.out.println(this); if (this.right != null) { this.right.infixOrder(); } } @Override public String toString() { return "Node{" + "value=" + value + '}'; } } |
Tags:
很赞哦! ()
上一篇:赫夫曼编码解码(图文)
下一篇:二叉排序树节点的删除(图文)
相关文章
随机图文
-
有效的括号(图文)
有效的括号 给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。 有效字符串需满足: 左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 每个右括号都有一个对应的相同类型的左括号。 -
删除排序链表中的重复元素(图文)
删除排序链表中的重复元素 给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。 -
基数排序法(图文)
基数排序法 -
栈实现队列(图文)
栈实现队列 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty): 实现 MyQueue 类: void push(int x) 将元素 x 推到队列的末尾 int pop() 从队列的开头移除并返回元素 int peek() 返回队列开头的元素 boolean empty() 如果队列为空,返回 true ;否则,返回 false