//节点类
class NodeTree{
public static NodeTree first;
public String Value;
public NodeTree leftChild;
public NodeTree rightChild;
public NodeTree(){
}
}
public class tree {
/*
前序(根-左-右):ABCDEF
中序(左-根-右):CBDAEF
后序(左-右-根):CDBFEA
*/
public static void main(String []args){
String a="ABCDEF";
String b="CBDAEF";
buildTree(a,b,null);
new tree().haha(NodeTree.first);
}
//后序遍历二叉树
public void haha(NodeTree n){
if(n.leftChild!=null){
haha(n.leftChild);
}
if(n.rightChild!=null){
haha(n.rightChild);
}
System.out.print(n.Value);
}
//递归建树
public static NodeTree buildTree(String frontChar,String behindChar,NodeTree father){
NodeTree nodeTree=new NodeTree();
if(father==null){
//创立根节点并标记
NodeTree.first=nodeTree;
}
nodeTree.Value=String.valueOf(frontChar.charAt(0));
//判断是否有子节点
if(frontChar.length()==1||frontChar==""){
return nodeTree ;
}
int head=0;
int mid=behindChar.indexOf(frontChar.charAt(head));
//划分左子树
String a=frontChar.substring(1, mid+1);
String b=behindChar.substring(0,mid);
//左节点的值 判断是否有左子树
if(a.length()>=1&&b.length()>=1)
nodeTree.leftChild=buildTree(a,b,nodeTree);
//划分右子树
String c=frontChar.substring(mid+1, frontChar.length());
String d=behindChar.substring(mid+1,behindChar.length());
//右节点的值 判断是否有右子树
if(c.length()>=1&&d.length()>=1)
nodeTree.rightChild=buildTree(c,d,nodeTree);
//返回本层节点 作为上层递归的子节点
return nodeTree;
}
}
简单的二叉树练习
时间: 2025-01-09 05:59:39
简单的二叉树练习的相关文章
编写最简单的二叉树
二叉树结构 源码 -swift- // // Node.swift // swift-TreeStructure // // Created by YouXianMing on 15/10/19. // Copyright © 2015年 ZiPeiYi. All rights reserved. // import UIKit class Node: NSObject { /// 节点名字 var nodeName : String? /// 左节点 var leftNode : Node?
c++简单实现二叉树
专业术语: 节点 父节点 根节点 子孙 堂兄弟 深度: 从根节点到最底层节点的层数称为深度 叶子节点: 没有子节点的节点称为叶子节点 非终端节点: 实际就是非叶子节点 度: 子节点的个数称为度 树的分类: 一般树 任意一个节点的子节点个数都不受限制 二叉树 任意一个节点的子节点的个数最多是两个 且子节点的位置不可更改 分类: 一般二叉树 满二叉树 在不添加树的层数的情况下每个子节点都满了不能再多一个节点 完全二叉树 如果只删除了满二叉树最底层最右边的连续若干个节点这样形成的二叉树就是完全二叉树
golang简单实现二叉树的数据添加和遍历
代码实现 package tree import "fmt" type Node struct { elem interface{} left, right *Node } type Tree struct { root *Node } func NewTree() *Tree { return &Tree{} } // 添加元素 func (this *Tree) Add(v interface{}) { node := &Node{elem: v} if this.
简单的二叉树的创建(前序输入)&;前序遍历&;中序遍历&;后序遍历
#include <stdio.h> #include <stdlib.h> #define MAX 1024 typedef struct bitnode { int data; struct bitnode *lchild; struct bitnode *rchild; }BinTree; BinTree *Creat_Bintree() { BinTree *p; int x; scanf("%d",&x); if(x==0) { p = NUL
JS二叉树的简单实现方法示例【转载】
简单的二叉树实现,并且实现升序和降序排序输出 function Node(data , left,right){ this.data = data; this.left = left; this.right = right; this.show = show; function show(){ return this.data; } }; function Bst(){ this.root = null; this.insert = insert;//插入 this.inOrder = inOr
数据结构(三):非线性逻辑结构-二叉树
接着上一次对非线性逻辑数据结构树的内容,开启对二叉树的深入复习和总结.首先还是先回顾一下几个重要的概念: 一.回顾 1. 满二叉树与完全二叉树 满二叉树指的是除了叶子节点外所有的节点都有两个子节点.这样可以很容易的计算出满二叉树的深度,要掌握满二叉树的一些性质. 完全二叉树则是从满二叉树继承而来,指的所有的节点按照从上到下,从左到右的层次顺序依次排列所构成的二叉树称之为完全二叉树.所以可以想象,对于深度为h的完全二叉树,前h-1层可以构成深度为h-1的满二叉树,而对于第h层则是从左到右连续排列的
Invert a binary tree 翻转一棵二叉树
假设有如下一棵二叉树: 4 / \ 2 7 / \ / \ 1 3 6 9翻转后: 4 / \ 7 2 / \ / \ 9 6 3 1 这里采用递归的方法来处理.遍历结点,将每个结点的两个子结点交换位置即可. 从左子树开始,层层深入,由底向上处理结点的左右子结点:然后再处理右子树 全部代码如下: public class InvertBinaryTree { public static void main(String args
二叉树的基本结构与实现
实现了二叉树类似链表的一种结构,主要是用两个函数进行添加左右节点,同时每次添加都返回新加上的节点地址,我觉得应该可以进行递归式的动态添加,但是我没有实现. 下面是最简单的二叉树的一些实现操作. BinaryTree.cpp 1 #include "iostream" 2 #include "stdlib.h" 3 4 typedef struct _btree_ 5 { 6 char *data; 7 struct _btree_ *leftTree; 8 stru
二叉树遍历,递归,栈,Morris
一篇质量非常高的关于二叉树遍历的帖子,转帖自http://noalgo.info/832.html 二叉树遍历(递归.非递归.Morris遍历) 2015年01月06日 | 分类:数据结构 | 标签:二叉树遍历 | 评论:8条评论 | 浏览:6,603次 二叉树遍历是二叉树中最基本的问题,其实现的方法非常多,有简单粗暴但容易爆栈的递归算法,还有稍微高级的使用栈模拟递归的非递归算法,另外还有不用栈而且只需要常数空间和线性时间的神奇Morris遍历算法,本文将对这些算法进行讲解和实现. 递归