这是去年的内容,之前放在github的一个被遗忘的reporsity里面,今天看到了就拿出来

#include<iostream>
#include<string>
using namespace std;
/*
question#2:The creation of BinaryTree
goal:
1.建立二叉树(通过先序序列作为输入)
2.实现中序遍历和层序遍历、元素查找
3.实现、解释
*/
/*先定义队列,以实现二叉树的层序遍历
使用链队列,以达到不预先定义队列大小的效果*/
template <typename Type>class LinkQueue;
template <typename Type>
class LinkQueueNode {
friend class LinkQueue<Type>;
public:
LinkQueueNode(Type &e, LinkQueueNode<Type>*p = NULL) :elem(e), next(p) {};
private:
Type elem;
LinkQueueNode<Type>*next;
};
template<typename Type>
class LinkQueue {
public:
LinkQueue() :front(NULL), rear(NULL) {};
~LinkQueue();
int IsEmpty()const { return front == NULL; };
void LinkQueueClear();
int LinkQueueLength()const;
Type GetFront();
void InQueue(Type&e);
Type OutQueue();
private:
LinkQueueNode<Type>*front, *rear;
};
template <typename Type>
LinkQueue<Type>::~LinkQueue() {
LinkQueueNode<Type>*p;
while (front != NULL) {
p = front;
front = front->next;
delete p;
}
};
template <typename Type>
void LinkQueue<Type>::InQueue(Type &e) {
if (front == NULL)front = rear = new LinkQueueNode<Type>(e, NULL);
else
rear = rear->next = new LinkQueueNode<Type>(e, NULL);
};
template <typename Type>
Type LinkQueue<Type>::OutQueue() {
if (IsEmpty())
{
cout << "链队列为空!" << endl;
exit(0);
}
LinkQueueNode<Type>*p = front;
Type e = p->elem;
front = front->next;
if (front == NULL)rear = NULL;
delete p;
return e;
};
template <typename Type>
Type LinkQueue<Type>::GetFront() {
if (IsEmpty()) {
cout << "链队列为空!" << endl;
exit(0);
}
else
return front->elem;
};
template <typename Type>
int LinkQueue<Type>::LinkQueueLength()const {
LinkQueueNode<Type>*p = front;
int i = 0;
while (p) {
i++;
p = p->next; }
return i;
};
template<typename T>class BinaryTree;
template<typename T>
class BinaryTreeNode {//define the node of binary tree
friend class BinaryTree<T>;
//friend class BinarySearchTree<T>;
private:
T data;
BinaryTreeNode<T>* left;
BinaryTreeNode<T>* right;
public:
BinaryTreeNode();
BinaryTreeNode<T>(const T&ele) :data(ele) {};
BinaryTreeNode<T>(const T&ele, BinaryTreeNode<T>*l, BinaryTreeNode<T>*r) :data(ele), left(l), right(r) {};
~BinaryTreeNode() {};
T value()const { return data; }
BinaryTreeNode<T>& operator=(const BinaryTreeNode<T>&Node) { this = Node; };
BinaryTreeNode<T>*leftchild()const { return left; };
BinaryTreeNode<T>*rightchild()const { return right; };
void setLeftchild(BinaryTreeNode<T>*);
void setRightchild(BinaryTreeNode<T>*);
void setValue(const T&val);
bool isLeaf()const;
};
template<typename T>
class BinaryTree {//define the binary tree
protected:
BinaryTreeNode<T>*root;
public:
BinaryTree() {
root = NULL;
}
BinaryTree(BinaryTreeNode<T>*r) { root = r; }
//~BinaryTree() { DeleteBinaryTree(root); };
//bool isEmpty()const;
void visit(const T&data) { cout << data << ' '; };
BinaryTreeNode<T>*&Root() { return root; };
/*BinaryTreeNode<T>*Parent(BinaryTreeNode<T>*current);
BinaryTreeNode<T>*LeftSibling(BinaryTreeNode<T>*current);
BinaryTreeNode<T>*RightSibling(BinaryTreeNode<T>*current);*/
void CreateTree(const T&data, BinaryTree<T>&leftTree, BinaryTree<T>&rightTree);
void CreateTree(BinaryTreeNode<T>*&r);
void DeleteBinaryTree(BinaryTreeNode<T>*root);
//void PreOrder(BinaryTreeNode<T>*root);
void InOrder(BinaryTreeNode<T>*root);
/*void PostOrder(BinaryTreeNode<T>*root);
void PreOrderWithoutRecusion(BinaryTreeNode<T>*root);
void InOrderWithoutRecusion(BinaryTreeNode<T>*root);
void PostOrderWithoutRecusion(BinaryTreeNode<T>*root);*/
void LevelOrder(BinaryTreeNode<T>*root);
bool Search(BinaryTreeNode<T>*root,T&data);
};
template<typename T>
/*利用用户输入的先序遍历序列来初始化二叉树*/
void BinaryTree<T>::CreateTree(BinaryTreeNode<T>*& r)
{
char ch;
cin >> ch;
if (ch == '#')r = NULL;
else {
r = new BinaryTreeNode<T>(ch);
CreateTree(r->left);
CreateTree(r->right);
}
};
template<typename T>
bool BinaryTree<T>::Search(BinaryTreeNode<T>*root,T &data)
{
/*前序遍历,递归进行元素的搜索*/
int flag = 0;
if (root == NULL)
return 0;
if (root->data == data)
{
flag = 1;
return flag;
}
flag=flag+Search(root->left, data);
flag=flag+Search(root->right, data);
return flag; };
template<typename T>
void BinaryTree<T>::InOrder(BinaryTreeNode<T>*root)
{
/*二叉树的中序遍历*/
if (root == NULL)return;
InOrder(root->left);
visit(root->data);
InOrder(root->right);
};
template<typename T>
void BinaryTree<T>::LevelOrder(BinaryTreeNode<T>*root)
{
/*二叉树的层序遍历*/
LinkQueue<BinaryTreeNode<T>*>tQueue;//链队列,节点类型为二叉树节点指针类型
BinaryTreeNode<T>*pointer = root;
if (pointer)tQueue.InQueue(pointer);
while (!tQueue.IsEmpty()) {
pointer = tQueue.GetFront();
tQueue.OutQueue();
visit(pointer->value());
if (pointer->leftchild() != NULL)
tQueue.InQueue(pointer->left);
if (pointer->rightchild() != NULL)
tQueue.InQueue(pointer->right);
}
};
int main()
{
char te ='A';
BinaryTree<char> test;//注意,用模板来创建对象时不使用new语句!!!
//BinaryTreeNode<char>* temp;
//temp = test.Root();
test.CreateTree(test.Root());//建立二叉树(通过先序序列作为输入)
test.InOrder(test.Root());//中序遍历
test.LevelOrder(test.Root());//层序遍历
if (test.Search(test.Root(), te))
cout << "True" << endl;
else
cout << "False" << endl;
return 0;
}

二叉树的C++实现的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. 二叉树的递归实现(java)

    这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...

  3. c 二叉树的使用

    简单的通过一个寻找嫌疑人的小程序 来演示二叉树的使用 #include <stdio.h> #include <stdlib.h> #include <string.h& ...

  4. Java 二叉树遍历右视图-LeetCode199

    题目如下: 题目给出的例子不太好,容易让人误解成不断顺着右节点访问就好了,但是题目意思并不是这样. 换成通俗的意思:按层遍历二叉树,输出每层的最右端结点. 这就明白时一道二叉树层序遍历的问题,用一个队 ...

  5. 数据结构:二叉树 基于list实现(python版)

    基于python的list实现二叉树 #!/usr/bin/env python # -*- coding:utf-8 -*- class BinTreeValueError(ValueError): ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  8. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

  9. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  10. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

随机推荐

  1. 20165207 Exp8 Web基础

    目录 20165207 Exp8 Web基础 0. 环境配置 0.1. apache 0.2. MySQL 0.3. php 0.4. php-mysql编程库 1. 前台HTML编写静态网页 2. ...

  2. CI集成Smarty的实现方式

    给新伙伴的忠告:不要去想着有多复杂,看一遍绝对就会弄了! 这样集成的目的是什么? 因为我使用过CI和smarty,所以我就按自己的理解讲一下:CI框架在控制器.models方面做的很好,但在多变的视图 ...

  3. 黑马vue---10-11、Vue实现跑马灯效果

    黑马vue---10-11.Vue实现跑马灯效果 一.总结 一句话总结: 1. 给 [浪起来] 按钮,绑定一个点击事件   v-on   @ 2. 在按钮的事件处理函数中,写相关的业务逻辑代码:拿到 ...

  4. linux安装软件时/usr/lib/python2.7/site-packages/urlgrabber/grabber.py文件异常

    linux安装软件时,经常出现以下异常信息 Traceback (most recent call last): File , in <module> main() File , in m ...

  5. javascript 生成img标签的3种方式(对象、方法、html)

    <div id="d1"></div> <script> //HTML function a(){ document.getElementByI ...

  6. Breadcrumb 面包屑

    显示当前页面的路径,快速返回之前的任意页面. 基础用法 适用广泛的基础用法. 在el-breadcrumb中使用el-breadcrumb-item标签表示从首页开始的每一级.Element 提供了一 ...

  7. numpy之数组属性与方法

    # coding=utf-8import numpy as npimport random # nan是一个float类型 ,not a num不是一个数字;inf,infinite 无穷 # 轴的概 ...

  8. HashMap 的实现原理解析(转载)

    HashMap 概述 HashMap 是基于哈希表的 Map 接口的非同步实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.此类不保证映射的顺序,特别是它不保证该顺序恒久不 ...

  9. MySQL 对 IP 字段的排序问题

    MySQL 对 IP 字段的排序问题 问题描述 想对一张带有 IP 字段的表,对 IP 字段进行升序排序,方便查看每个段的 IP 信息. 表结构和表数据如下: SET NAMES utf8mb4; ; ...

  10. android4.2 webkit 中的jni

    在android 应用开发中使用WebView,当一个webveiw 被创建时, 也会去load 他所对应的动态库,这里动态库也就是传说中的webkit 内核等. C++ 层与java 层的交互也是通 ...