1. 普通二叉树、完全二叉树、满二叉树
- 1.2 二叉树的遍历
二叉树的有三种遍历方式,先中后序遍历,“先、中、后”表示根节点的遍历时间。
先序:先遍历分支的根节点,再遍历左子树,最后遍历右子树。
先序序列:ABDFECGHI
中序:先遍历分支的左子树,再遍历根节点,最后遍历右子树。
中序序列:DBEFAGHCI
后序:先遍历分支的左子树,再遍历右子树,最后遍历根节点。
后序序列:DEFBHGICA
通过包含中序序列(通过前后序列创建出的二叉树不唯一)的两个序列推出 剩余的一个序列,算法分别用前中推后,中后推前。
typedef struct BinaryTreeNode { ElemType data; struct BinaryTreeNode *Left; struct BinaryTreeNode *Right; }*BinaryTree; void PreAndInToPost(ElemType* pre, ElemType* in,int length) { if (length == 0) return; int rootIndex; BinaryTree BT = new BinaryTreeNode; BT->data = *pre; for (rootIndex = 0; in[rootIndex] != *pre; rootIndex++); PreAndInToPost(pre + 1, in, rootIndex); PreAndInToPost(pre + rootIndex + 1, in + rootIndex + 1, length - (rootIndex + 1)); cout << BT->data << " "; } void PostAndInToPre(ElemType* in, ElemType* post,int length) { if (length == 0) return; int rootIndex; BinaryTree BT = new BinaryTreeNode; BT->data = *(post + length - 1); for (rootIndex = length - 1; in[rootIndex] != *(post + length - 1); rootIndex--); cout << BT->data << " "; PostAndInToPre(in, post, rootIndex); PostAndInToPre(in + rootIndex + 1, post + rootIndex, length - (rootIndex + 1)); }
- 1.3 二叉树的构建
二叉树的建立一般有两种方法:
(1)用数组的方式,或者一个个输入,按照节点的顺序,用一个特殊值(比如0,# )表示当前位置没有节点。但这样总归是会出现冲突的,比如节点的值就是0, # 的ASCII值。就不做写了。
(2)给定包含中序序列的两个序列。以下为 给定前序、中序序列时,创建二叉树。
1 #include <iostream> 2 using namespace std; 3 4 typedef char ElemType; 5 6 typedef struct BinaryTreeNode 7 { 8 ElemType data; 9 struct BinaryTreeNode *Left; 10 struct BinaryTreeNode *Right; 11 }*BinaryTree; 12 13 BinaryTree CreatBTFromPreAndIn(ElemType* pre, ElemType* in, int length) { 14 if (length == 0) return NULL; 15 int rootIndex; 16 BinaryTree BT = new BinaryTreeNode; 17 //将当前根节点入树 18 BT->data = *pre; 19 for (rootIndex = 0; in[rootIndex] != *pre; rootIndex++); 20 BT->Left = CreatBTFromPreAndIn(pre+1, in,rootIndex); 21 BT->Right = CreatBTFromPreAndIn(pre+rootIndex+1, in+rootIndex+1,length-(rootIndex+1)); 22 return BT; 23 } 24 BinaryTree CreatBTFromPostAndIn(ElemType* in, ElemType* post, int length) { 25 if (length == 0) return NULL; 26 int rootIndex; 27 BinaryTree BT = new BinaryTreeNode; 28 //将当前根节点入树 29 BT->data = *(post + length - 1); 30 for (rootIndex = length - 1; in[rootIndex] != *(post + length - 1); rootIndex--); 31 BT->Left = CreatBTFromPostAndIn(in, post, rootIndex); 32 BT->Right = CreatBTFromPostAndIn(in + rootIndex + 1, post + rootIndex/*删掉post的最后一个*/, length - (rootIndex + 1)); 33 34 return BT; 35 } 36 37 38 void Inorder(BinaryTree BT) { 39 if (!BT)return; 40 Inorder(BT->Left); 41 cout << BT->data << " "; 42 Inorder(BT->Right); 43 } 44 void Preorder(BinaryTree BT) { 45 if (!BT)return; 46 cout << BT->data << " "; 47 Preorder(BT->Left); 48 Preorder(BT->Right); 49 } 50 void Postorder(BinaryTree BT) { 51 if (!BT)return; 52 Postorder(BT->Left); 53 Postorder(BT->Right); 54 cout << BT->data << " "; 55 } 56 57 int main() { 58 char pre[] = "ABDGHCEIF"; 59 char in[] = "GDHBAEICF"; 60 char post[]= "GHDBIEFCA"; 61 BinaryTree BT1 = CreatBTFromPreAndIn(pre, in, strlen(in)); 62 BinaryTree BT2 = CreatBTFromPostAndIn(in, post, strlen(in)); 63 Postorder(BT1); 64 cout << endl; 65 Preorder(BT1); 66 cout << endl; 67 return 0; 68 }
前序、中序序列构建二叉树图解: