- //--- 二叉树的链式存储结构----
- /* 实现二叉树的先序,中序和后序遍历,
- 并按凹入法打印。打印层次遍历序列。
- 交换树的左右子树*/
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- typedef char TElemType;// 树的元素类型
- class BiTree{
- public:
- BiTree()
- {
- data = '/0';
- parent = NULL;
- lchild = NULL;
- rchild = NULL;
- }
- void creatBiTree();// 构造链式的二叉树 T
- void preOrderTraverse(int);
- // 先序遍历二叉树T.凹入法打印
- void inOrderTraverse();
- // 中序遍历二叉树T.
- void postOrderTraverse();
- // 后序遍历二叉树T.
- void leveOrderTraverse();
- // 层次遍历二叉树T.
- void swapChild();
- // 交换左右子树
- private:
- TElemType data; // 结点(字符串型)
- BiTree *parent, *lchild, *rchild; // 左孩子和又孩子
- };
- typedef BiTree * QElemType;// 队列的元素类型
- class QueueNode
- {//队列结点
- private:
- friend class LinkQueue;
- QElemType data;
- QueueNode *next;
- public:
- QueueNode(QElemType e)
- {
- data = e;
- next = NULL;
- }
- QueueNode()
- {
- data = NULL;
- next = NULL;
- }
- };
- class LinkQueue
- {// 对列指针
- public:
- LinkQueue();//初始化队列
- void enQueue(QElemType);//入队
- void deQueue(QElemType &);//出队
- bool emputyQueue();//判断队是否空,队空TURE,非空FALSE
- private:
- QueueNode *front; // 队头指针
- QueueNode *rear; // 队尾指针
- };
- void BiTree:: creatBiTree()
- {
- char str[5];
- scanf("%s",str);
- BiTree *currentT = this;
- if (str[0] == '*')// 左孩子为空
- {
- currentT->parent->lchild = NULL;
- delete currentT;
- }
- else if(str[0] == '#')// 右孩子为空
- {
- currentT->parent->rchild = NULL;
- delete currentT;
- }
- else
- {
- currentT->data = str[0]; // 构造根节点
- currentT->lchild = new BiTree();
- currentT->lchild->parent = currentT;
- currentT->lchild->creatBiTree(); // 构造左子树
- currentT->rchild = new BiTree();
- currentT->rchild->parent = currentT;
- currentT->rchild->creatBiTree(); // 够造右子树
- }
- }
- void BiTree:: preOrderTraverse(int i)
- {
- if (this)
- { int j;
- for(j = i; j > 0; j--)
- cout << " ";
- cout << data << endl; // 遍历根节点
- lchild->preOrderTraverse(i+1); //先序遍历左子树
- rchild->preOrderTraverse(i+1); //先序遍历右子树
- }
- }
- void BiTree:: inOrderTraverse()
- {
- if (this)
- {
- lchild->inOrderTraverse(); //先序遍历左子树
- cout << data; // 遍历根节点
- rchild->inOrderTraverse(); //先序遍历右子树
- }
- }
- void BiTree:: postOrderTraverse()
- {
- if (this)
- {
- lchild->postOrderTraverse(); //先序遍历左子树
- rchild->postOrderTraverse(); //先序遍历右子树
- cout << data; // 遍历根节点
- }
- }
- void BiTree:: leveOrderTraverse()
- {
- LinkQueue *p = new LinkQueue();
- QElemType a = new BiTree();
- if (this)
- {
- p->enQueue(this);
- while(!p->emputyQueue())
- {
- p->deQueue(a);
- cout << a->data;
- if(a->lchild!=NULL)
- p->enQueue(a->lchild);
- if(a->rchild!=NULL)
- p->enQueue(a->rchild);
- }
- cout << "/n";
- }
- }
- LinkQueue:: LinkQueue()
- {
- QueueNode *p = new QueueNode();//创建一个内容为空的结点
- front = rear = p; //空队列
- }
- void LinkQueue:: enQueue(QElemType e)
- {
- QueueNode *p = new QueueNode(e); //申请结点
- rear->next = p; //加入新结点
- rear = p; //队尾指针指向最后结点
- }
- void LinkQueue:: deQueue(QElemType &e)
- {
- QueueNode *p = new QueueNode();
- if(!this->emputyQueue())//队非空才允许出队
- p = front->next;
- e = p->data;
- front->next = p->next;
- if(rear == p)rear = front;
- delete [] p;
- }
- bool LinkQueue:: emputyQueue()
- {
- if(rear == front)//队空
- return true;
- else return false;
- }
- void main()
- {
- BiTree T = BiTree();
- T.creatBiTree();
- cout << "先序遍历(凹入法表示)/n";
- T.preOrderTraverse(0);
- cout << "/n中序遍历:/n";
- T.inOrderTraverse();
- cout << "/n后序遍历:/n";
- T.postOrderTraverse();
- cout << "/n层次遍历:/n";
- T.leveOrderTraverse();
- }
二叉树的练习
最新推荐文章于 2025-08-07 21:27:16 发布
1230

被折叠的 条评论
为什么被折叠?



