二叉树
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树相关的知识,希望对你有一定的参考价值。
二叉树的定义:
二叉树是每个结点最多有两个子树的树结构。
性质:1、在二叉树的第i层上至多有2^i-1个结点。(i>=1)
2、深度为k的二叉树至多有(2^k)-1个结点。(k>=1)
3、具有n个结点的完全二叉树的深度为(log2n)+1。
4、对于任何一颗二叉树,如果其终端结点数为n0,度为2的结点数为n2,则n0=n2+1。
题:如果一个完全二叉树的结点总数为768个,求叶子结点的个数。
由二叉树的性质知:n0=n2+1,将之带入768=n0+n1+n2中得:768=n1+2n2+1,因为完全二叉树度为1的结点个数要么为0,要么为1,那么就把n1=0或 者1都代入公式中,很容易发现n1=1才符合条件。所以算出来n2=383,所以叶子结点个数n0=n2+1=384。
二叉树前序、中序、后序遍历:
源码:
#include <iostream> #include <stack> #include <queue> using namespace std; template <typename T> struct BitreeNode { T data; BitreeNode<T> *left; BitreeNode<T> *right; }; template <typename T> class Bitree { public: Bitree() :_Root(NULL) {} Bitree(T *arr, size_t sz) { size_t i = 0; _Root = create_tree(arr,sz,i); } ~Bitree() {} void BitreeFirstOrderNO() { FirstOrderNo(_Root); } void BitreeFirstOrder() { FirstOrder(_Root); } void BitreeMidOrderNO() { MidOrderNO(_Root); } void BitreeMidOrder() { MidOrder(_Root); } void BitreeLaterOrderNO() { LaterOrderNO(_Root); } void BitreeLaterOrder() { LaterOrder(_Root); } void BitreeFloorOrder() { FloorOrder(_Root); } protected: BitreeNode<T>* create_tree(T* arr, size_t sz, size_t &i)//建树 { if (*arr == NULL || arr[i] == '#' || i >= sz) return NULL; else { BitreeNode<T> *cur = new BitreeNode<T>; cur->data = arr[i]; i++; cur->left = create_tree(arr,sz,i);//1 i++; cur->right = create_tree(arr,sz,i);// return cur; } } void FirstOrderNo(BitreeNode<T> *Root)//前序遍历非递归 { stack<BitreeNode<T>*> s; BitreeNode<T> *cur = Root; while (cur || !s.empty()) { while (cur) { cout<<cur->data<<" "; s.push(cur); cur = cur->left; } BitreeNode<T> *tmp = s.top(); cur = tmp->right; s.pop(); } } void FirstOrder(BitreeNode<T> *Root)//递归 { if (Root == NULL) return; cout<<Root->data<<" "; FirstOrder(Root->left); FirstOrder(Root->right); } void MidOrderNO(BitreeNode<T> *Root)//中序遍历非递归 { stack<BitreeNode<T>*> s; BitreeNode<T> *cur = Root; while (cur || !s.empty()) { while (cur) { s.push(cur); cur = cur->left; } BitreeNode<T> *tmp = s.top(); cout<<tmp->data<<" "; cur = tmp->right; s.pop(); } } void MidOrder(BitreeNode<T> *Root)//递归 { if(Root==NULL) return; MidOrder(Root->left); cout<<Root->data<<" "; MidOrder(Root->right); } void LaterOrderNO(BitreeNode<T> *Root)//后序遍历非递归 { stack<BitreeNode<T>*> s; BitreeNode<T> *cur = Root; BitreeNode<T> *prev = NULL; s.push(cur); while (!s.empty()) { cur = s.top(); if ((cur->left == NULL && cur->right == NULL) || ((prev != NULL)&&((prev == cur->left)||(prev == cur->right)))) { cout<<cur->data<<" "; s.pop(); prev = cur; } else { if(cur->right) s.push(cur->right); if(cur->left) s.push(cur->left); } } } void LaterOrder(BitreeNode<T> *Root)//递归 { if(Root==NULL) return; LaterOrder(Root->left); LaterOrder(Root->right); cout<<Root->data<<" "; } void FloorOrder(BitreeNode<T> *Root)//层序遍历 { if (Root == NULL) return; queue<BitreeNode<T>*> q; BitreeNode<T> *cur = Root; q.push(cur); while (!q.empty()) { BitreeNode<T> *tmp = q.front(); cout<<tmp->data<<" "; q.pop(); if (tmp->left) { q.push(tmp->left); } if (tmp->right) { q.push(tmp->right); } } } private: BitreeNode<T> *_Root; }; void test() { char arr[] = {'1','2','3','#','4','#','#','5','#','#','6','#','7'}; int sz = sizeof(arr)/sizeof(arr[0]); Bitree<char> b(arr,sz); //b.BitreeFirstOrderNO(); //b.BitreeFirstOrder(); //b.BitreeMidOrderNO(); //b.BitreeMidOrder(); //b.BitreeLaterOrderNO(); //b.BitreeLaterOrder(); b.BitreeFloorOrder(); } int main() { test(); return 0; }
以上是关于二叉树的主要内容,如果未能解决你的问题,请参考以下文章