⑴ 二叉樹怎麼建立
二叉樹建立方法:
⑵ 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有什麼關系? 你解釋一下?
頭文件就是寫 結構 的定義,類型定義。