java集合:二叉树
来源:互联网 发布:mirai僵尸网络 编辑:程序博客网 时间:2024/06/03 23:40
- 二叉树结点
// Class for storing a single node of a binary tree of intspublic class IntTreeNode { public int data; public IntTreeNode left; public IntTreeNode right; // constructs a leaf node with given data public IntTreeNode(int data) { this(data, null, null); } // constructs a branch node with given data, left subtree, // right subtree public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) { this.data = data; this.left = left; this.right = right; }}
- 二叉树
public class IntTree { private IntTreeNode overallRoot; // pre : max > 0 // post: constructs a sequential tree with given number of // nodes public IntTree(int max) { if (max <= 0) { throw new IllegalArgumentException("max: " + max); } overallRoot = buildTree(1, max); } // post: returns a sequential tree with n as its root unless // n is greater than max, in which case it returns an // empty tree private IntTreeNode buildTree(int n, int max) { if (n > max) { return null; } else { return new IntTreeNode(n, buildTree(2 * n, max), buildTree(2 * n + 1, max)); } } // post: prints the tree contents using a preorder traversal public void printPreorder() { System.out.print("preorder:"); printPreorder(overallRoot); System.out.println(); } // post: prints the tree contents using a preorder traversal // post: prints in preorder the tree with given root private void printPreorder(IntTreeNode root) { if (root != null) { System.out.print(" " + root.data); printPreorder(root.left); printPreorder(root.right); } } // post: prints the tree contents using a inorder traversal public void printInorder() { System.out.print("inorder:"); printInorder(overallRoot); System.out.println(); } // post: prints in inorder the tree with given root private void printInorder(IntTreeNode root) { if (root != null) { printInorder(root.left); System.out.print(" " + root.data); printInorder(root.right); } } // post: prints the tree contents using a postorder traversal public void printPostorder() { System.out.print("postorder:"); printPostorder(overallRoot); System.out.println(); } // post: prints in postorder the tree with given root private void printPostorder(IntTreeNode root) { if (root != null) { printPostorder(root.left); printPostorder(root.right); System.out.print(" " + root.data); } } // post: prints the tree contents, one per line, following an // inorder traversal and using indentation to indicate // node depth; prints right to left so that it looks // correct when the output is rotated. public void printSideways() { printSideways(overallRoot, 0); } // post: prints in reversed preorder the tree with given // root, indenting each line to the given level private void printSideways(IntTreeNode root, int level) { if (root != null) { printSideways(root.right, level + 1); for (int i = 0; i < level; i++) { System.out.print(" "); } System.out.println(root.data); printSideways(root.left, level + 1); } }}
0 0
- java集合:二叉树
- Java二叉树问题集合
- 二叉树题目集合
- 二叉树的java集合(遍历等)
- 二叉树的集合操作
- 二叉树----set集合容器
- 《数据结构和Java集合框架第三版》读书笔记(七)二叉树
- 《数据结构和Java集合框架第三版》读书笔记(八)二叉搜索树
- 《数据结构和Java集合框架第三版》读书笔记(九)平衡二叉搜索树的旋转
- Java基础---集合框架---迭代器、ListIterator、Vector中枚举、LinkedList、ArrayList、HashSet、TreeSet、二叉树、Comparator
- Java源码集合类TreeMap学习1——数据结构3二叉树创建代码
- Java源码集合类TreeMap学习1——数据结构4平衡二叉树的旋转
- Java源码集合类TreeMap学习1——数据结构4平衡二叉树创建代码
- Java复习笔记——Map运用(检测Random特性,多维集合,二叉树实现)
- 二叉树集合与集合家族,listIterator(),Vector,Stack,迭代器小讲
- 关于二叉树的算法集合
- 基于二叉查找树的集合
- 关于二叉树的算法集合
- Underscore.js 入门
- Android so堆栈信息定位行号
- Unity学习日记-hello world
- 操作帧缓冲设备--LCD的操作
- 程序中使用backtrace打印调用栈
- java集合:二叉树
- 装载GPSspg Api
- Java 抽象类
- hdoj1421搬寝室
- mysql连接查询和in的效率取舍
- java 实现快速排序法
- 期末考计划
- linux环境下gsoap调用过程中的超时设置
- MySQLdb模块用法