- 浏览: 102871 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
-
dreamoftch:
...
对hibernate的理解 -
quanwsx:
对hibernate的理解 -
zxt1985:
太坑爹了……啥都没
**java网络编程 -
Java_zhou:
坑爹啊。。。
**java网络编程 -
juda:
this code can not work rightly ...
Reverse String
第一题:
在一棵一般的二叉树中找到指定的元素,如果有重复出现的元素,要求元素为深度最深的任何一个。
指定元素找不到时返回EMPTY_NODE,请用C语言实现,相关数据结构与函数声明如下:
struct Node
{
int iValue;
int id;
Node *pLeft;
Node *pRight;
};
const Node EMPTY_NODE = {0, 0, NULL, NULL};
Node findDeepest(Node *pRoot, int iWanted); //pRoot为根节点,wanted为指定元素的iValue
-----------------------------------------------------------------------------------------------------------------
第二题:
(1)写一个建立二叉树的算法。(2)写一个判别给定的二叉树是否是完全二叉树的算法。
(3)判断是否为满二叉树
完全二叉树定义为:深度为K,具有N 个结点的二叉树的每个结点都与深度为K 的满二叉树中编号从
1 至N 的结点一一对应。此题以此定义为准。
--------------------------------------------------------------------------------------------------------------------
第三题:求二叉树节点的最大距离
package org.jyjiao;
import java.util.LinkedList;
class BiTreeNode {
char data;
BiTreeNode leftChild, rightChild;
public BiTreeNode() {
}
public BiTreeNode(char data, BiTreeNode leftChild, BiTreeNode rightChild) {
this.data = data;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
public char getData() {
return data;
}
public void setData(char data) {
this.data = data;
}
public BiTreeNode getLeftChild() {
return leftChild;
}
public void setLeftChild(BiTreeNode leftChild) {
this.leftChild = leftChild;
}
public BiTreeNode getRightChild() {
return rightChild;
}
public void setRightChild(BiTreeNode rightChild) {
this.rightChild = rightChild;
}
}
class BiTree {
BiTreeNode root;
public BiTree() {
root = null;
}
/*
* 创建一棵二叉树
*/
public BiTreeNode createTree() {
BiTreeNode root = null;
BiTreeNode nodeD = new BiTreeNode('d', null, null);
BiTreeNode nodeE = new BiTreeNode('e', null, null);
BiTreeNode nodeF = new BiTreeNode('f', null, null);
BiTreeNode nodeG = new BiTreeNode('g', null, null);
BiTreeNode nodeB = new BiTreeNode('b', nodeD, nodeE);
BiTreeNode nodeC = new BiTreeNode('c', nodeB, nodeG);
BiTreeNode nodeA = new BiTreeNode('a', nodeB, nodeC);
root = nodeA;
return root;
}
/* 获得二叉树的节点个数 */
public int getTreeSize(BiTreeNode root) {
if (root == null) {
return 0;
} else {
return (1 + getTreeSize(root.getLeftChild()) + getTreeSize(root
.getRightChild()));
}
}
/* 获得树的高度 */
public int getTreeHeight(BiTreeNode root) {
if (root == null) {
return -1;
} else {
return 1 + (getTreeHeight(root.getLeftChild()) > getTreeHeight(root
.getRightChild()) ? getTreeHeight(root.getLeftChild())
: getTreeHeight(root.getRightChild()));
}
}
/*
* 判断是否为满二叉树 满二叉树的元素个数为n,高度为h,那么n=2^h-1 (h从1开始计数)
* 层次遍历二叉树,计算元素的个数和高度,最后利用满二叉树的性质判断即可
*/
public boolean isFull(BiTreeNode root) {
boolean ret = false;
int size = this.getTreeSize(root);
System.out.println("size=" + size);
int height = this.getTreeHeight(root);
System.out.println("height=" + height);
if (size == (Math.pow(2, height) - 1)) {
return true;
}
return ret;
}
/*
* 判断是否为完全二叉树 层次遍历二叉树,如果遇到第一个子节点为null后,再有节点的子节点不为null,则不是完全二叉树;否则是完全二叉树。
*/
public boolean isComplete(BiTreeNode root) {
boolean ret = true;
int tag = 0;
int front = 0, rear = 0;
LinkedList<BiTreeNode> biQueue = new LinkedList<BiTreeNode>();
biQueue.add(root);
rear++;
while (front != rear) {
BiTreeNode delNode = biQueue.get(front++);
if (delNode.getLeftChild() != null) {
if (tag == 1) {
return false;
} else {
biQueue.add(delNode.getLeftChild());
rear++;
}
} else {
tag = 1;
}
if (delNode.getRightChild() != null) {
if (tag == 1) {
return false;
}
biQueue.add(delNode.getRightChild());
rear++;
} else {
tag = 1;
}
}
return ret;
}
/* 递归:判断一个节点是否在树中,如果在树中,求该节点在树中的最深的层次;如果不在返回 -1
* 算法缺点:如果要找的节点为根节点,并且下面还有和根节点值相同的节点,只会返回1(根节点所在高度)
*/
public int findDeapestNode(BiTreeNode fNode, BiTreeNode root) {
int leftHeight=-1,rightHeight=-1;
if (root == null) {
return -1;
}
if(root.getData() == fNode.getData()) {
return 1;
}
if(root.getLeftChild()!=null){
leftHeight=this.findDeapestNode(fNode, root.getLeftChild());
}
if(root.getRightChild()!=null){
rightHeight=this.findDeapestNode(fNode, root.getRightChild());
}
if(leftHeight<0 && rightHeight<0){
return -1;
}
int height=leftHeight<rightHeight?rightHeight:leftHeight;
return 1+height;
}
/* 层次遍历:判断一个节点是否在树中,如果在树中,求该节点在树中的最深的层次;如果不在返回 -1 */
public int findDeapestNode2(BiTreeNode fNode, BiTreeNode root) {
int height=1,maxHight=0,front=0,rear=0,last=0;
LinkedList<BiTreeNode> queue=new LinkedList<BiTreeNode>();
queue.add(root);
rear++;
while(front!=rear){
BiTreeNode delNode=queue.get(front++);
if(delNode.getData()==fNode.getData()){
maxHight=height;
}
if(delNode.getLeftChild()!=null){
queue.add(delNode.getLeftChild());
rear++;
}
if(delNode.getRightChild()!=null){
queue.add(delNode.getRightChild());
rear++;
}
if(front>last){
last=rear;
height++;
}
}
if(maxHight==0){
return -1;
}
return maxHight;
}
/*
* 创建一棵节点为n的二叉树
*/
public BiTreeNode createTree(int n) {
BiTreeNode[] queue = new BiTreeNode[n];
int front = 0, rear = 0;
int count = 0;
try {
char c = (char) System.in.read();
BiTreeNode node = new BiTreeNode();
node.setData(c);
node.setLeftChild(null);
node.setRightChild(null);
queue[rear++] = node;
root = node;
count++;
while ((count < n) && (front < rear)) {
BiTreeNode parNode = queue[front++];
if ((count + 1) < n) {
c = (char) System.in.read();
if (c != '#') {
count++;
BiTreeNode leftNode = new BiTreeNode();
leftNode.setData(c);
leftNode.setLeftChild(null);
leftNode.setRightChild(null);
parNode.setLeftChild(leftNode);
queue[rear++] = leftNode;
}
}
if ((count + 1) < n) {
c = (char) System.in.read();
if (c != '#') {
count++;
BiTreeNode rightNode = new BiTreeNode();
rightNode.setData(c);
rightNode.setLeftChild(null);
rightNode.setRightChild(null);
parNode.setRightChild(rightNode);
queue[rear++] = rightNode;
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
int i = 0;
while (queue[i] != null) {
System.out.print(queue[i].getData() + ",");
i++;
}
return root;
}
}
public class TreeJudge {
public static void main(String[] args) {
BiTree tree = new BiTree();
BiTreeNode root = tree.createTree();
if (tree.isFull(root)) {
System.out.println("the tree is full");
} else {
System.out.println("the tree is not full");
}
if (tree.isComplete(root)) {
System.out.println("the tree is complete");
} else {
System.out.println("the tree is not complete");
}
System.out.println("deapest="+tree.findDeapestNode2(new BiTreeNode('b',null,null), root));
}
}
// 另:参考:http://www.docin.com/p-62909092.html
发表评论
-
0928--算法题
2010-09-28 11:14 1554算法设计:n个连续自然数,乱序存放于一个数组中,缺失一个,缺失 ... -
Reverse String
2010-09-19 16:46 1000package org.jyjiao; public c ... -
0906--拼接出最小整数
2010-09-06 10:38 1180题目描述:设有n个正整数,将它们联接成一排,组成一个最小的多位 ... -
0830--算法练习题
2010-08-28 17:42 9291. 内存中有一个长数组,条目数为10万,数组单元为结构体st ... -
??0829-Joseph问题
2010-08-28 16:39 871N个人排成一圈,指定第一个人,去除他,然后跳着一人去除第3人, ... -
???0828--存储空间管理器+n选m问题
2010-08-28 16:36 1031用单链表实现一个存储空间管理器,包括分配和释放空间。要求释放 ... -
# 0827--算法练习题
2010-08-25 14:12 8181. 一个文本文件有多行 ... -
!!!0826--图
2010-08-25 14:11 649baidu1 -
###0825-1 最短路径+最小支撑树+路径压缩+等价类问题+拓扑排序
2010-08-25 13:28 832dijkska算法实现 floyed算法实现 ... -
# 0823--树进阶
2010-08-25 13:27 7311. 判断一棵二叉树是否平衡 2. 构造AVL ... -
#0822 系分
2010-08-23 14:18 670http://www.blogjava.net/ITdavid ... -
0821集合问题
2010-08-23 14:16 785{ aaa, bbb, ccc},{bbb, ddd }, { ... -
0820-mirosoft
2010-08-20 12:43 948传说中微软的几道算法题,练习一下吧: 1.设计一个 ... -
0819- 找共同url
2010-08-18 17:47 829给你a、b两个文件,各存放50亿条url,每条url各占用64 ... -
0819--找队友
2010-08-18 11:52 1044全体员工玩分组游戏,前面五分钟大家分头找队友,并将每个人找到的 ... -
0817--概率问题
2010-08-16 18:53 736输入:N(整数)输入:数据文件A.txt,不超过6条记录,字符 ... -
0816--支配数
2010-08-16 18:52 769支配数:数组中某个元素出现的次数大于数组总数的一半时就成为支配 ... -
0811-3 对webservice执行自动化测试
2010-08-11 20:05 9510811-3 对webservice执行自动化测试 i ... -
0812-字典树算法
2010-08-11 19:59 19571. 在用户输入英文单词时,经常发生错误,我们需要对其进行纠错 ... -
0811-2 求最小前缀
2010-08-11 19:34 970给以文件,文件中每一行是一个单词,求出每个单词的最小前缀,使得 ...
相关推荐
/*********************************************************** ***********************************************************/ void preorder1(bitree *root) { bitree *p,*s[100]; int top=0;...
"数据结构--二叉树--思维导图" 二叉树是一种常见的数据结构,它是一种树形结构,每个节点最多有两个孩子节点,分别是左子树和右子树。二叉树可以用来存储大量数据,并且可以快速地查找、插入和删除数据。 二叉树的...
在这个特定的实验——“数据结构实验-二叉树”中,我们将会深入探讨二叉树这一重要的数据结构,它是广东工业大学数据结构课程的一个实践部分。二叉树在很多算法和应用中都扮演着关键角色,例如搜索、排序、文件系统...
在这个问题中,"二叉树"是一种常见的数据结构用于优化解决方案。Java作为广泛使用的编程语言,提供了丰富的工具和库来实现这种算法。 首先,我们需要理解二叉树的基本概念。二叉树是每个节点最多有两个子节点的数据...
通过阅读《算法-理论基础- 二叉树- 二叉树的遍历(包含源程序).pdf》这份文档,你将能够深入理解二叉树遍历的概念,并有机会通过实际的源代码加深理解,从而更好地掌握这个重要的数据结构和算法知识。在学习过程中...
数据结构---07---二叉树---20220506107---邹世豪.c
数据结构中的二叉树是一种特殊的树形数据结构,它的每个节点最多只有两个子节点,分别称为左子节点和右子节点。二叉树的概念源于计算机科学,被广泛应用于算法设计和数据存储。以下是对二叉树相关知识的详细阐述: ...
二叉树是计算机科学中一种重要的数据结构,它在很多算法和应用中都有广泛的应用,尤其是在编译器设计、文件系统、图形处理等领域。在VC++环境下,我们可以使用C++语言来实现二叉树的各种操作,包括创建、插入、删除...
实现下面两种生成二叉树的方法:a,先根生成二叉树(注意输入的先根序列),b)给定两个序列:前序+中序的序列,生成一棵二叉链表类型的二叉树 实现对生成的二叉树进行前序、中序、后序遍历,打印出遍历序列 ...
### 数据结构与算法(C#实现)系列---二叉树 #### 概述 本文档将详细介绍二叉树这一重要的数据结构及其在C#中的实现。二叉树是一种非线性的数据结构,在计算机科学中有着广泛的应用,如在编译器、数据库系统、搜索...
在IT领域,数据结构是计算机科学的基础,而二叉树作为一种重要的数据结构,广泛应用于搜索、排序、图形处理等场景。二叉树是由n(n≥0)个有限节点组成一个具有层次关系的集合,通常表现为一个有根的层次结构。每个...
二叉树是数据结构中的重要概念,它是一种特殊的树形数据结构,每个节点最多有两个子节点,通常称为左子节点和右子节点。在计算机科学中,二叉树的应用广泛,涉及搜索、排序、编译器设计等多个领域。本主题将深入探讨...
数据结构-二叉树的建立先序中序后序层次遍历
软件技术基础大作业--二叉树遍历,包含流程图,源代码,和文字说明
根据给定的信息,我们可以分析并总结出关于二叉树这一数据结构的相关知识点: ### 一、二叉树定义 二叉树是一种常见的非线性数据结构,它具有以下特点: - 每个节点最多有两个子节点:左子节点(left child)和右...
原二叉树:反转后的二叉树:TreeNode temp = root.left;欢迎光临我的博客,发现更多技术资源~
数据结构-二叉树的建立及遍历操作