c++: almost same input but 2nd call shows segment fault -


I've been a C # developer for a long time and try to refresh my previous C ++ knowledge I do not know That is why the "root" address of the second ISLN (Route) call becomes 0x0, of course, this question will be very clear for most C ++ developers. Please, give me some pointers.

Result: 1. What is balanced? 0 partition fault: 11

  #include & lt; Iostream & gt; using namespace std; Structure node {node * left; Node * true; }; Int Gatehite (node ​​* node) {if (node ​​== faucet) returns 0; If (node-> left == faucet & node-> true == faucet) returns 1; Int leftHeight = GetHeight (node-> left); Int rightHeight = GetHeight (node-> right); If left (left> gt; right right) 1 + left return; And 1+ correct return; } Bool isbalanced (node ​​and node) {int leftHight = 0; Int rightHeight = 0; If (node.left == NULL & amp; amp; node.right == NULL) return true; If (node.left! = NULL) leftHeight = GetHeight (node. Left); If (node.right! = NULL) rightHeight = GetHeight (node. Right); Int diff = leftHeight - rightHeight; If (diff> = -1 & amp; amp; amp;; diff & lt; = 1) {if (IsBalanced (* node.left) & IsBalanced (* node. Wright)) True; Second false return; } And false return; } int main () {node root, N1, N2, N3, N4, N5; Root.left = & amp; n1; Root.right = NULL; N1.left = & amp; N2; N1 Right = & amp; N3; N2.left = NULL; N2.right = NULL; N3.left = & amp; n4; N3.right = NULL; N4 Right = & amp; N5; N4.left = NULL; N5.right = NULL; N5.left = NULL; The court's & lt; & Lt; "1. Is it balanced?" & Lt; & Lt; Isalant (Route) & lt; & Lt; Endl; Root.left = & amp; n1; Root.right = & amp; n2; N1.left = & amp; n3; N1 Right = & amp; N4; N2.left = & amp; n5; N2.right = NULL; N3.left = NULL; N3.right = NULL; N4.right = NULL; N4.left = NULL; N5.right = NULL; N5.left = NULL; The court's & lt; & Lt; "2. Is it balanced?" & Lt; & Lt; Isalant (Route) & lt; & Lt; Endl; Return 0; }   

This row is the culprit.

  if (IsBalanced (* node.left)) and amp; IsBalanced (* node.right))   

You are not checking whether the node.left or node. Right tap before being removed. The first walk works due to the lucky coincidence.

This is an updated version of IsBalanced that works for me:

  bool isbalanced (node ​​& node) {int leftHeight = 0; Int rightHeight = 0; If (node.left == NULL & amp; amp; node.right == NULL) return true; If (node.left! = NULL) leftHeight = GetHeight (node. Left); If (node.right! = NULL) rightHeight = GetHeight (node. Right); Int diff = leftHeight - rightHeight; If (diff> = -1 & amp; amp; and diff & lt; = 1) {if (node.left == NULL || node .right == NULL} {back true; } And {return (Islawand (* node left) and IsBalanced (* node. Right); }} And return false; }    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -