`

0815-二叉树

阅读更多

第一题:

 

在一棵一般的二叉树中找到指定的元素,如果有重复出现的元素,要求元素为深度最深的任何一个。
指定元素找不到时返回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

 

 

分享到:
评论

相关推荐

    二叉树建立-二叉树建立-二叉树建立-二叉树建立-二叉树建立-二叉树建立

    /*********************************************************** ***********************************************************/ void preorder1(bitree *root) { bitree *p,*s[100]; int top=0;...

    数据结构--二叉树--思维导图.pdf

    "数据结构--二叉树--思维导图" 二叉树是一种常见的数据结构,它是一种树形结构,每个节点最多有两个孩子节点,分别是左子树和右子树。二叉树可以用来存储大量数据,并且可以快速地查找、插入和删除数据。 二叉树的...

    数据结构实验-二叉树

    在这个特定的实验——“数据结构实验-二叉树”中,我们将会深入探讨二叉树这一重要的数据结构,它是广东工业大学数据结构课程的一个实践部分。二叉树在很多算法和应用中都扮演着关键角色,例如搜索、排序、文件系统...

    二维矩形装箱算法--二叉树--java实现.rar

    在这个问题中,"二叉树"是一种常见的数据结构用于优化解决方案。Java作为广泛使用的编程语言,提供了丰富的工具和库来实现这种算法。 首先,我们需要理解二叉树的基本概念。二叉树是每个节点最多有两个子节点的数据...

    算法-理论基础- 二叉树- 二叉树的遍历(包含源程序).rar

    通过阅读《算法-理论基础- 二叉树- 二叉树的遍历(包含源程序).pdf》这份文档,你将能够深入理解二叉树遍历的概念,并有机会通过实际的源代码加深理解,从而更好地掌握这个重要的数据结构和算法知识。在学习过程中...

    数据结构---07---二叉树---20220506107---邹世豪.c

    数据结构---07---二叉树---20220506107---邹世豪.c

    数据结构课件---二叉树

    数据结构中的二叉树是一种特殊的树形数据结构,它的每个节点最多只有两个子节点,分别称为左子节点和右子节点。二叉树的概念源于计算机科学,被广泛应用于算法设计和数据存储。以下是对二叉树相关知识的详细阐述: ...

    VC++----二叉树(经典)

    二叉树是计算机科学中一种重要的数据结构,它在很多算法和应用中都有广泛的应用,尤其是在编译器设计、文件系统、图形处理等领域。在VC++环境下,我们可以使用C++语言来实现二叉树的各种操作,包括创建、插入、删除...

    数据结构实验8-二叉树

    实现下面两种生成二叉树的方法:a,先根生成二叉树(注意输入的先根序列),b)给定两个序列:前序+中序的序列,生成一棵二叉链表类型的二叉树 实现对生成的二叉树进行前序、中序、后序遍历,打印出遍历序列 ...

    数据结构与算法(C#实现)系列---二叉树.doc

    ### 数据结构与算法(C#实现)系列---二叉树 #### 概述 本文档将详细介绍二叉树这一重要的数据结构及其在C#中的实现。二叉树是一种非线性的数据结构,在计算机科学中有着广泛的应用,如在编译器、数据库系统、搜索...

    数据结构--二叉树--线索化二叉树

    在IT领域,数据结构是计算机科学的基础,而二叉树作为一种重要的数据结构,广泛应用于搜索、排序、图形处理等场景。二叉树是由n(n≥0)个有限节点组成一个具有层次关系的集合,通常表现为一个有根的层次结构。每个...

    数据结构-二叉树相关功能及算法

    二叉树是数据结构中的重要概念,它是一种特殊的树形数据结构,每个节点最多有两个子节点,通常称为左子节点和右子节点。在计算机科学中,二叉树的应用广泛,涉及搜索、排序、编译器设计等多个领域。本主题将深入探讨...

    数据结构-二叉树建立

    数据结构-二叉树的建立先序中序后序层次遍历

    软件技术基础大作业--二叉树遍历

    软件技术基础大作业--二叉树遍历,包含流程图,源代码,和文字说明

    数据结构-------二叉树

    根据给定的信息,我们可以分析并总结出关于二叉树这一数据结构的相关知识点: ### 一、二叉树定义 二叉树是一种常见的非线性数据结构,它具有以下特点: - 每个节点最多有两个子节点:左子节点(left child)和右...

    qingq0825#Javabook-Interview#算法-二叉树-递归-二叉树反转1

    原二叉树:反转后的二叉树:TreeNode temp = root.left;欢迎光临我的博客,发现更多技术资源~

    数据结构-二叉树的建立及遍历操作

    数据结构-二叉树的建立及遍历操作

Global site tag (gtag.js) - Google Analytics