⑴ 二叉树怎么建立
二叉树建立方法:
⑵ C++中结构体创建二叉树出现使用未定义的类型怎么回事啊 我包含这个头文件了啊
定义TREENODE类型的指针,例如:
TREENODE *p=new TREENODE;
p->ch='A';
⑶ 二叉树c语言模块化实现要写头文件吗
写文件分离,即和主函数不在同一个源文件内,就写头文件
~~~~~~~~~~~~~~
⑷ 数据结构 C++ 二叉树
线索二叉的应用。要求:线索二叉树的建立、插入、删除、恢复线索的实现。 求呵呵,我刚好学完数据结构,试验的时候自己写了线索二叉树的头文件; ----
⑸ 关于C语言二叉树
首先二叉树的结点是由做孩子指针*lchild 右孩子指针*rchild 以及数据成员data
L表示左孩子R表示右孩子T表示他们的父结点
后序遍历的访问顺序是LRT
中序遍历的访问顺序是LTR
前序遍历的访问顺序是TLR
其中说的前中后就是指访问父结点的次序;
拓扑图在这里没法给出啊。。。
--------------------------------------------
这是我用C++类写的二叉树的头文件,里面有几个函数你可能用不到,你主要看看那几个遍历函数
#include<iostream>
using namespace std;
typedef char elemType;
struct bnode
{
bnode *lchild,*rchild;
elemType data;
};
class BinaryTree
{
public:
BinaryTree();
void create(bnode* &tempR);
void visite(bnode *T);
void preorder(bnode *T);
void inorder(bnode *T);
void postorder(bnode *T);
int high(bnode *T);
void convert(bnode* &tempR,string &a,int i);
void (bnode *T,bnode *&T1);
void level(bnode *T,int i);
void swap(bnode *T);
bnode *root;
private:
int count;
};
BinaryTree::BinaryTree()
{
root = NULL;
count = 0;
}
void BinaryTree::create(bnode* &tempR)
{
elemType x;
cin>>x;
if(x == '.')
{
tempR = NULL;
}
else
{
tempR = new bnode;
count++;
tempR->data = x;
create(tempR->lchild);
create(tempR->rchild);
}
}
void BinaryTree::visite(bnode *T)
{
if(T!=NULL)
cout<<T->data<<' ';
}
void BinaryTree::preorder(bnode *T)
{
if(T!=NULL)
{
visite(T);
preorder(T->lchild);
preorder(T->rchild);
}
}
void BinaryTree::inorder(bnode *T)
{
if(T!=NULL)
{
inorder(T->lchild);
visite(T);
inorder(T->rchild);
}
}
void BinaryTree::postorder(bnode *T)
{
if(T!=NULL)
{
postorder(T->lchild);
postorder(T->rchild);
visite(T);
}
}
int BinaryTree::high(bnode *T)
{
if(T==NULL)
return 0;
else if(high(T->lchild)>high(T->rchild))
return high(T->lchild)+1;
else
return high(T->rchild)+1;
}
void BinaryTree::level(bnode *T,int i)
{
if(T!=NULL)
{
level(T->lchild,i+1);
visite(T);
cout<<i<<' ';
level(T->rchild,i+1);
}
}
void BinaryTree::convert(bnode *&T,string &a,int i)
{
elemType x;
if(i<=a.length())
{
x = a[i-1];
T = new bnode;
count++;
T->data = x;
convert(T->lchild,a,2*i);
convert(T->rchild,a,2*i+1);
}
else
{
T=NULL;
}
}
void BinaryTree::(bnode *T,bnode *&T1)
{
elemType x;
if(T!=NULL)
{
x=T->data;
if(x == '.')
{
T1 = NULL;
}
else
{
T1 = new bnode;
T1->data = x;
T1->lchild = NULL;
T1->rchild = NULL;
(T->lchild,T1->lchild);
(T->rchild,T1->rchild);
}
}
}
void BinaryTree::swap(bnode *T)
{
if(T!=NULL)
{
bnode *temp;
temp=T->lchild;
T->lchild=T->rchild;
T->rchild=temp;
swap(T->lchild);
swap(T->rchild);
}
}
⑹ 用递归交换二叉树的左右子树,用c++实现。请高手写出完整的程序,包括头文件和主函数
#include "stdio.h"
#include "conio.h"
typedef struct node
{int data;
struct node *lchild,*rchild;
}bitree;
typedef int datatype;
typedef struct
{datatype data[64];
int top;
}seqstack;
seqstack *s;
bitree *creat()
{bitree *t;
int x;
printf("0 for NULL");
scanf("%d",&x);
if (x==0)
t=NULL;
else
{t=(bitree *)malloc(sizeof(bitree));
t->data=x;
t->lchild=creat();
t->rchild=creat();
}
return t;
}
void inorder(bitree *t)
{if (t!=NULL)
{inorder(t->lchild);
printf("%4d",t->data);
inorder(t->rchild);
}
}
void exchange(bitree *t)
{bitree *p;
if (t!=NULL)
{p=t->lchild;
t->lchild=t->rchild;
t->rchild=p;
exchange(t->lchild);
exchange(t->rchild);
}
}
main()
{bitree *root;
printf("INPUT THE TREE\n");
root=creat();
inorder(root);
exchange(root);
printf("\n");
inorder(root);
getch();
}
⑺ /*完全二叉树顺序存储的头文件*/ #define MAXSIZE 20 /*完全二叉树顺序存储的头文件*/
// #define MAXSIZE 20 /*别名*/
// typedef char datatype; /*别名*/
char tree[20];
int n;
⑻ c++中有没有对各种基础数据结构如二叉树,无向图,队列,栈定义的库或头文件呢
STL库中有各种来常用容器,这源些容器的底层实现就是这些数据结构。如vector是顺序表,queue是队列,stack是栈,set和map是红黑树,hash_map是哈希表等。其他数据结构可通过这些容器组合定义。
⑼ C++ 数据结构 二叉树头文件
// BinaryTreeNode.h: interface for the BinaryTreeNode class.
//
//////////////////////////////////////////////////////////////////////
#if !(AFX_BINARYTREENODE_H__65C73C3B_E763_40D9_8460_F5703119C756__INCLUDED_)
#define AFX_BINARYTREENODE_H__65C73C3B_E763_40D9_8460_F5703119C756__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
template <class T> class BinaryTree;
template <class T> class BinarySearchTree;
template <class T>
class BinaryTreeNode
{
friend class BinaryTree <T> ;
friend class BinarySearchTree <T> ;
private:
T element; //二叉树结点数据域
BinaryTreeNode <T> * left; //二叉树结点指向左子树的指针
BinaryTreeNode <T> * right; //二叉树结点指向左子树的指针
public:
BinaryTreeNode();
BinaryTreeNode(const T& ele); //给定数据的构造函数
BinaryTreeNode(const T& ele,BinaryTreeNode* l, BinaryTreeNode* r);//给定数据的左右指针的构造函数
T value() const; //返回当前结点的数据
BinaryTreeNode <T> & operator= (const BinaryTreeNode <T> & Node)
{this=Node;}; //重载赋值操作符
BinaryTreeNode <T> * leftchild() const; //返回当前结点指向左子树的指针
BinaryTreeNode <T> * rightchild() const; //返回当前结点指向右子树的指针
void setLeftchild(BinaryTreeNode <T> *); //设置当前结点的左子树
void setRightchild(BinaryTreeNode <T> *); //设置当前结点的右子树
void setValue(const T& val); //设置当前结点的数据域
bool isLeaf() const; //判定当前结点是否为叶结点,若是返回true
};
//***************************************************************************//
//**********************Class BinaryTreeNode Implementation******************//
//***************************************************************************//
template <class T>
BinaryTreeNode <T> ::BinaryTreeNode()
{
left=right=NULL;
}
template <class T>
BinaryTreeNode <T> ::BinaryTreeNode(const T& ele) //给定数据的构造函数
{
element=ele;
left=right=NULL;
}
template <class T>
BinaryTreeNode <T> ::BinaryTreeNode(const T& ele,BinaryTreeNode* l, BinaryTreeNode* r)
//给定数据的左右指针的构造函数
{
element=ele;
left=l;
right=r;
}
template <class T>
T BinaryTreeNode <T> ::value() const
{
return element;
}
template <class T>
BinaryTreeNode <T> * BinaryTreeNode <T> ::leftchild() const
{
return left;
} //返回当前结点指向左子树的指针
template <class T>
BinaryTreeNode <T> * BinaryTreeNode <T> ::rightchild() const
{
return right; //返回当前结点指向右子树的指针
}
template <class T>
void BinaryTreeNode <T> ::setLeftchild(BinaryTreeNode <T> * subroot)//设置当前结点的左子树
{
left=subroot;
}
template <class T>
void BinaryTreeNode <T> ::setRightchild(BinaryTreeNode <T> * subroot)//设置当前结点的右子树
{
right=subroot;
}
template <class T>
void BinaryTreeNode <T> ::setValue(const T& val) //设置当前结点的数据域
{
element = val;
}
template <class T>
bool BinaryTreeNode <T> ::isLeaf() const //判定当前结点是否为叶结点,若是返回true
{
return (left == NULL) && (right == NULL);
}
#endif // !defined(AFX_BINARYTREENODE_H__65C73C3B_E763_40D9_8460_F5703119C756__INCLUDED_)
⑽ 二叉树c语言模块化实现要写头文件吗
其实你完全可以用模版来实现啊 ,如果不用模版的话,如果你要的类型名不一版样的话,那你当然不能定义权相同的名字,这是显而易见的。
当然要有源文件了, 源文件跟extern有什么关系? 你解释一下?
头文件就是写 结构 的定义,类型定义。