zyzz1974 2021-02-16 10:08 采纳率: 25%
浏览 17

(二叉树)为什么打印出的左子树高不是0

#include <iostream>
#include <malloc.h>
using namespace std;
#include <queue> 
class tre
{
private:
	struct binarytree* tree;
	void create(binarytree*& e);                  //树创建 
	void preOrder(binarytree* e);                //先序遍历 
	void inOrder(binarytree* e);                 //中序遍历 
	void postOrder(binarytree* e);               //后序遍历 
	void levelOrder(binarytree* e);               //层次遍历 
	int hight(binarytree*& e);                   //确定二叉树高度 
public:
	tre();
	tre(binarytree*& e);
	void trcreate() ;                //树创建 
	void trpreOrder();               //先序遍历 
	void trinOrder();                //中序遍历 
	void trpostOrder() ;             //后序遍历 
	void trlevelOrder();            //层次遍历 
	void trhight();                   //确定二叉树高度 
    void gethi();

};
typedef struct binarytree
{
	char element;
	struct binarytree* leftchild, * rightchild;
	int hl;                              //左子树高 
	int hr;                               //右子树高 
	int hi;                                 //树高 
}*tree, treenode;


int main()
{
	
	tre  fe;
	
      fe.trcreate();
      fe.gethi();
      
}
void tre::trpreOrder()
{preOrder(tree);} 
void tre::trpostOrder()
{postOrder(tree);} 
void tre::trlevelOrder()
{levelOrder(tree);}   
void tre::trinOrder()
{inOrder(tree);} 
void tre::trhight()
{hight(tree);}
void tre::trcreate(){
	create(tree);
} 
void tre::gethi()
{
	cout<< tree->hi<<endl;
	cout<<tree->hl;
}
tre::tre()
{
  tree=(binarytree*)malloc(sizeof(binarytree));
  tree->hi=0;
  tree->hl=0;
  tree->hr=0;
  
}
tre::tre(binarytree* &e)
{
	e = new treenode;
	tree = e;
	e->hi = 0; e->hl = 0; e->hr = 0; e->leftchild = NULL; e->rightchild = NULL;
}
void tre::create(binarytree*& e)
{
	
	char s;
	cin >> s;
	if (s == '#')
	{

		e = NULL;
		return;
	}
	e = new treenode;
	e->element = s;
	create(e->leftchild);
	create(e->rightchild);
	
}
void tre::preOrder(binarytree* e)
{

	if (e != NULL)
	{
		cout << e->element << " ";
		preOrder(e->leftchild);
		preOrder(e->rightchild);
	}

}
void tre::inOrder(binarytree* e)
{
	if (e != NULL)
	{

		preOrder(e->leftchild);
		cout << e->element << " ";
		preOrder(e->rightchild);
	}

}
void tre::postOrder(binarytree* e)
{
	if (e != NULL)
	{

		preOrder(e->leftchild);

		preOrder(e->rightchild);
		cout << e->element << " ";
	}
}
void tre::levelOrder(binarytree* e)
{
	queue <binarytree*> s;
	while (e != NULL)
	{
		cout << e->element << " ";
		if (e->leftchild != NULL)
			s.push(e->leftchild);
		if (e->rightchild != NULL)
			s.push(e->rightchild);
		if (s.empty()) return;
		e = s.front();
		s.pop();
	}
}
int tre::hight(binarytree* &e)
{
	
	if (e == NULL) return 0;
		e->hl=hight(e->leftchild);
		e->hr=hight(e->rightchild);
	if (e->hl > e->hr)
		return ++e->hl;
	else
		return ++e->hr;
	
}
  • 写回答

1条回答 默认 最新

  • 幻灰龙 2021-02-16 22:12
    关注

    左子树高度为什么应该是0?

    评论

报告相同问题?

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真