树的定义java代码 java树形数据结构

建立一个二叉树 , 附带查询代码,JAVA代码import java.util.ArrayList;
// 树的一个节点
class TreeNode {
Object _value = https://www.04ip.com/post/null; // 他的值
TreeNode _parent = null; // 他的父节点 , 根节点没有PARENT
ArrayList _childList = new ArrayList(); // 他的孩子节点
public TreeNode( Object value, TreeNode parent ){
this._parent = parent;
this._value = https://www.04ip.com/post/value;
}
public TreeNode getParent(){
return _parent;
}
public String toString() {
return _value.toString();
}
}
public class Tree {
// 给出宽度优先遍历的值数组,构建出一棵多叉树
// null 值表示一个层次的结束
// "|" 表示一个层次中一个父亲节点的孩子输入结束
// 如:给定下面的值数组:
// { "root", null, "left", "right", null }
// 则构建出一个根节点,带有两个孩子("left","right")的树
public Tree( Object[] values ){
// 创建根
_root = new TreeNode( values[0], null );
// 创建下面的子节点
TreeNode currentParent = _root; // 用于待创建节点的父亲
//TreeNode nextParent = null;
int currentChildIndex = 0; // 表示 currentParent 是他的父亲的第几个儿子
//TreeNode lastNode = null; // 最后一个创建出来的TreeNode,用于找到他的父亲
for ( int i = 2; ivalues.length; i){
// 如果null ,表示下一个节点的父亲是当前节点的父亲的第一个孩子节点
if ( values[i] == null ){
currentParent = (TreeNode)currentParent._childList.get(0);
currentChildIndex = 0;
continue;
}
// 表示一个父节点的所有孩子输入完毕
if ( values[i].equals("|") ){
if ( currentChildIndex 1currentParent._childList.size() ){
currentChildIndex;
currentParent = (TreeNode)currentParent._parent._childList.get(currentChildIndex);
}
continue;
}
TreeNode child = createChildNode( currentParent, values[i] );
}
}
TreeNode _root = null;
public TreeNode getRoot(){
return _root;
}
/**
// 按宽度优先遍历,打印出parent子树所有的节点
private void printSteps( TreeNode parent, int currentDepth ){
for ( int i = 0; iparent._childList.size(); i){
TreeNode child = (TreeNode)parent._childList.get(i);
System.out.println(currentDepth ":" child);
}
if ( parent._childList.size() != 0 )System.out.println("" null);// 为了避免叶子节点也会打印null
//打印 parent 同层的节点的孩子
if ( parent._parent != null ){ // 不是root
int i = 1;
while ( iparent._parent._childList.size() ){// parent 的父亲还有孩子
TreeNode current = (TreeNode)parent._parent._childList.get(i);
printSteps( current, currentDepth );
i;
}
}
// 递归调用,打印所有节点
for ( int i = 0; iparent._childList.size(); i){
TreeNode child = (TreeNode)parent._childList.get(i);
printSteps( child, currentDepth 1 );
}
}
// 按宽度优先遍历 , 打印出parent子树所有的节点
public void printSteps(){
System.out.println("" _root);
System.out.println("" null);
printSteps(_root, 1 );
}**/
// 将给定的值做为 parent 的孩子,构建节点
private TreeNode createChildNode( TreeNode parent, Object value ){
TreeNode child = new TreeNode( value , parent );
parent._childList.add( child );
return child;
}
public static void main(String[] args) {
Tree tree = new Tree( new Object[]{ "root", null,
"left", "right", null,
"l1","l2","l3", "|", "r1","r2",null } );
//tree.printSteps();
System.out.println(""( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(0) );
System.out.println(""( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(1) );
System.out.println(""( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(2) );
System.out.println(""( (TreeNode)tree.getRoot()._childList.get(1) )._childList.get(0) );
System.out.println(""( (TreeNode)tree.getRoot()._childList.get(1) )._childList.get(1) );
}
}
java:二叉树添加和查询方法
package arrays.myArray;
public class BinaryTree {
private Node root;
// 添加数据
public void add(int data) {
// 递归调用
if (null == root)
root = new Node(data, null, null);
else
addTree(root, data);
}
private void addTree(Node rootNode, int data) {
// 添加到左边
if (rootNode.datadata) {
if (rootNode.left == null)
rootNode.left = new Node(data, null, null);
else
addTree(rootNode.left, data);
} else {
【树的定义java代码 java树形数据结构】// 添加到右边
if (rootNode.right == null)
rootNode.right = new Node(data, null, null);
else
addTree(rootNode.right, data);
}
}
// 查询数据
public void show() {
showTree(root);
}
private void showTree(Node node) {
if (node.left != null) {
showTree(node.left);
}
System.out.println(node.data);
if (node.right != null) {
showTree(node.right);
}
}
}
class Node {
int data;
Node left;
Node right;
public Node(int data, Node left, Node right) {
this.data = https://www.04ip.com/post/data;
this.left = left;
this.right = right;
}
}
java如何创建一颗二叉树计算机科学中树的定义java代码,二叉树是每个结点最多有两个子树树的定义java代码的有序树 。通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree) 。二叉树常被用作二叉查找树和二叉堆或是二叉排序树 。
二叉树的每个结点至多只有二棵子树(不存在度大于2的结点)树的定义java代码,二叉树的子树有左右之分,次序不能颠倒 。二叉树的第i层至多有2的 i -1次方个结点;深度为k的二叉树至多有2^(k) -1个结点;对任何一棵二叉树T,如果其终端结点数(即叶子结点数)为n0,度为2的结点数为n2,则n0 = n21 。
树是由一个或多个结点组成的有限集合,其中树的定义java代码:
⒈必有一个特定的称为根(ROOT)的结点;
二叉树
⒉剩下的结点被分成n=0个互不相交的集合T1、T2、......Tn,而且,这些集合的每一个又都是树 。树T1、T2、......Tn被称作根的子树(Subtree) 。
树的递归定义如下:(1)至少有一个结点(称为根)(2)其它是互不相交的子树
1.树的度——也即是宽度,简单地说,就是结点的分支数 。以组成该树各结点中最大的度作为该树的度,如上图的树,其度为2;树中度为零的结点称为叶结点或终端结点 。树中度不为零的结点称为分枝结点或非终端结点 。除根结点外的分枝结点统称为内部结点 。
2.树的深度——组成该树各结点的最大层次 。
3.森林——指若干棵互不相交的树的集合,如上图,去掉根结点A,其原来的二棵子树T1、T2、T3的集合{T1,T2,T3}就为森林;
4.有序树——指树中同层结点从左到右有次序排列,它们之间的次序不能互换 , 这样的树称为有序树,否则称为无序树 。
树的表示
树的表示方法有许多,常用的方法是用括号:先将根结点放入一对圆括号中,然后把它的子树由左至右的顺序放入括号中,而对子树也采用同样的方法处理;同层子树与它的根结点用圆括号括起来,同层子树之间用逗号隔开,最后用闭括号括起来 。如右图可写成如下形式:
二叉树
(a( b(d,e), c( f(,g(h,i) ),)))
用java怎么构造一个二叉树呢?java构造二叉树,可以通过链表来构造 , 如下代码:
public class BinTree {
public final static int MAX=40;
BinTree []elements = new BinTree[MAX];//层次遍历时保存各个节点
int front;//层次遍历时队首
int rear;//层次遍历时队尾
private Object data; //数据元数
private BinTree left,right; //指向左,右孩子结点的链
public BinTree()
{
}
public BinTree(Object data)
{ //构造有值结点
this.data = https://www.04ip.com/post/data;
left = right = null;
}
public BinTree(Object data,BinTree left,BinTree right)
{ //构造有值结点
this.data = https://www.04ip.com/post/data;
this.left = left;
this.right = right;
}
public String toString()
{
return data.toString();
}
//前序遍历二叉树
public static void preOrder(BinTree parent){
if(parent == null)
return;
System.out.print(parent.data " ");
preOrder(parent.left);
preOrder(parent.right);
}
//中序遍历二叉树
public void inOrder(BinTree parent){
if(parent == null)
return;
inOrder(parent.left);
System.out.print(parent.data " ");
inOrder(parent.right);
}
//后序遍历二叉树
public void postOrder(BinTree parent){
if(parent == null)
return;
postOrder(parent.left);
postOrder(parent.right);
System.out.print(parent.data " ");
}
// 层次遍历二叉树
public void LayerOrder(BinTree parent)
{
elements[0]=parent;
front=0;rear=1;
while(frontrear)
{
try
{
if(elements[front].data!=null)
{
System.out.print(elements[front].data" ");
if(elements[front].left!=null)
elements[rear]=elements[front].left;
if(elements[front].right!=null)
elements[rear]=elements[front].right;
front;
}
}catch(Exception e){break;}
}
}
//返回树的叶节点个数
public int leaves()
{
if(this == null)
return 0;
if(left == nullright == null)
return 1;
return (left == null ? 0 : left.leaves()) (right == null ? 0 : right.leaves());
}
//结果返回树的高度
public int height()
{
int heightOfTree;
if(this == null)
return -1;
int leftHeight = (left == null ? 0 : left.height());
int rightHeight = (right == null ? 0 : right.height());
heightOfTree = leftHeightrightHeight?rightHeight:leftHeight;
return 1heightOfTree;
}
//如果对象不在树中,结果返回-1;否则结果返回该对象在树中所处的层次,规定根节点为第一层
public int level(Object object)
{
int levelInTree;
if(this == null)
return -1;
if(object == data)
return 1;//规定根节点为第一层
int leftLevel = (left == null?-1:left.level(object));
int rightLevel = (right == null?-1:right.level(object));
if(leftLevel0rightLevel0)
return -1;
levelInTree = leftLevelrightLevel?rightLevel:leftLevel;
return 1 levelInTree;
}
//将树中的每个节点的孩子对换位置
public void reflect()
{
if(this == null)
return;
if(left != null)
left.reflect();
if(right != null)
right.reflect();
BinTree temp = left;
left = right;
right = temp;
}
// 将树中的所有节点移走,并输出移走的节点
public void defoliate()
{
if(this == null)
return;
//若本节点是叶节点,则将其移走
if(left==nullright == null)
{
System.out.print(this" ");
data = https://www.04ip.com/post/null;
return;
}
//移走左子树若其存在
if(left!=null){
left.defoliate();
left = null;
}
//移走本节点 , 放在中间表示中跟移走...
String innerNode= this" ";
data = https://www.04ip.com/post/null;
//移走右子树若其存在
if(right!=null){
right.defoliate();
right = null;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BinTree e = new BinTree("E");
BinTree g = new BinTree("G");
BinTree h = new BinTree("H");
BinTree i = new BinTree("I");
BinTree d = new BinTree("D",null,g);
BinTree f = new BinTree("F",h,i);
BinTree b = new BinTree("B",d,e);
BinTree c = new BinTree("C",f,null);
BinTree tree = new BinTree("A",b,c);
System.out.println("前序遍历二叉树结果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("中序遍历二叉树结果: ");
tree.inOrder(tree);
System.out.println();
System.out.println("后序遍历二叉树结果: ");
tree.postOrder(tree);
System.out.println();
System.out.println("层次遍历二叉树结果: ");
tree.LayerOrder(tree);
System.out.println();
System.out.println("F所在的层次: " tree.level("F"));
System.out.println("这棵二叉树的高度: " tree.height());
System.out.println("--------------------------------------");
tree.reflect();
System.out.println("交换每个节点的孩子节点后......");
System.out.println("前序遍历二叉树结果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("中序遍历二叉树结果: ");
tree.inOrder(tree);
System.out.println();
System.out.println("后序遍历二叉树结果: ");
tree.postOrder(tree);
System.out.println();
System.out.println("层次遍历二叉树结果: ");
tree.LayerOrder(tree);
System.out.println();
System.out.println("F所在的层次: " tree.level("F"));
System.out.println("这棵二叉树的高度: " tree.height());
}
如何在java构造函数中创建一棵树import java.util.Stack;//导入栈包
public class newtree {
private newtree lchild;// 声明数据成员
private newtree rchild;
private char data;
private newtree root;
public newtree(newtree l, newtree r, char data) {// 有参构造函数进行成员赋值
lchild = l;
rchild = r;
this.data = https://www.04ip.com/post/data;
}
public newtree() {// 无参构造函数创建树
newtree f = new newtree(null, null, 'f');
newtree g = new newtree(null, null, 'g');
newtree d = new newtree(null, null, 'd');
newtree e = new newtree(null, null, 'e');
newtree b = new newtree(d, e, 'b');
newtree c = new newtree(f, g, 'c');
newtree a = new newtree(b, c, 'a');
this.root=a;
}
public void visit(newtree p) {/* 输出数据 */
System.out.print(p.data);// 访问结点
}
@SuppressWarnings("unchecked")
public void InOrder() {/* 输入数据 */
newtree p=this.root;//你建了一棵树要把根节点赋值进去啊
Stack s = new Stack();
while (p != null || !s.isEmpty()) /* 处理数据:进行中序遍历 */
{
if (p != null) {
s.push(p);
p = p.lchild;
} else {
p = (newtree) s.pop();
p.visit(p);//this指的是当前的类对象
p = p.rchild;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
newtree h = new newtree();// 声明变量,变量赋值
h.InOrder();
}
}
//根据你的代码改了一个
import java.util.Stack;//导入栈包
public class newtree {
public Tree createTree() {// 无参构造函数创建树
Tree f = new Tree(null, null, 'f');
Tree g = new Tree(null, null, 'g');
Tree d = new Tree(null, null, 'd');
Tree e = new Tree(null, null, 'e');
Tree b = new Tree(d, e, 'b');
Tree c = new Tree(f, g, 'c');
Tree a = new Tree(b, c, 'a');
return a;
}
public void InOrder(Tree p) {/* 输入数据 */
StackTree s = new StackTree();
while (p != null || !s.isEmpty()) { /* 处理数据:进行中序遍历 */
if (p != null) {
s.push(p);
p = p.lchild;
} else {
p = s.pop();
System.out.print(p.data);
p = p.rchild;
}
}
}
public void inOrder1(Tree p) {
if (p == null)
return;
inOrder1(p.lchild);
System.out.print(p.data);
inOrder1(p.rchild);
}
public static void main(String[] args) {
newtree h = new newtree();// 声明变量,变量赋值
h.InOrder(h.createTree());
System.out.println();
h.inOrder1(h.createTree());
}
}
class Tree {
Tree lchild;// 声明数据成员
Tree rchild;
char data;
Tree(Tree lchild, Tree rchild, char data) {
this.lchild = lchild;
this.rchild = rchild;
this.data = https://www.04ip.com/post/data;
}
}
树的定义java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java树形数据结构、树的定义java代码的信息别忘了在本站进行查找喔 。

    推荐阅读