#include <iostream> #include <deque> #include <stack> using namespace std; enum {ERROR,OK,OVERFLOW = 0}; int Nil = ' '; typedef struct BiTNode { int data; BiTNode *lchild,*rchild,*parent; }BiTNode,*BiTree; bool InitBiTree(BiTree &T) { T = NULL; // return OK; } void DestroyBiTree(BiTree &T) { if (T) { if (T->lchild) { DestroyBiTree(T->lchild); } if (T->rchild) { DestroyBiTree(T->rchild); } free(T); T = NULL; } } void Create(BiTree &T) { int data; scanf("%d",&data); while (data != -1) { T = (BiTree)malloc(sizeof(BiTNode)); if (!T) { exit(OVERFLOW); } T->data = data; CreateBiTree(T->lchild); CreateBiTree(T->rchild); return; } T = NULL; } void CreateBiTree(BiTree &T) { deque<BiTree> q; Create(T); //创建一棵树. //利用层次遍历设置parent指针. if (T) { T->parent = NULL; q.push_back(T); while (!q.empty()) { BiTree t = q.front(); q.pop_front(); if (t->lchild) { t->lchild->parent = a; q.push_back(t->lchild); } if (t->rchild) { t->rchild->parent = a; q.push_back(t->rchild); } } } } bool BiTreeEmpty(BiTree T) { if (T) { return false; } return true; } int BiTreeDepth(BiTree T) { int i,j; if (!T) { return 0; } i = BiTreeDepth(T->lchild); j = BiTreeDepth(T->rchild); return i > j ? i + 1 : j + 1; } int Root(BiTree T) { if (BiTreeEmpty(T)) { return Nil; } else { return T->data; } } int value(BiTree p) { return p->data; } void Assign(BiTree p,int value) { p->data = value; } /* 函数功能:返回节点值等于s的节点指针. */ BiTree Point(BiTree T,int s) { deque<BiTree> q; if (T) { q.push_back(T); while (!q.empty()) { BiTree t = q.front(); q.pop_front(); if (t->data = s) { return t; } if (t->lchild) { q.push_back(t->lchild); } if (t->rchild) { q.push_back(t->rchild); } } } return NULL; } /* 使用队列实现对二叉树的查找.首先将根节点入队. */ int Parent(BiTree T,int e) { if (T) { BiTree t = Point(T,e); if (t && t != T) //确定根节点的值不为e. { return t->parent->data; } } return Nil; } int LeftChild(BiTree T,int e) { if(T) { BiTree t = Point(T,e); if (t && t->lchild) { return t->lchild->data; } } return Nil; } int RightChild(BiTree T,int e) { if (T) { BiTree t = Point(T,e); if (t && t->rchild) { return t->rchild->data; } } return Nil; } int LeftSibling(BiTree T,int e) { if (T) { BiTree t = Point(T,e); if (t && t != T && a->parent->lchild && a->parent->lchild != t) { return t->parent->data; } } return Nil; } int RightSibling(BiTree T,int e) { if (T) { BiTree t = Point(T,e); if (t && t != T && t->parent->rchild && p->parent->rchild != t) { return t->parent->rchild->data; } } return Nil; } /* 初始条件:二叉树T存在,p指向T中某个节点,LR为0或1.非空二叉树c与T不相叫且右子树 为空,跟军LR为0或1插入c为T中p所指结点的左或右子树。p所指结点原来的左或右子树 则成为c的右子树 */ bool InsertChild(BiTree p,int LR,BiTree c) { if (p) { if (LR == 0) //插入左子树 { c->rchild = p->lchild; if (c->rchild) { c->rchild->parent = c; } p->lchild = c; c->parent = p; } if (LR == 1) //插入右子树 { c->rchild = p->rchild; if (c->rchild) { c->rchild->parent = c; } p->rchild = c; c->parent = p; } return OK; } return ERROR; } /* 在二叉树中删除一个节点的孩子,即将该节点拥有的孩子节点全部释放. */ bool DeleteChild(BiTree p,int LR) { if (p) { if (LR == 0) { DestroyBiTree(p->lchild); } else { DestroyBiTree(p->rchild); } return OK; } return ERROR; } void PreOrderTraverse(BiTree T,bool (*visit)(int)) { if (T) { visit(T->data); PreOrderTraverse(T->lchild,visit); PreOrderTraverse(T->rchild,visit); } } void InOrderTraverse(BiTree T,bool (*visit)(int)) { if (T) { InOrderTraverse(T->lchild,visit); visit(T->data); InOrderTraverse(T->rchild,visit); } } bool InOrderTraverse1(BiTree T,bool (*visit)(int)) { stack<BiTree> q; if (T)//根指针入栈 { q.push(T); while (!q.empty()) { BiTree t = q.top(); while (t && t->lchild) //如果左子树存在的话,一直入栈. { q.push(t->lchild); } q.pop(); //出去空指针. if (!q.empty()) { t = q.top(); q.pop(); visit(t->data); q.push(t->rchild); } } return OK; } return ERROR; } bool InOrderTraverse2(BiTree T,bool (*visit)(int)) { stack<BiTree> q; BiTree p = T; while (p || !q.empty()) { if (p) { q.push(p); p = p->lchild; } else { p = q.top(); q.pop(); visit(p->data); p = p->rchild; } } return OK; } void PostOrderTraverse(BiTree T, bool (*visit)(int)) { if (T) { PostOrderTraverse(T->lchild,visit); PostOrderTraverse(T->rchild,visit); visit(T->data); } } void LevelOrderTraverse(BiTree T,bool (*visit)(int)) { deque<BiTree> q; if (T) { q.push_back(T); while (!q.empty()) { BiTree t = q.front(); q.pop_front(); visit(t->data); if (t->lchild) { q.push_back(t->lchild); } if (t->rchild) { q.push_back(t->rchild); } } } }