数据结构期末报告 实验二

1. 建立一棵采用二叉链表结构存储的二叉树。

2. 分别对该二叉树进行先序、中序和后序遍历。

3. 求二叉树的高度。

4求二叉树中叶子结点的数目。

5按中序方式输出二叉树中各结点的值及其层次数。

6复制一棵二叉树T到T1。

7交换二叉树中每个结点的左右孩子指针的值。

#include<iostream>
#include<algorithm>
#include<string>
#include <stack>
#include<cmath>
using namespace std;


typedef struct bitree {
	char  data;
	struct bitree* lchild, * rchild;
} bnode;


class BinaryTree {
public:
	BinaryTree();
	void preorder() { preorder(root); }
	void inorder() { inorder(root); }
	void postorder() { postorder(root); }
	void CreateBitree(bnode*& T);
	void preTraverse(bnode* T);
	void midTraverse(bnode* T);
	void postTraverse(bnode* T);
	int  DepthTree(bnode* root);
	int  treeheight(bnode* T);
	void in_traverse_level(bnode*& T, int n);
	void copy(bnode* T, bnode*& S);
	void Switch(bnode*& T1, bnode*& T2);
	void NodeSwap(bnode*& T);
	bnode* get_root() { return root; }

private:
	bnode* root;
	void visit(bnode* T);         //访问结点函数
	void preorder(bnode* T);      //先序遍历
	void inorder(bnode* T);
	void postorder(bnode* T);
	
};

BinaryTree::BinaryTree()               //初始化函数
{
	root = NULL;
}
 
void BinaryTree::visit(bnode* T)       //访问结点函数
{
	cout << T->data;
}

void BinaryTree::preorder(bnode* T)    //先序遍历
{
	if (T != NULL) {
		visit(T);    	
		preorder(T->lchild);       
		preorder(T->rchild);
	}
}
void BinaryTree::inorder(bnode* T)    //中序遍历
{
	if (T != NULL)
	{
		inorder(T->lchild);
		visit(T);
		inorder(T->rchild);
	}

}

void BinaryTree::postorder(bnode* T) //后序遍历
{
	if (T != NULL)
	{
		postorder(T->lchild);
		postorder(T->rchild);
		visit(T);
	}
}

void BinaryTree::CreateBitree(bnode*& T)   //按照前序顺序建立二叉树
{
	char ch;
	cin >> ch;
	if (ch == '#')
		T = NULL;
	else
	{
		T = new bnode;
		T->data = ch;
		CreateBitree(T->lchild);
		CreateBitree(T->rchild);
	}
}

void BinaryTree::preTraverse(bnode* T)  //前序遍历输出
{
	if (T)
	{
		
		cout <<T->data << " ";
		preTraverse(T->lchild);
		preTraverse(T->rchild);
	}
}
void BinaryTree::midTraverse(bnode* T) //中序遍历输出
	{
		if (T)
		{
			midTraverse(T->lchild);
			
			cout << T->data << " ";
			midTraverse(T->rchild);
		}
	}

void BinaryTree::postTraverse(bnode *T) //后续遍历输出
{
	if (T)
	{
		postTraverse(T->lchild);
		postTraverse(T->rchild);
		
		cout << T->data << " ";
	}
}

int BinaryTree::DepthTree(bnode* root) //求叶子结点
{
	if (!root)
	{
		return 0;
	}
	else
	{
		if ((!root->lchild) && (!root->rchild))
		{
			return 1;
		}
		else
		{
			return (DepthTree(root->lchild) + DepthTree(root->rchild));
		}
	}
}
int BinaryTree::treeheight(bnode* T)   //递归法求树高
{
	int height;
	if (T == NULL)
		return 0;
	else
		return max(treeheight(T->lchild), treeheight(T->rchild)) + 1 ;
	

}

void BinaryTree::in_traverse_level(bnode*& T, int n) //按中序方式输出二叉树中各结点的值及其层次数
{
	//对中序遍历算法进行改造  
	if (T != NULL)
	{
		in_traverse_level(T->lchild, n + 1);
		cout << "节点:" << T->data << "  该节点层次:" << n << endl;;
		in_traverse_level(T->rchild, n + 1);
	}
}

void BinaryTree::copy(bnode* T, bnode*& S)
{
	if (T == NULL)
		S = NULL;
	else
	{
		S = new bnode;
		S->data = T->data;
		copy(T->lchild, S->lchild);
		copy(T->rchild, S->rchild);
	}
}



void BinaryTree::Switch(bnode*& T1, bnode*& T2)//交换两个二叉树结点指针的指向
{
	bnode* t = T1;
	T1 = T2;
	T2 = t;
}
void BinaryTree::NodeSwap(bnode*& T)//交换二叉树每个结点的左孩子和右孩子
{
	//此算法根据二叉树先序遍历算法改造而来
	if (T != NULL)
	{
		if (T->lchild != NULL && T->rchild != NULL)//如果T的左孩子和右孩子都不空
		{
			//将"交换二叉树每个结点的左孩子和右孩子"转换为"交换二叉树每个结点的左孩子的数据域和右孩子的指针域".
			Switch(T->lchild, T->rchild);
		}
		else if (T->lchild != NULL && T->rchild == NULL)//如果T的左孩子不空且右孩子为空
		{
			//将T的左子树变为右子树
			T->rchild = T->lchild;
			T->lchild = NULL;
		}
		else if (T->lchild == NULL && T->rchild != NULL)//如果T的左孩子为空且右孩子不为空
		{
			//将T的右子树变为左子树
			T->lchild = T->rchild;
			T->rchild = NULL;
		}
		else//如果T的左孩子和右孩子都为空
		{
			//空操作
			;
		}
		NodeSwap(T->lchild);
		NodeSwap(T->rchild);
	}
	else
	{
		;
	}
}

int main() {
	BinaryTree T, A;
	bnode* a = A.get_root();
	bnode* b = T.get_root();
	A.CreateBitree(a);
	cout << "创建成功!" << endl;
	A.copy(a, b);
	cout << "复制成功,复制后的新树的前序遍历为:" << endl;
	T.preTraverse(b);
	return 0;
}


//测试图:1 2 3 # 4 # # 5 # 6 # # 7 8 # # 9 # #

 

  • 9
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值