用java构建完全二叉树

本文详细讲解如何使用Java编程语言来构建完全二叉树。内容涵盖完全二叉树的概念,以及通过递归和非递归方式实现节点插入,最终形成完整的二叉树结构。同时,还将讨论相关算法的时间复杂度和空间复杂度分析。
摘要由CSDN通过智能技术生成
//树结点类
public class TreeNode {

 private int value;
 private TreeNode leftchild;
 private TreeNode rightchild;
 
 public int getValue() {
  return value;
 }
 public void setValue(int value) {
  this.value = value;
 }
 public TreeNode getLeftchild() {
  return leftchild;
 }
 public void setLeftchild(TreeNode leftchild) {
  this.leftchild = leftchild;
 }
 public TreeNode getRightchild() {
  return rightchild;
 }
 public void setRightchild(TreeNode rightchild) {
  this.rightchild = rightchild;
 }
 }

 

//树类
public class Tree {
 private TreeNode node;

 public TreeNode getNode() {
  return node;
 }

 public void setNode(TreeNode node) {
  this.node = node;
 }
}

//队列

import java.util.LinkedList;


public class Queue {

 private LinkedList<TreeNode> list;
 public LinkedList<TreeNode> getList() {
  return list;
 }
 public void setList(LinkedList<TreeNode> list) {
  this.list = list;
 }
 public Queue(){
  list = new 
/* * 基于向量实现的完全二叉树 */ package dsa; public class ComplBinTree_Vector extends BinTree_LinkedList implements ComplBinTree { private Vector T;//向量 //构造方法:默认的空树 public ComplBinTree_Vector() { T = new Vector_ExtArray(); root = null; } //构造方法:按照给定的节点序列,批量式建立完全二叉树 public ComplBinTree_Vector(Sequence s) { this(); if (null !=s) while (!s.isEmpty()) addLast(s.removeFirst()); } /*---------- BinaryTree接口中各方法的实现 ----------*/ //返回树根(重写) public BinTreePosition getRoot() { return T.isEmpty() ? null : posOfNode(0); } //判断是否树空(重写) public boolean isEmpty() { return T.isEmpty(); } //返回树的规模(重写) public int getSize() { return T.getSize(); } //返回树(根)的高度(重写) public int getHeight() {return isEmpty() ? -1 : getRoot().getHeight(); } /*---------- ComplBinTree接口中各方法的实现 ----------*/ //生成并返回一个存放e的外部节点,该节点成为新的末节点 public BinTreePosition addLast(Object e) { BinTreePosition node = new ComplBinTreeNode_Rank(T, e); root = (BinTreePosition) T.getAtRank(0); return node; } //删除末节点,并返回其中存放的内容 public Object delLast() { if (isEmpty()) return null;//若树(堆)已空,无法删除 if (1 == getSize()) root = null;//若删除最后一个节点,则树空 return T.removeAtRank(T.getSize()-1); } //返回按照层次遍历编号为i的节点的位置,0 <= i < size() public BinTreePosition posOfNode(int i) { return (BinTreePosition)T.getAtRank(i); } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值