Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The height of the node in AVL tree is wrong #1

Open
zhiyuanpeng opened this issue Nov 22, 2018 · 5 comments
Open

The height of the node in AVL tree is wrong #1

zhiyuanpeng opened this issue Nov 22, 2018 · 5 comments

Comments

@zhiyuanpeng
Copy link

I input the data 10, 4, 6. After the rebalance, the height of each of the three nodes should be ZERO. However, in your AVL tree code, the height isn't ZERO. You can check it.

@amitbansal7
Copy link
Owner

hey, i am not sure what are you trying to say. can you please elaborate?
And how height of all the three nodes can be zero? One of these three has to be the root node. right?
and the root can have minimum height of 1 if there are three nodes.

@zhiyuanpeng
Copy link
Author

hey, i am not sure what are you trying to say. can you please elaborate?
And how height of all the three nodes can be zero? One of these three has to be the root node. right?
and the root can have minimum height of 1 if there are three nodes.

The first input data 10 is the root, then the sencond data 4 is the left substree of 10. After the third data 6 is inserted in the AVL tree. The balance factor of the root is 2>1, so, the tree is unbalanced. This is a right of left unbalanced tree, so, after the rebalance processing, the three nodes' balance factors should all be ZERO. However, I run your code, the result is that the balance factors are not all zero.

@amitbansal7
Copy link
Owner

amitbansal7 commented Nov 29, 2018

void preorder(struct Node* root)
{
	if(root==NULL)
		return;

	printf("Data : %d  | Balancing factor : %d\n",root->data, Balance(root));
	preorder(root->left);
	preorder(root->right);
}

So i inserted (10, 4, 6) in that order. After rebalancing(left right case) i did the above preorder traversal. And this was the output. this looks good to me. Can you share the changes you made in the code?

Preorder traversal of tree is : 
Data : 6  | Balancing factor : 0
Data : 4  | Balancing factor : 0
Data : 10  | Balancing factor : 0

@zhiyuanpeng
Copy link
Author

zhiyuanpeng commented Nov 30, 2018 via email

@amitbansal7
Copy link
Owner

There is no attachment here.
And i am using the same code as in my repository with only changes made in preorder traversal which are shown above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants