此算法中的树结构为“左儿子有兄弟链接结构”

在这样的一个二叉树中,一个节点的左分支是他的大儿子节点,右分支为他的大兄弟节点。

这里讲的树有递归前根,中根,后根遍历,插入节点,插入兄弟节点,查找结点,释放内存这些功能。

重点说一下查找节点这一算法:

pSTreeNode CTree::Search( pSTreeNode pNode, TreeDataType Value )

{

if ( pNode == NULL )

return NULL;

  if ( pNode->data == Value )

return pNode;

  if ( pNode->pFirstChild == NULL && pNode->pNextBrother == NULL )

return NULL;

else

{

if ( pNode->pFirstChild != NULL )                                    //首先判断根节点的左儿子节点是否为空,若不为空,通过引入指针来寻找节点

{

pSTreeNode pNodeTemp = Search( pNode->pFirstChild, Value );

if ( pNodeTemp != NULL )

return pNodeTemp;

else                                                                               

{

return Search( pNode->pNextBrother, Value );

}

}

else                                                                                 //否则在根节点的右兄弟节点中寻找

return Search( pNode->pNextBrother, Value );

}

}

完整代码如下:

#include <iostream>

using namespace std;

typedef struct STreeNode* pSTreeNode;

typedef int TreeDataType;

//定义结构体

struct STreeNode

{

TreeDataType data;

pSTreeNode pFirstChild;

pSTreeNode pNextBrother;

//结构体构造函数

STreeNode( TreeDataType Value )

{

data = Value;

pFirstChild = NULL;

pNextBrother = NULL;

}

};

//定义类

class CTree

{

public:

CTree();

CTree( TreeDataType Value );

~CTree();

public:

void Insert( TreeDataType parentValue, TreeDataType Value ); // parentValue:该点的父亲节点;Value:该点的值

void InsertBrother( pSTreeNode pParentNode, TreeDataType Value );

pSTreeNode Search( pSTreeNode pNode, TreeDataType Value );

void Preorder( pSTreeNode pNode ); // 前序遍历

void Inorder( pSTreeNode pNode ); // 中序遍历

void postorder( pSTreeNode pNode ); // 后序遍历

void FreeMemory( pSTreeNode pNode ); // 释放内存

public:

pSTreeNode pRoot;

};

//构造函数

CTree::CTree()

{

pRoot = NULL;

}

//构造函数

CTree::CTree( TreeDataType Value )

{

pRoot = new STreeNode( Value );

}

//析构函数

CTree::~CTree()

{

if (pRoot == NULL )

return;

FreeMemory( pRoot );

}

//释放内存

void CTree::FreeMemory( pSTreeNode pNode )

{

if ( pNode == NULL )

return;

if ( pNode->pFirstChild != NULL )

FreeMemory( pNode->pFirstChild );

if ( pNode->pNextBrother != NULL )

FreeMemory( pNode->pNextBrother );

delete pNode;

pNode = NULL;

}

//插入节点

void CTree::Insert( TreeDataType parentValue, TreeDataType Value )

{

if ( pRoot == NULL )

return;

pSTreeNode pFindNode = Search( pRoot, parentValue );

if ( pFindNode == NULL )

return;

if ( pFindNode->pFirstChild == NULL )

{

pFindNode->pFirstChild = new STreeNode( Value );

return;

}

else

{

InsertBrother( pFindNode->pFirstChild, Value );

return;

}

}

//插入右兄弟节点

void CTree::InsertBrother( pSTreeNode pBrotherNode, TreeDataType Value )

{

if ( pBrotherNode->pNextBrother != NULL )

InsertBrother( pBrotherNode->pNextBrother, Value );

else

{

pBrotherNode->pNextBrother = new STreeNode( Value );

return;

}

}

//查找函数

pSTreeNode CTree::Search( pSTreeNode pNode, TreeDataType Value )

{

if ( pNode == NULL )

return NULL;

if ( pNode->data == Value )

return pNode;

if ( pNode->pFirstChild == NULL && pNode->pNextBrother == NULL )

return NULL;

else

{

if ( pNode->pFirstChild != NULL )

{

pSTreeNode pNodeTemp = Search( pNode->pFirstChild, Value );

if ( pNodeTemp != NULL )

return pNodeTemp;

else

{

return Search( pNode->pNextBrother, Value );

}

}

else

return Search( pNode->pNextBrother, Value );

}

}

//前序遍历

void CTree::Preorder( pSTreeNode pNode )

{

if (pNode == NULL)

return;

cout << " " << pNode->data << " ";

Preorder( pNode->pFirstChild );

Preorder( pNode->pNextBrother );

}

//中序遍历

void CTree::Inorder( pSTreeNode pNode )

{

if ( pNode == NULL )

return;

Inorder( pNode->pFirstChild );

cout << " " << pNode->data << " ";

Inorder( pNode->pNextBrother );

}

//后序遍历

void CTree::postorder( pSTreeNode pNode )

{

if ( pNode == NULL )

return;

postorder( pNode->pFirstChild );

postorder( pNode->pNextBrother );

cout << " " << pNode->data << " ";

}

//主函数

int main()

{

CTree* pTree = new CTree( 1 );

pTree->Insert( 1, 2 );

pTree->Insert( 1, 3 );

pTree->Insert( 1, 4 );

pTree->Insert( 1, 5 );

pTree->Insert( 1, 6 );

pTree->Insert( 1, 7 );

pTree->Insert( 4, 8 );

pTree->Insert( 5, 9 );

pTree->Insert( 5, 10 );

pTree->Insert( 6, 11 );

pTree->Insert( 6, 12 );

pTree->Insert( 6, 13 );

pTree->Insert( 10, 14 );

pTree->Insert( 10, 15 );

cout << "前序遍历:" << endl;

pTree->Preorder( pTree->pRoot );

cout << endl;

cout << "中序遍历:" << endl;

pTree->Inorder( pTree->pRoot );

cout << endl;

cout << "后序遍历:" << endl;

pTree->postorder( pTree->pRoot );

cout << endl;

delete pTree;

pTree = NULL;

return 0;

}

c++树及树与二叉树的转换的更多相关文章

  1. 哈夫曼树【最优二叉树】【Huffman】

    [转载]只为让价值共享,如有侵权敬请见谅! 一.哈夫曼树的概念和定义 什么是哈夫曼树? 让我们先举一个例子. 判定树:         在很多问题的处理过程中,需要进行大量的条件判断,这些判断结构的设 ...

  2. 树(二叉树 & 二叉搜索树 & 哈夫曼树 & 字典树)

    树:n(n>=0)个节点的有限集.有且只有一个root,子树的个数没有限制但互不相交.结点拥有的子树个数就是该结点的度(Degree).度为0的是叶结点,除根结点和叶结点,其他的是内部结点.结点 ...

  3. Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结

    Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...

  4. 以AVL树为例理解二叉树的旋转(Rotate)操作

    树旋转是在二叉树中的一种子树调整操作, 每一次旋转并不影响对该二叉树进行中序遍历的结果. 树旋转通常应用于需要调整树的局部平衡性的场合. 树旋转包括两个不同的方式, 分别是左旋转和右旋转. 两种旋转呈 ...

  5. 数据结构(一)二叉树 & avl树 & 红黑树 & B-树 & B+树 & B*树 & R树

    参考文档: avl树:http://lib.csdn.net/article/datastructure/9204 avl树:http://blog.csdn.net/javazejian/artic ...

  6. C# 表达式树 创建、生成、使用、lambda转成表达式树~表达式树的知识详解

    笔者最近学了表达式树这一部分内容,为了加深理解,写文章巩固知识,如有错误,请评论指出~ 表达式树的概念 表达式树的创建有 Lambda法 和 组装法. 学习表达式树需要 委托.Lambda.Func& ...

  7. php数据结构课程---5、树(树的 存储方式 有哪些)

    php数据结构课程---5.树(树的 存储方式 有哪些) 一.总结 一句话总结: 双亲表示法:data parent:$tree[1] = ["B",0]; 孩子表示法:data ...

  8. 字符串 --- KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组

    涉及到字符串的问题,无外乎这样一些算法和数据结构:自动机 KMP算法 Extend-KMP 后缀树 后缀数组 trie树 trie图及其应用.当然这些都是比较高级的数据结构和算法,而这里面最常用和最熟 ...

  9. 【查找结构5】多路查找树/B~树/B+树

    在前面专题中讲的BST.AVL.RBT都是典型的二叉查找树结构,其查找的时间复杂度与树高相关.那么降低树高自然对查找效率是有所帮助的.另外还有一个比较实际的问题:就是大量数据存储中,实现查询这样一个实 ...

  10. 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树

    另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...

随机推荐

  1. 利用jsplumb和碰撞检测自动生成流程图

    使用jsplumb构建流程图模型时,有一个需求要求,选项可以从选项表中拖拽到指定容器,并且两个选项要接触到的时候才能连接起来,不接触不能连接.效果图如下 略丑- 因为这里用到了拖拽,拖放功能,所以用到 ...

  2. CSS样式表优化

    前几天公司要模仿一家客户的网站模板来为另一客户新建一个模板,说白了就是换个数据源,然后样式表再小修小改一下就行了.但通过浏览器控制台下载素材时,发现这个网站开发的挺专业的,单就样式表而言,代码工整,注 ...

  3. 【BZOJ1040】[ZJOI2008] 骑士(基环外向树DP)

    点此看题面 大致题意: 给你一片基环外向树森林,如果选定了一个点,就不能选择与其相邻的节点.求选中点的最大权值和. 树形\(DP\) 此题应该是 树形\(DP\) 的一个升级版:基环外向树\(DP\) ...

  4. 安装git 配置邮箱和用户名

    git 查看用户名和邮箱地址 $ git config user.email $ git config user.name 运行命令来配置你的用户名和邮箱 $ git config --global ...

  5. 使ListView控件中的选择项高亮显示

    实现效果: 知识运用: ListView控件的SelectedItems属性 //获取在ListView控件中被选中数据项的集合 public ListView.SelectedListViewIte ...

  6. numpy各函数简介之生成数组函数

    1.empty(shape[, dtype, order]) 依据给定形状和类型(shape[, dtype, order])返回一个新的空数组. 参数: shape : 整数或者整型元组 定义返回数 ...

  7. 基础I/O

    基础IO: c库文件IO操作接口:(详细查看c语言中的文件操作函数总结:https://www.cnblogs.com/cuckoo-/p/10560640.html) fopen 打开文件 fclo ...

  8. CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第四节

    了解和使用共享内存(1) Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Laboratory)的高级科研人员.他在多个国家级的实验室进行大型并行运 ...

  9. 操作系统(3)_CPU调度_李善平ppt

    不只上面的四种,比如时间片到了也会引起调度. 具体的调度算法: fcfs简单,但是波动很大. 最高相应比算法,执行时间最长就应该等待的长点,比sjf多了一个等待时间的考虑. 硬件定时器和软件计数器共同 ...

  10. read指令使用方法

    read命令用于从标准输入中读取输入单行,并将读取的单行根据IFS变量分裂成多个字段,并将分割后的字段分别赋值给指定的变量列表var_name.第一个字段分配给第一个变量var_name1,第二个字段 ...