【考研-数据结构】树和二叉树
1.二叉树
#include <iostream>
#define MaxSize 20
using namespace std;
typedef struct BiTNode{
int data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef struct LinkNode {
BiTNode* data;
LinkNode* next;
}LinkNode;
typedef struct {
LinkNode* rear, * front;
}LinkQueue;
void Init_root(BiTree& root,int e){
BiTree root = NULL;
root = new BiTNode();
root->data = e;
root->lchild = nullptr;
root->rchild = nullptr;
}
void Insert_Node(BiTree& root,int e){
BiTNode* p= new BiTNode();
p->data = e;
p->lchild = nullptr;
p->rchild = p;
}
void visit(BiTree T){
if(T!=nullptr){
cout<<T->data<<endl;
}
}
void PreOrder(BiTree T){
if(T!=nullptr){
visit(T);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
void PreOrder02(BiTree T){
InitStack(S);
BiTree p = T;
while(p||IsEmpty(S)){
if(p){
visit(p);
Push(S,p);
p = p->lchild;
}else{
Pop(S,p);
p = p->rchild;
}
}
}
void InOrder(BiTree T){
if(T!=nullptr){
PreOrder(T->lchild);
visit(T);
PreOrder(T->rchild);
}
}
void InOrder02(BiTree T){
InitStack(S);
BiTree p = T;
while(p||IsEmpty(S)){
if(p){
Push(S,p);
p = p->lchild;
}else{
Pop(S,p);
visit(p);
p = p->rchild;
}
}
}
void PostOrder(BiTree T){
if(T!=nullptr){
PreOrder(T->lchild);
PreOrder(T->rchild);
visit(T);
}
}
int treeDepth(BiTree T){
if(T == nullptr){
return 0;
}else{
int l = treeDepth(T->lchild);
int r = treeDepth(T->rchild);
return l>r?l+1:r+1;
}
}
void InitQueue(LinkQueue& Q) {
Q.rear = Q.front = new LinkNode();
Q.front->next = nullptr;
}
bool isEmpty(LinkQueue Q) {
if (Q.front == Q.rear) {
return true;
}
else {
return false;
}
}
void EnQueue(LinkQueue& Q, BiTNode *e) {
LinkNode* temp = new LinkNode();
temp->data = e;
temp->next = nullptr;
Q.rear->next = temp;
Q.rear = temp;
}
bool DeQueue(LinkQueue& Q, BiTNode*& x) {
if (Q.front == Q.rear) return false;
LinkNode* temp = Q.front->next;
x = temp->data;
Q.front->next = Q.front->next->next;
if (Q.front->next == nullptr) {
Q.rear = Q.front;
}
delete temp;
return true;
}
void LevelOrder(BiTree T){
LinkQueue Q;
InitQueue(Q);
BiTree p;
EnQueue(Q,T);
while(!isEmpty(Q)){
DeQueue(Q,p);
visit(p);
if(p->lchild!=nullptr){
EnQueue(Q,p->lchild);
}
if(p->rchild!=nullptr){
EnQueue(Q,p->rchild);
}
}
}
int main() {
return 0;
}
2.线索二叉树
#include<iostream>
using namespace std;
typedef struct ThreadNode{
int data;
struct ThreadNode *lchild,*rchild;
int ltag,rtag;
}ThreadNode,*ThreadTree;
typedef struct BiTNode{
int data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void InOrder(BiTree T){
if(T!=nullptr){
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
BiTNode* p;
BiTNode* pre = nullptr;
BiTNode* final = nullptr;
void visit(BiTNode *q){
if(q == p){
final = pre;
}else{
pre = q;
}
}
ThreadNode* pre02 = nullptr;
void CreateInThread(ThreadTree T){
pre02 = nullptr;
if(T!=nullptr){
InThread(T);
if(pre02->rchild == nullptr){
pre02->rtag = 1;
}
}
}
void InThread(ThreadTree T){
if(T!=nullptr){
InThread(T->lchild);
visit02(T);
InThread(T->rchild);
}
}
void visit02(ThreadNode *q){
if(q->lchild == nullptr){
q->lchild = pre02;
q->ltag = 1;
}
if(pre!=nullptr&&pre->rchild == nullptr){
pre02->rchild = q;
pre02->rtag = 1;
}
pre02 = q;
}
ThreadNode* pre03 = nullptr;
void CreatePreThread(ThreadTree T){
pre03 = nullptr;
if(T!=nullptr){
InThread(T);
if(pre03->rchild == nullptr){
pre03->rtag = 1;
}
}
}
void PreThread(ThreadTree T){
if(T != nullptr){
visit03(T);
if(T->ltag == 0){
PreThread(T->lchild);
}
PreThread(T->rchild);
}
}
void visit03(ThreadNode* q){
if(q->lchild == nullptr){
q->lchild = pre03;
q->ltag = 1;
}
if(pre03!=nullptr&&pre03->rchild == nullptr){
pre03->rchild = q;
pre03->rtag = 1;
}
pre03 = q;
}
ThreadNode* pre04 = nullptr;
void CreatePostThread(ThreadTree T){
pre04 = nullptr;
if(T!=nullptr){
InThread(T);
if(pre04->rchild == nullptr){
pre04->rtag = 1;
}
}
}
void PostThread(ThreadTree T){
if(T != nullptr){
PostThread(T->lchild);
PostThread(T->rchild);
visit03(T);
}
}
void visit03(ThreadNode* q){
if(q->lchild == nullptr){
q->lchild = pre04;
q->ltag = 1;
}
if(pre04!=nullptr&&pre04->rchild == nullptr){
pre04->rchild = q;
pre04->rtag = 1;
}
pre04 = q;
}
ThreadNode*Firstnode(ThreadNode*p){
while(p->ltag == 0) p = p->lchild;
return p;
}
ThreadNode*Nextnode(ThreadNode*p){
if(p->rtag == 0) return Firstnode(p->rchild);
else return p->rchild;
}
void InOrder(ThreadNode* T){
for(ThreadNode* p = Firstnode(T);p!=nullptr;p = Nextnode(p)){
visit_zb(p);
}
}
void visit_zb(ThreadNode* p){
if(p!=nullptr){
cout<<p->data<<endl;
}
}
ThreadNode*Lastnode(ThreadNode*p){
while(p->rtag == 0) p = p->rchild;
return p;
}
ThreadNode *Prenode(ThreadNode*p){
if(p->ltag == 0) return Lastnode(p->lchild);
else return p->lchild;
}
void ReInorder(ThreadNode*T){
for(ThreadNode *p = Lastnode(T);p!=nullptr;p = Prenode(p)){
visit_zb(p);
}
}
int main() {
return 0;
}
3.二叉排序树和森林
#include<iostream>
using namespace std;
typedef struct BSNode{
int key;
struct BSNode *lchild,*rchild;
}BSTNode,*BSTree;
BSTNode*BST_Search(BSTree T,int key){
while(T!=nullptr&&T->key!=key){
if(key>T->key) T = T->rchild;
else T = T->lchild;
}
return T;
}
BSTNode*BST_Search02(BSTree T,int key){
if(T == nullptr) return nullptr;
if(key == T->key) return T;
else if(key<T->key) return BST_Search02(T->lchild,key);
else return BST_Search02(T->rchild,key);
}
int BST_Insert(BSTree &T,int k){
if(T == nullptr){
T = new BSTNode();
T->key = k;
T->lchild = T->rchild = nullptr;
return 1;
}
else if(k == T->key)
return 0;
else if(k < T->key){
return BST_Insert(T->lchild,k);
}
else if(k > T->key){
return BST_Insert(T->rchild,k);
}
return 0;
}
int BST_Insert02(BSTree &T,int k){
while(T!=nullptr){
if(k<T->key){
T = T->lchild;
}else if(k>T->key){
T = T->rchild;
}else if(k == T->key){
return 0;
}
}
T = new BSTNode();
T->key = k;
T->lchild = T->rchild = nullptr;
return 1;
}
void Create_BST(BSTree &T,int str[],int n){
T = nullptr;
int i = 0;
while(i<n){
BST_Insert(T,str[i]);
i++;
}
}
int main() {
BSTree T;
int str[] = {2,3};
Create_BST(T,str,2);
cout<<T->key<<endl;
return 0;
}