排序二叉树 生成双向链表
来源:互联网 发布:java程序员的发展前景 编辑:程序博客网 时间:2024/05/17 22:01
BinarySortTree.h:
//树中的节点元素typedef struct node {
int data;
node *left;
node *right;
}Node;
class BinarySortTree
{
public:
//测试方法
static void Test();
private:
//向树中添加结点
static void AddDataToTree(Node *&node1,int data);
//根节点
static Node *root;
//树生成的双向链表的头结点
static Node *treeQueueHead;
//树生成的双向链表的尾结点
static Node *treeQueueTail;
//创建结点
static Node * CreateNewNode(int data);
//中序遍历
static void LDRTra(Node *node1);
//树成为双向列表的入口函数
static Node *TreeToQueue(Node *node1);
//将树中的结点加入链表
static void ConvertToQueue(Node *pCurrent);
//打印链表
static void PrintTreeQueue(Node *head);
};
BinarySortTree.cpp
#include "BinarySortTree.h"
#include "windows.h"
#include "stdio.h"
#include <iostream>
Node *BinarySortTree::root = NULL;
Node *BinarySortTree::treeQueueHead=NULL;
Node *BinarySortTree::treeQueueTail=NULL;
void BinarySortTree::Test()
{
const int len =4;
int a[len] = {4,3,1,-1};
for(int i=0;i<len;i++)
{
AddDataToTree(root,a[i]);
}
LDRTra(root);
TreeToQueue(root);
PrintTreeQueue(treeQueueHead);
}
void BinarySortTree::AddDataToTree(Node *&pCurrent,int data)
{
if(pCurrent==NULL)
{
Node *pNode =(Node *)malloc(sizeof(Node));
pNode->data = data;
pNode->left = NULL;
pNode->right=NULL;
pCurrent = pNode;
}else if(pCurrent->data>data)
{
AddDataToTree(pCurrent->right,data);
}else if(pCurrent->data<data)
{
AddDataToTree(pCurrent->left,data);
}else
{
std::cout <<"node repeated" << std::endl;
}
}
Node *BinarySortTree::CreateNewNode(int data)
{
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data=data;
newNode->left = NULL;
newNode->right=NULL;
return newNode;
}
void BinarySortTree::LDRTra(Node *node1)
{
if(node1)
{
LDRTra(node1->left);
printf("%d\n",node1->data);
LDRTra(node1->right);
}
}
Node *BinarySortTree::TreeToQueue(Node *node1)
{
if(node1)
{
TreeToQueue(node1->left);
ConvertToQueue(node1);
TreeToQueue(node1->right);
}
return NULL;
}
void BinarySortTree::ConvertToQueue(Node *pCurrent)
{
if(treeQueueTail==NULL)
{
treeQueueHead = pCurrent;
treeQueueTail = pCurrent;
return;
}else
{
pCurrent->left = treeQueueTail;
treeQueueTail->right = pCurrent;
treeQueueTail = pCurrent;
}
}
void BinarySortTree::PrintTreeQueue(Node *node)
{
while(node!=NULL)
{
printf("\t%d",node->data);
node = node->right;
}
}
- 排序二叉树 生成双向链表
- 排序二叉树变为双向链表
- 二叉搜索树转排序双向链表
- 排序二叉树转换为双向链表
- 二叉查找树 转换成 排序的双向链表
- 把二叉查找树转变成排序的双向链表
- 二叉查找树转变为排序的双向链表
- 查找二叉树转换为排序的双向链表
- 二叉查找树转变成排序的双向链表
- 二叉查找树转换成排序的双向链表
- 二叉树转化为排序的双向链表
- 二叉搜索树转排序双向链表
- 二叉搜索树转换为排序双向链表
- 将二叉树转换为排序双向链表
- 双向链表(4) - 排序二叉树转换为循环双向链表
- 【二叉树】将二叉搜索树转换成一个排序的双向链表
- 二叉树--将二叉搜索树转换成一个排序的双向链表
- 树:将二叉搜索树转换为排序的双向链表
- 网络KTV中的技术模式的相关介绍
- 对作业中进程一些操作
- HDU_2021 发工资咯:)
- HDU1003--Max Sum--最大连续和
- 如何在VC6.0下用pthread.h这个头文件
- 排序二叉树 生成双向链表
- select 函数----续 3
- 数据项目面临的六大挑战
- 使用复选框控制表格中的文本框
- 前台用的是ajax请求,后台json字符串拼接方式
- HDU2084--动态规划数塔
- (转)iOS编程——Objective-C KVO/KVC机制
- Android Camera 使用小结
- CEdit用法小记