博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二叉树的实现(Java语言描述)
阅读量:7125 次
发布时间:2019-06-28

本文共 1772 字,大约阅读时间需要 5 分钟。

实现二叉树   并先序遍历之。

package 二叉树的实现;public class BinaryTree
{ class Node { int value; // 该节点存储的值。 Node leftChild; // 指向左子节点的引用。 Node rightChild; // 指向右子节点的引用。 Node(int value) { this.value = value; leftChild = null; rightChild = null; } } private Node root; // 根节点。 BinaryTree() { root = null; } BinaryTree(int[] arr) { for (int i : arr) { insert(i); } } private void insert(int value) { root = insert(root, value); } private Node insert(Node node, int value) { if (node == null) { node = new Node(value); } else { if (value <= node.value) { node.leftChild = insert(node.leftChild, value); } else { node.rightChild = insert(node.rightChild, value); } } return node; } private void visit(Node node) { if (node == null) { return; } int value = node.value; System.out.println(value); } private void preOrderTravels(Node node) { if (node == null) { return; } else { visit(node); preOrderTravels(node.leftChild); preOrderTravels(node.rightChild); } } public void preOrderTravels() { preOrderTravels(root); }}

 

package 二叉树的实现;import java.util.*;public class Treetest {	public static void main(String[] args) {		Scanner scan = new Scanner(System.in);		int[] arr = new int[10];		int m,n;		for(int i=0; i<10; i++){			arr[i] = scan.nextInt();		}		BinaryTree
tree = new BinaryTree
(arr); tree.preOrderTravels(); } }

 

转载于:https://www.cnblogs.com/wxisme/p/4363777.html

你可能感兴趣的文章
C语言realloc,malloc,calloc的区别【转载】
查看>>
SQL中采用Newtonsoft.Json处理json字符串
查看>>
jspace2d——A free 2d multiplayer space shooter
查看>>
Form 重置记录编号(app_record.for_all_record)
查看>>
VC之美化界面(内容覆盖十分全面,经典)
查看>>
国外经典设计:12个漂亮的移动APP网站案例
查看>>
pl/sql programming 15 数据提取
查看>>
DBCP(一)数据源配置文件
查看>>
C++ map 映照容器
查看>>
重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件
查看>>
[WinAPI] API 13 [遍历指定目录 打印文件和其他属性]
查看>>
字对齐、半字对齐、字节对齐的理解
查看>>
杀毒软件导致YourSQLDba备份失败
查看>>
struts2漏洞原理及解决办法
查看>>
利用cca进行fmri分析
查看>>
Oracle Dataguard之switchover
查看>>
Step-by-Step XML Free Spring MVC 3 Configuration--reference
查看>>
关于LD_DEBUG (转载)
查看>>
linux2.6.30.4内核移植(4)——完善串口驱动
查看>>
Timer&TimerTask原理分析
查看>>