数据结构实训(三)--- 求二叉树中值为x的结点的层号


【问题描述】
以二叉链表为存储结构,编写算法求二叉树中值为x的结点的层号。

【输入形式】两行,第一行是扩展二叉树的前序遍历序列,第二行是待查询结点x
【输出形式】值为x的结点所在层号。根结点所在层记为第1层。
【样例输入】AB#D##C##
D
【样例输出】
3

main.cpp文件

#include <iostream>
#include "BiTree.h"
using namespace std; // 可以不写

int main()
{
    BiTree<char> t;
    char ch;
    cin >> ch;
    cout << t.XLay(ch);
}

 

BiTree.h文件(综合上几题的二叉树方法)

#ifndef BITREE_H_INCLUDED
#define BITREE_H_INCLUDED using namespace std; //定义结点 template <typename DataType> struct BiNode { DataType data; BiNode<DataType> *lchild,*rchild; }; template <typename DataType> class BiTree { public : // 构建函数,建立一颗二叉树  BiTree() { root = Creat(); } // 析构函数,释放格结点的存储空间 ~BiTree() { Release(root); } // 前序遍历二叉树 void PreOrder() { PreOrder(root); } // 中序遍历二叉树 void InOrder() { InOrder(root); } // 后序遍历二叉树 void PostOrder() { PostOrder(root); } // 判断数中是否存在X bool ExistX(DataType x) { return ExistX(root, x); } // 节点x所在的层数 int XLay(DataType x) { return XLay(root, x); } private: BiNode<DataType> * Creat(); void Release(BiNode<DataType> *bt); void PreOrder(BiNode<DataType> *bt); void InOrder(BiNode<DataType> *bt); void PostOrder(BiNode<DataType> *bt); bool ExistX(BiNode<DataType> *bt, DataType x); int XLay(BiNode<DataType> *bt, DataType x); BiNode<DataType> *root; }; // 构建函数,建立一颗二叉树 template <typename DataType> BiNode<DataType> *BiTree<DataType>::Creat() { BiNode<DataType>* bt; char ch; cin>>ch; // 输入结点的数据信息 if(ch == '#') bt=nullptr; // 建立一棵空树 else { bt = new BiNode<DataType>; bt->data = ch; bt->lchild = Creat(); // 递归建立左子树 bt->rchild = Creat(); // 递归建立右子树  } return bt; } // 析构函数,释放格结点的存储空间 template <typename DataType> void BiTree<DataType> ::Release(BiNode<DataType> * bt) { if(bt == nullptr) return; else { Release(bt ->lchild); Release(bt->rchild); delete bt; } } // 前序遍历二叉树 template <typename DataType> void BiTree<DataType> :: PreOrder(BiNode<DataType> * bt) { if(bt == nullptr) return ; else { cout<<bt->data; PreOrder(bt->lchild); PreOrder(bt->rchild); } } // 中序遍历二叉树 template <typename DataType> void BiTree<DataType> :: InOrder(BiNode<DataType> * bt) { if(bt == nullptr) return ; else { InOrder(bt->lchild); cout<<bt->data; InOrder(bt->rchild); } } // 后序遍历二叉树 template <typename DataType> void BiTree<DataType> :: PostOrder(BiNode<DataType> * bt) { if(bt == nullptr) return ; else { PostOrder(bt->lchild); PostOrder(bt->rchild); cout<<bt->data; } } // 判断是否存在X template <typename DataType> bool BiTree<DataType> :: ExistX(BiNode<DataType> * bt, DataType x) { if(bt == nullptr) return false; else if(bt->data == x) { return true; } else { if(ExistX(bt->lchild, x)) return true; if(ExistX(bt->rchild, x)) return true; } return false; } // 存在节点X的层数 template <typename DataType> int BiTree<DataType> :: XLay(BiNode<DataType> * bt, DataType x) { int cot=0; if(bt == nullptr) return cot; else if(bt->data == x) { cot = 1; return cot; } else { if(XLay(bt->lchild, x)) { cot = XLay(bt->lchild, x) + 1; return cot; } if(XLay(bt->rchild, x)){ cot = XLay(bt->rchild, x) + 1; return cot; } } return cot; } #endif // BITREE_H_INCLUDED

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM