예제 #1
0
파일: bt.hpp 프로젝트: sundyCoder/CSK
	int BItree<Dtype>::bt_height(TreeNode*& root) {
		if (root == nullptr) {
			return 0;
		}
		else {
			int lheight = bt_height(root->left);
			int rheight = bt_height(root->right);
			if (lheight > rheight) {
				return (lheight + 1);
			}
			else {
				return (rheight + 1);
			}
		}
	}
예제 #2
0
파일: btree.c 프로젝트: qd-cae/qd
STATIC int bt_height(NODE *node)
{
/*
  Computes the height of a node in the tree, recursively
*/
  int left, right;
  if(node->left)
    left = bt_height(node->left);
  else
    left = 0;
  if(node->right)
    right = bt_height(node->right);
  else
    right = 0;
  node->height = (left > right ? left : right) + 1;
}
예제 #3
0
파일: bt.hpp 프로젝트: sundyCoder/CSK
	vector<Dtype> BItree<Dtype>::bt_LevelOrderRe(TreeNode*& root) {
		int height = bt_height(root);
		vector<Dtype> result;
		for (int i = 1; i <= height; i++) { // 遍历每一层
			vector<Dtype> ret = bt_getLevel(root, i);
			if (ret.size() > 0) {
				result.insert(result.end(), ret.begin(), ret.end());
			}
		}
		return result;
	}
예제 #4
0
void main () 
{
	char		c;
	int		n;
	tree_pointer	t;

	t = build_simple_tree();

	printf("************* Command ************\n");
	printf("C: Count tree, A: Add tree data    \n");
	printf("H: Height of tree, S: Show preorder \n");
	printf("F: Free tree, Q: Quit              \n");
	printf("**********************************\n");

	while (1) {
		printf("\nCommand> ");
		c = _getch();
		_putch(c);
		c = toupper(c);
		switch (c) {
			case 'C' : 
				n = bt_count(t);
				printf ("\n Total number of node = %d \n", n);
				break;
			case 'A' : 
				n = bt_add(t);
				printf ("\n Sum of tree data = %d \n", n);
				break;
			case 'H' : 
				n = bt_height(t);
				printf ("\n Height of tree = %d \n", n);
				break;
			case 'S' : 
				printf ("\n");
				bt_show_preorder(t); 
				printf ("\n");
				break;
			case 'F' : 
				printf ("\n");
				free_bt(t); 
				printf ("\n");
				break;
			case 'Q' : 
				printf("\n");
				exit(1);
			default  : break;
		}
	}
}
예제 #5
0
파일: btree.c 프로젝트: qd-cae/qd
BT_verify(void *bti)
{
  BTREE *bt = (BTREE *) bti;
/*
  Compute height of every node
*/
  bt_height(bt->head);
  if(bt_verify(bt,bt->head) == -1) {
/* printf("TREE IS INVALID\n"); */
    return -1;
  } else {
/*    printf("Tree is valid\n");  */
    return 1;
  }
}
예제 #6
0
int bt_height(tree_pointer ptr){ // 트리의 높이를 계산
	if(ptr==NULL)
		return 0;
	return MAX(bt_height(ptr->left),bt_height(ptr->right))+1;
}