- #include <iostream>
- using namespace std;
- struct BinaryTree
- {
- string data;
- BinaryTree *Lchild;
- BinaryTree *Rchild;
- };
- void creat(BinaryTree *&T, string k) //先序遍历顺序建立二叉链表
- {
- string ch;
- cin >> ch;
- if (ch == k)
- T = NULL;
- else
- {
- T = new BinaryTree;
- T->data = ch;
- creat(T->Lchild, k);
- creat(T->Rchild, k);
- }
- }
- int m1 = 0,m2=0,m3=0;
- void PerOrder(BinaryTree *T) //先序遍历(递归)
- {
- if (T)
- {
- if (m1 == 1)
- cout << ',';
- cout << T->data;
- m1 = 1;
- PerOrder(T->Lchild);
- PerOrder(T->Rchild);
- }
- else return;
- }
- void InOrder(BinaryTree *T) //中序遍历(递归)
- {
- if (T)
- {
- InOrder(T->Lchild);
- if (m2 == 1)
- cout << ',';
- cout << T->data;
- m2 = 1;
- InOrder(T->Rchild);
- }
- else return;
- }
- void PostOrder(BinaryTree *T) //后序遍历(递归)
- {
- if (T)
- {
- PostOrder(T->Lchild);
- PostOrder(T->Rchild);
- if (m3 == 1)
- cout << ',';
- cout << T->data;
- m3 = 1;
- }
- }
- int Depth(BinaryTree *T)//求二叉树深度
- {
- int m,n;
- if(T)
- {
- m=Depth(T->Lchild); //递归计算左子树深度为m
- n=Depth(T->Rchild); //递归计算右子树深度为n
- return m>n? m+1 : n+1;
- }
- else return 0;
- }
- int Depthx(BinaryTree *T,string x)//求二叉树中以值为x的结点为根的子树深度
- {
- int m,n;
- if(T)
- {
- if(T->data==x)
- return Depth(T);
- else
- {
- m=Depthx(T->Lchild,x);
- n=Depthx(T->Rchild,x);
- return m>n ? m:n;
- }
- }
- else return 0;
- }
- int CountLeaf(BinaryTree *T, int &count) //二叉树叶子结点个数
- {
- if (T != NULL && T->Lchild == NULL && T->Rchild == NULL) {
- count++;
- }
- if (T != NULL) {
- CountLeaf(T->Lchild, count);
- CountLeaf(T->Rchild, count);
- }
- return count;
- }
- void change(BinaryTree *T)//交换左右子树(所有)
- {
- if(T==NULL) return ;
- else {
- BinaryTree *p=T->Lchild;
- T->Lchild=T->Rchild;
- T->Rchild=p;
- change(T->Lchild);
- change(T->Rchild);
- }
- }
- void DestroyTree(BinaryTree *T)
- {
- if(T==NULL) return;
- DestroyTree(T->Lchild);
- DestroyTree(T->Rchild);
- free(T);
- }
- int main()
- {
- BinaryTree *Tree;
- string k;
- cin >> k;
- creat(Tree, k);//创建
- //需要什么加什么
-
- return 0;
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |