二叉树的输入输出,遍历结果,和双亲结点的查找


/*20130701_denyz*/
/*二叉树的存储,输入,输出,遍历(DLR,LDR,RDL)*/
 /*查找值为x的结点的双亲结点*/
/*建立二叉树的二叉链表*/
#include <stdio.h>
#include <malloc.h>


typedef struct node{
        struct node *Lchild,*Rchild;
        char data;
        }Btree;
        
Btree *CreatTree()
{ 
     Btree *BT;
     char a;
     scanf("%c",&a);
     if(a==' ') 
        BT=NULL;
     else
         {
         BT=(Btree *)malloc(sizeof(Btree));    /*DLR method construct the binary tree*/
         BT->data=a;
         BT->Lchild=CreatTree();//create Left child tree
         BT->Rchild=CreatTree();//create Right child tree
         }
     return BT;/*the first time run the function,BT is the head pointor of the binary tree*/
}

/*DLR sequence*/
void DLR(Btree *BT)
{
     if(BT!=NULL)
     {
     printf("%c ",BT->data);
     DLR(BT->Lchild);
     DLR(BT->Rchild);
     }
}

/*LDR sequence*/
void LDR(Btree *BT)
{
     if(BT!=NULL)
     {
     LDR(BT->Lchild);
     printf("%c ",BT->data);
     LDR(BT->Rchild);
     }
}

/*LRD sequence*/
void LRD(Btree *BT)
{
     if(BT!=NULL)
     {
     LRD(BT->Lchild);
     LRD(BT->Rchild);
     printf("%c ",BT->data);
     }
}

/*查找值为 x 的双亲结点*/
Btree *find(Btree *BT,char X)
{
      char ch;
   
      ch=X;
      if(BT)
      { 
          if((BT->Lchild!=NULL && BT->Lchild->data==ch) || (BT->Rchild!=NULL && BT->Rchild->data==ch))
     {
      printf("存在值为%c的结点的孩子结点值为%c",BT->data,ch);
    
     }
    if((BT->Lchild!=NULL && BT->Lchild->data!=ch) || (BT->Rchild!=NULL && BT->Rchild->data!=ch))
            {
    
                find(BT->Lchild,ch);                        
                find(BT->Rchild,ch);
            }
            
      } 
     
}

int main()
{
    
     Btree *BT,*pos,*parent=NULL;   
  printf("Note:对于广义表为:a(b(d,x),c( ,e))的二叉树,\
   \n输入的时候叶子结点的左右结点以空格的形式输入,\n为空的结点也是以空格输入,输入的时候将括号省略\
   \nFor Example:abd  x  c e  \n\n");
 
     BT=CreatTree();     
     printf("DLR\n");
     DLR(BT);
     printf("\nLDR\n");
     LDR(BT);    
     printf("\nLRD\n");
     LRD(BT);  
     
     printf("\n");
     find(BT,'x');

 
     getch();
     return 1;
}






 

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我来为你解答。 首先,我们需要设计一个二叉树的节点构体,包含左右子树指针和节点值: ```c++ struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; ``` 接着,我们可以通过递归的方式来创建二叉树: ```c++ TreeNode* createTree() { int val; cin >> val; if (val == -1) { // -1 表示空节点 return nullptr; } TreeNode* root = new TreeNode(val); root->left = createTree(); root->right = createTree(); return root; } ``` 创建好二叉树后,我们可以通过递归的方式来遍历二叉树: ```c++ void preOrder(TreeNode* root) { // 前序遍历 if (!root) { return; } cout << root->val << " "; preOrder(root->left); preOrder(root->right); } void inOrder(TreeNode* root) { // 遍历 if (!root) { return; } inOrder(root->left); cout << root->val << " "; inOrder(root->right); } void postOrder(TreeNode* root) { // 后序遍历 if (!root) { return; } postOrder(root->left); postOrder(root->right); cout << root->val << " "; } ``` 计算二叉树的叶子节点个数和深度可以通过递归的方式来实现: ```c++ int countLeaf(TreeNode* root) { if (!root) { return 0; } if (!root->left && !root->right) { return 1; } return countLeaf(root->left) + countLeaf(root->right); } int getDepth(TreeNode* root) { if (!root) { return 0; } int leftDepth = getDepth(root->left); int rightDepth = getDepth(root->right); return max(leftDepth, rightDepth) + 1; } ``` 查找指定节点的双亲和兄弟节点可以通过递归的方式来实现: ```c++ void findSibling(TreeNode* root, int val, int& parent, int& sibling) { if (!root || (!root->left && !root->right)) { return; } if (root->left && root->left->val == val) { parent = root->val; sibling = (root->right) ? root->right->val : -1; return; } if (root->right && root->right->val == val) { parent = root->val; sibling = (root->left) ? root->left->val : -1; return; } findSibling(root->left, val, parent, sibling); findSibling(root->right, val, parent, sibling); } int findParent(TreeNode* root, int val) { if (!root || (!root->left && !root->right)) { return -1; } if ((root->left && root->left->val == val) || (root->right && root->right->val == val)) { return root->val; } int leftParent = findParent(root->left, val); if (leftParent != -1) { return leftParent; } int rightParent = findParent(root->right, val); if (rightParent != -1) { return rightParent; } return -1; } ``` 最后,我们可以在主函数测试这些函数: ```c++ int main() { TreeNode* root = createTree(); cout << "前序遍历:"; preOrder(root); cout << endl; cout << "遍历:"; inOrder(root); cout << endl; cout << "后序遍历:"; postOrder(root); cout << endl; cout << "叶子节点个数:" << countLeaf(root) << endl; cout << "树的深度:" << getDepth(root) << endl; int parent = -1, sibling = -1; findSibling(root, 4, parent, sibling); cout << "节点 4 的双亲为:" << findParent(root, 4) << ",兄弟为:" << sibling << endl; return 0; } ``` 这样就完成了二叉树的创建、遍历、计算叶子节点个数和深度、查找指定节点的双亲和兄弟节点的操作。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值