二叉树的建立
定义树的结构:
1 typedef int ElemType; 2 3 struct BinaryTreeNode 4 { 5 ElemType m_nValue;//结点值 6 BinaryTreeNode *m_pLeft;//左节点地址 7 BinaryTreeNode *m_pRight;//右节点地址 8 };
将二叉树构建成一个完整的,若不存在的节点用字符'#'代替,
1 BinaryTreeNode * CreateTree(BinaryTreeNode *bTree) 2 { 3 int input; 4 scanf("%d",&input); //按先序建立二叉树 5 if(input == ‘#’) 6 { 7 bTree = NULL; //置为NULL后结束 8 return bTree; 9 } 10 bTree = (BinaryTreeNode *)malloc(sizeof(BinaryTreeNode)); 11 bTree ->m_nValue = input; 12 bTree->m_pLeft = CreateTree(bTree->m_pLeft); 13 bTree->m_pRight = CreateTree(bTree->m_pRight); 14 return bTree; 15 }