-
二叉树(二叉链表实现)JAVA代码
publicclassTest{
publicstaticvoid main(String[] args){
char[] ch =newchar[]{'A','B','D','#','#','G','#','#','C','J','#','#','M','#','#'};
BinaryTree binaryTree =newBinaryTree(ch);
binaryTree.preOrder();
System.out.println();
binaryTree.inOrder();
System.out.println();
binaryTree.postOrder();
System.out.println();
binaryTree.levelOrder();
System.out.println();
}
}
publicclassBinaryTree{
//二叉树结点
publicclassBiNode{
char data;
BiNode left;
BiNode right;
BiNode(char data,BiNode left,BiNode right){
this.data = data;
this.left = left;
this.right = right;
}
int flag =0;//供非递归后序遍历使用
}
//根结点
publicBiNode root;
privatestaticint i;
publicBinaryTree(char[] pre){
i =0;
root = create(pre);
}
//初始化(先序遍历顺序存放数组、'#'表示null)
privateBiNode create(char[] pre)
{
if(i < pre.length)
{
if(pre[i]=='#') //结点为空
{
i++;
return null;
}
BiNode p =newBiNode(pre[i], null, null); //结点非空
i++;
p.left = create(pre); //递归建立左子树
p.right = create(pre); //递归建立右子树
return p;
}
return null;
}
//先序遍历
publicvoid preOrder(){
System.out.print("preOrder traversal with recursion:");
preOrder(root);
System.out.println();
System.out.print("preOrder traversal without recursion:");
preOrder2(root);
System.out.println();
}
//递归
privatevoid preOrder(BiNode root){
if(root == null)return;
System.out.print(root.data); //访问结点
preOrder(root.left);
preOrder(root.right);
}
//非递归
privatevoid preOrder2(BiNode head){
LinkedList<BiNode> s =newLinkedList<BiNode>();
while(head != null ||!s.isEmpty())
{
while(head != null) //访问左子树
{
System.out.print(head.data); //访问左子树
s.push(head); //结点入栈(待后面找其右子树使用)= =(“递归”)
head = head.left;
}
if(!s.isEmpty()) //转向右子树
{
head = s.peek().right; //转向右子树
s.pop(); //结点出栈(已经找到其右子树)= =(“递归结束”)
}
}
}
//中序遍历
publicvoid inOrder(){
System.out.print("inOrder traversal with recursion:");
inOrder(root);
System.out.println();
System.out.print("inOrder traversal without recursion:");
inOrder2(root);
System.out.println();
}
//递归
privatevoid inOrder(BiNode root){
if(root == null)return;
inOrder(root.left);
System.out.print(root.data); //访问结点
inOrder(root.right);
}
//非递归
privatevoid inOrder2(BiNode head){
LinkedList<BiNode> s =newLinkedList<BiNode>();
while(head != null ||!s.isEmpty())
{
while(head != null) //左子树入栈
{
s.push(head); //结点入栈(待后面找其右子树使用)= =(“递归”)
head = head.left;
}
System.out.print(s.peek().data); //访问左子树
if(!s.isEmpty()) //转向右子树
{
head = s.peek().right; //转向右子树
s.pop(); //结点出栈(已经找到其右子树)= =(“递归结束”)
}
}
}
//后序遍历
publicvoid postOrder(){
System.out.print("postOrder traversal with recursion:");
postOrder(root);
System.out.println();
System.out.print("postOrder traversal without recursion:");
postOrder2(root);
System.out.println();
}
//递归
privatevoid postOrder(BiNode root){
if(root == null)return;
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data); //访问结点
}
//非递归
//后序遍历特点:递归左右子树后,还需访问结点:
//1、左子树入栈
//2、“两次出栈”(用flag标记模仿):第一次是为了找到左子树相应的右子树结点;第二次是为了访问结点
privatevoid postOrder2(BiNode head){
LinkedList<BiNode> s =newLinkedList<BiNode>();
while(head != null ||!s.isEmpty())
{
while(head != null) //左子树入栈
{
head.flag =1;
s.push(head); //结点连同flag入栈(待后面找其右子树使用)= =(“递归”)
head = head.left;
}
while(!s.isEmpty()&& s.peek().flag ==2) //若flag为2(已经找到其右子树出过一次栈),访问结点
{
System.out.print(s.peek().data); //访问结点元素
s.pop(); //(第二次“结点出栈”)实际结点出栈(已经访问结点元素)= =(“递归结束”)
}
if(!s.isEmpty()) //flag为1,转向右子树
{
head = s.peek().right; //转向右子树
s.peek().flag =2; //(第一次“flag模拟出栈”)标记为2,但实际结点不出栈(已经找到其右子树)
}
}
}
//层序遍历
publicvoid levelOrder(){
levelOrder(root);
}
privatevoid levelOrder(BiNode root){
LinkedList<BiNode>queue=newLinkedList<BiNode>(); //LinkedList实现了Queue接口
BiNode p = root;
while(p != null){
System.out.print(p.data); //访问结点
if(p.left != null)
queue.add(p.left);
if(p.right != null)
queue.add(p.right);
p =queue.poll(); //队头出队并返回为p
}
}
//在p结点后插入data
publicvoid insert(BiNode p,char data, boolean left){
if(p != null){
if(left) //插入位置为左孩子
p.left =newBiNode(data,p.left,null);
else //插入位置为右孩子
p.right =newBiNode(data,p.right,null);
}
}
//删除p的一个子树
publicvoiddelete(BiNode p, boolean left){
if(p != null){
if(left) //删除目标为左子树
p.left = null;
else //删除目标为右子树
p.right = null;
}
}
}
-
相关阅读:
python split的用法
大学排名爬虫
一直在报错:ModuleNotFoundError: No module named 'bs4'.
微信小程序部署到线上环境
mybatis关联三张表查询对应字段名
WPF图像裁剪控件
git创建标签并推送到远程
Windows7、10的热键屏蔽
WPF使用SVG简单整理
Visual Studio 注册码和下载
-
原文地址:https://www.cnblogs.com/Doing-what-I-love/p/5535035.html
Copyright © 2020-2023
润新知