鱼C论坛

 找回密码
 立即注册
查看: 3398|回复: 9

简单地二叉树的创建和遍历,有点不会,求指点

[复制链接]
发表于 2014-5-7 21:24:36 | 显示全部楼层 |阅读模式
5鱼币
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std ;

typedef struct btnode
{
        char val ;
        struct btnode *lchild ,*rchild ;
}BTNODE;

BTNODE* create_bt (BTNODE* node )
{
        int ch;
        //scanf("%c" ,&ch );
        cin>>ch;
        BTNODE *p ,*head=NULL;//operate
        p=node;
        p=(BTNODE*)malloc(sizeof(BTNODE));
        if('#'==ch)
        {
                p=NULL;
        }
        else
        {
                        if(p->val=ch ) head=p;
                        create_bt(p->lchild);
                        create_bt(p->rchild);
        }

        return (head) ;
}
void visit (char val ,int level)
{
        printf("%c是%d层\n" ,val ,level);
}
void preorder (BTNODE* node ,int level)
{
        BTNODE* p ;
        p=node;
        if(p)
        {
                visit(p->val , level );
                preorder(p->lchild ,level+1);
                preorder(p->rchild ,level+1);
        }
}
int main ()
{
        int level=0 ;
        BTNODE *head;
        head=create_bt(NULL);
        preorder(head ,level);
        return 0;
}

PS:我在创建的时候,不小心创建成了.cpp,所以出现了#inlcude<iostream> using namespace std ;
求大神指点

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-5-7 21:26:27 | 显示全部楼层
感觉在create_bt函数里面,出现了问题,但是不知道是怎么回事,求教
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-5-7 22:31:25 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. using namespace std;

  5. typedef struct btnode
  6. {
  7.         char val ;
  8.         struct btnode *lchild ,*rchild ;
  9. }BTNODE, *BiTree;

  10. // 二叉链表--排序树
  11. void Creat( BiTree &bt )
  12. {
  13.         char x;

  14.         scanf("%c", &x );
  15.         if( x == '#' )
  16.         {
  17.                 bt = NULL;
  18.         }
  19.         else
  20.         {
  21.                 bt = ( BiTree )malloc( sizeof( BTNODE ) );
  22.                 bt ->val = x;
  23.                 Creat( bt ->lchild );
  24.                 Creat( bt ->rchild );
  25.         }
  26. }

  27. void visit (char val ,int level)
  28. {
  29.         printf("%c是%d层\n" ,val ,level);
  30. }

  31. void preorder (BTNODE* node ,int level)
  32. {
  33.         BTNODE* p ;
  34.         p=node;
  35.         if(p)
  36.         {
  37.                 visit(p->val , level );
  38.                 preorder(p->lchild ,level+1);
  39.                 preorder(p->rchild ,level+1);
  40.         }
  41. }
  42. int main ()
  43. {
  44.         int level=0 ;
  45.         BTNODE *head = NULL;
  46.        
  47.         Creat( head );
  48.        
  49.         preorder(head ,level);
  50.        
  51.         return 0;
  52. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-5-7 22:50:09 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-5-8 11:13:31 | 显示全部楼层
运行了一下前面的程序,但运行不出结果,于是调试了一下,修改程序如下:
  1. //#include <stdio.h>
  2. //#include <stdlib.h>
  3. #include <iostream>
  4. using namespace std ;

  5. typedef struct btnode
  6. {
  7.         char val ;
  8.         struct btnode *lchild ,*rchild ;
  9. }BTNODE;

  10. BTNODE* create_bt (BTNODE* node )
  11. {
  12.         //int ch;
  13.         char ch;
  14.         //scanf("%c" ,&ch );
  15.         cin>>ch;
  16.         //BTNODE *p ,*head=NULL;//operate
  17.         //p=node;
  18.         //p=(BTNODE*)malloc(sizeof(BTNODE));
  19.         if('#'==ch)
  20.         {
  21.                 return NULL;
  22.         }
  23.         else
  24.         {
  25.                 node=(BTNODE*)malloc(sizeof(BTNODE));
  26.                 node->val=ch;
  27.                 node->lchild=create_bt(node->lchild);
  28.             node->rchild=create_bt(node->rchild);
  29.                         //if(p->val=ch ) head=p;
  30.                         //create_bt(p->lchild);
  31.                         //create_bt(p->rchild);
  32.         }

  33.         return (node) ;
  34. }
  35. void visit (char val ,int level)
  36. {
  37.         //printf("%c是%d层\n" ,val ,level);
  38.         cout<<val<<"是"<<level<<"层"<<endl;
  39. }
  40. void preorder (BTNODE* node ,int level)
  41. {
  42.         BTNODE* p ;
  43.         p=node;
  44.         if(p)
  45.         {
  46.                 visit(p->val , level );
  47.                 preorder(p->lchild ,level+1);
  48.                 preorder(p->rchild ,level+1);
  49.         }
  50. }
  51. int main ()
  52. {
  53.         int level=0 ;
  54.         BTNODE *head;
  55.         head=create_bt(NULL);
  56.         preorder(head ,level);
  57.         return 0;
  58. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-5-8 12:42:04 | 显示全部楼层
elvo 发表于 2014-5-8 11:13
运行了一下前面的程序,但运行不出结果,于是调试了一下,修改程序如下:

这样就可以了吗,感觉,还是有点问题呀,不管怎么样,先运行一下再说,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-5-10 08:42:10 | 显示全部楼层
可以看看的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-5-10 09:32:27 | 显示全部楼层
秦晓彬 发表于 2014-5-7 22:50
大神,好像形参是不可以分配内存的,形参是不能做左值的
不过,思想蛮好的,谢谢

左值相当于地址值,右值相当于数据值
&符号放在一个变量声明或者是函数的形参声明前就是引用
如果放在一个已经定义的变量前,就是取地址
。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-5-10 20:08:52 | 显示全部楼层
  1. package sun;

  2. import java.util.Scanner;

  3. public class BinaryTree {
  4.        
  5.         private Scanner scanner;
  6.         public static void main(String[] args) {
  7.                 BinaryNode<String> note = new BinaryNode<String>("start", null);
  8.                 BinaryTree bt = new BinaryTree();
  9.                 bt.createBiTree(note);
  10.                 bt.preorderTraversal(note);
  11.         }

  12.         //创建二叉树
  13.         public void createBiTree(BinaryNode<String> node){
  14.                 createLeftChild(node);
  15.                 createRightChild(node);
  16.         }
  17.         public void createLeftChild(BinaryNode<String> node){
  18.                 System.out.println("Please Input:");
  19.                 scanner = new Scanner(System.in);
  20.                 String data = scanner.next();
  21.                 if(!data.equals("e")){
  22.                         node.setLeftChild(new BinaryNode<String>(data, node));               
  23.                         createLeftChild(node.getLeftChild());
  24.                         createRightChild(node.getRightChild());
  25.                 }
  26.         }
  27.         public void createRightChild(BinaryNode<String> node){
  28.                 System.out.println("Please Input:");
  29.                 scanner = new Scanner(System.in);
  30.                 String data = (String) scanner.next();
  31.                 if(!data.equals("e")){
  32.                         node.setRightChild(new BinaryNode<String>(data, node));
  33.                         createLeftChild(node);
  34.                         createRightChild(node);
  35.                 }
  36.         }
  37.        
  38.         //前序遍历二叉树
  39.         public void preorderTraversal(BinaryNode<String> node){
  40.                 System.out.println(node.getData());
  41.                 if(node.getLeftChild() != null){
  42.                         preorderTraversal(node.getLeftChild());
  43.                 }
  44.                 if(node.getRightChild() != null){
  45.                         preorderTraversal(node.getRightChild());
  46.                 }
  47.         }
  48. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 19:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表