Skip to main content
added 2879 characters in body; added 1 characters in body
Source Link
dting
  • 39.4k
  • 10
  • 98
  • 117

add two methodshttp://en.wikipedia.org/wiki/Recursion

For recursion to return your leftwork you need a base case, and a set of cases and actions that reduce towards the base case.

For counting the number of nodes in a binary trees, we can use the base case of "a node does not exists", and other case of "a node does exist".

For the other case, (getLeft()node does exist) and rightwe will add 1 to the count of nodes add the number of nodes in each of the tree's subtrees (getRight()left and right) to the total count. How do we find the number of nodes in the subtrees? Simply apply the same set of conditions that we used for the first node to the child nodes.

By repeatedly breaking down the tree's subnodes we can obtain the total number of nodes in the tree

Take for example:

 N1 /\ N2 N3 /\ \ N4 N5 N6 

Let's call our counting method countNodes().

pseudocode for countSubNodescountNodes

int countSubNodescountNodes(Node parent): if parent: return 1 + countSubNodescountNodes(parent.getLeft()left) + countSubNodescountNodes(parent.getRight()right) else: return 0 

countNodes will first check if the node you wantpass to count allit exists.

If not (base case) return 0 [logically this makes sense because if the node doesnt exist, there will be no nodes from rootin it's subtree's that dont exist]

countSubNodesIf the node exists you will return 1 + the sum of the number of nodes in the subtrees. To find the number of nodes in the subtrees we will just call our countNodes(root) method on each child node.

In the example tree above we start with N1. We see that N1 exists so what we need to find now is:

1 + # of nodes in the left subtree + # of nodes in the right subtree.

N1's subtrees are:

 Left Right N2 N3 /\ \ N4 N5 N6 

We start by calling the countNodes() method on N2 (N1's left subtree).

N2 exists so now we are looking for

1 + # of nodes in N2's left subtree + # of nodes in N2's right subtree

N2's subtrees are:

Left Right N4 N5 

calling countNodes() on N4 we are looking for

1 + # of nodes in N4's left subtree + # of nodes in N4's right subtree

N4's left node is null so when countNodes() is called on N4's left node it will return 0 (base case).

N4's right node is also null so countNodes() for the right node is also going to return 0

N4's pending operations can complete and you have 1 + 0 + 0 (non-base case return value)

Now N2's pending operation looks like

1 + 1 + # of nodes right subtree

calling countNodes() on N5 results in the same value as N4 so N2's return value has become:

1 + 1 + 1

N2 returns 3 to N1's pending operation to make it look like:

1 + 3 + # nodes in right subtree

# nodes in N3 subtree is: countNodes() on N3 returns 1 + countNodes->null (base) + countNodes()->N6 # nodes in N6 subtree is countNodes() on N6 returns 1 + countNodes->null (base) + countNodes()->null (base) return value = 1 # nodes in N3 subtree is 1 + 0 + 1 returns 2 finally the call on N1's countNodes() can complete with 1 + 3 + 2 countNodes() on N1 returns 6 

To count all the right nodes in a tree you can use the following pseudocode:

add two methods to return your left (getLeft()) and right (getRight()) nodes

pseudocode for countSubNodes

int countSubNodes(Node parent): if parent: return 1 + countSubNodes(parent.getLeft()) + countSubNodes(parent.getRight()) else: return 0 

if you want to count all the nodes from root

countSubNodes(root)

http://en.wikipedia.org/wiki/Recursion

For recursion to work you need a base case, and a set of cases and actions that reduce towards the base case.

For counting the number of nodes in a binary trees, we can use the base case of "a node does not exists", and other case of "a node does exist".

For the other case, (node does exist) we will add 1 to the count of nodes add the number of nodes in each of the tree's subtrees (left and right) to the total count. How do we find the number of nodes in the subtrees? Simply apply the same set of conditions that we used for the first node to the child nodes.

By repeatedly breaking down the tree's subnodes we can obtain the total number of nodes in the tree

Take for example:

 N1 /\ N2 N3 /\ \ N4 N5 N6 

Let's call our counting method countNodes().

pseudocode for countNodes

int countNodes(Node parent): if parent: return 1 + countNodes(parent.left) + countNodes(parent.right) else: return 0 

countNodes will first check if the node you pass to it exists.

If not (base case) return 0 [logically this makes sense because if the node doesnt exist, there will be no nodes in it's subtree's that dont exist]

If the node exists you will return 1 + the sum of the number of nodes in the subtrees. To find the number of nodes in the subtrees we will just call our countNodes() method on each child node.

In the example tree above we start with N1. We see that N1 exists so what we need to find now is:

1 + # of nodes in the left subtree + # of nodes in the right subtree.

N1's subtrees are:

 Left Right N2 N3 /\ \ N4 N5 N6 

We start by calling the countNodes() method on N2 (N1's left subtree).

N2 exists so now we are looking for

1 + # of nodes in N2's left subtree + # of nodes in N2's right subtree

N2's subtrees are:

Left Right N4 N5 

calling countNodes() on N4 we are looking for

1 + # of nodes in N4's left subtree + # of nodes in N4's right subtree

N4's left node is null so when countNodes() is called on N4's left node it will return 0 (base case).

N4's right node is also null so countNodes() for the right node is also going to return 0

N4's pending operations can complete and you have 1 + 0 + 0 (non-base case return value)

Now N2's pending operation looks like

1 + 1 + # of nodes right subtree

calling countNodes() on N5 results in the same value as N4 so N2's return value has become:

1 + 1 + 1

N2 returns 3 to N1's pending operation to make it look like:

1 + 3 + # nodes in right subtree

# nodes in N3 subtree is: countNodes() on N3 returns 1 + countNodes->null (base) + countNodes()->N6 # nodes in N6 subtree is countNodes() on N6 returns 1 + countNodes->null (base) + countNodes()->null (base) return value = 1 # nodes in N3 subtree is 1 + 0 + 1 returns 2 finally the call on N1's countNodes() can complete with 1 + 3 + 2 countNodes() on N1 returns 6 

To count all the right nodes in a tree you can use the following pseudocode:

edited body
Source Link
dting
  • 39.4k
  • 10
  • 98
  • 117

add two methods to return your left (getLeft()) and right (getRight()) nodes

pseudocode for countSubNodes

int countSubNodes(Node parent): if parent: return 1 + countSubNodes(parent.getLeft()) + countSubNodes(parent.getRight()) else: return 0 

if you want to count all the nodes from root

countSubNodes(root)

int countRightNodes(Node parent): if !parent: return 0 if has right node: return 1 + (count# of right nodes ofin left subtree) + (count# of right nodes ofin right subtree) else: return (# of right nodes ofin left subtree) 

add two methods to return your left (getLeft()) and right (getRight()) nodes

pseudocode for countSubNodes

int countSubNodes(Node parent): if parent: return 1 + countSubNodes(parent.getLeft()) + countSubNodes(parent.getRight()) else: return 0 

if you want to count all the nodes from root

countSubNodes(root)

int countRightNodes(Node parent): if !parent: return 0 if has right node: return 1 + (count right nodes of left subtree) + (count right nodes of right subtree) else: return # of right nodes of left subtree 

add two methods to return your left (getLeft()) and right (getRight()) nodes

pseudocode for countSubNodes

int countSubNodes(Node parent): if parent: return 1 + countSubNodes(parent.getLeft()) + countSubNodes(parent.getRight()) else: return 0 

if you want to count all the nodes from root

countSubNodes(root)

int countRightNodes(Node parent): if !parent: return 0 if has right node: return 1 + (# of right nodes in left subtree) + (# of right nodes in right subtree) else: return (# of right nodes in left subtree) 
edited body
Source Link
dting
  • 39.4k
  • 10
  • 98
  • 117

add two methods to return your left (getLeft()) and right (getRight()) nodes

pseudocode for countSubNodes

int countSubNodes(Node parent): if parent: return 1 + countSubNodes(parent.getLeft()) + countSubNodes(parent.getRight()) else: return 0 

if you want to count all the nodes from root

countSubNodes(root)

int countRightNodes(Node parent): if !parent: return 0 if parent.getRight()has right node: return 1 + countRightNodes(parent.getLeft()count right nodes of left subtree) + countRightNodes(parent.getRight()count right nodes of right subtree) else: return countRightNodes(parent.getLeft())# of right nodes of left subtree 

add two methods to return your left (getLeft()) and right (getRight()) nodes

pseudocode for countSubNodes

int countSubNodes(Node parent): if parent: return 1 + countSubNodes(parent.getLeft()) + countSubNodes(parent.getRight()) else: return 0 

if you want to count all the nodes from root

countSubNodes(root)

int countRightNodes(Node parent): if !parent: return 0 if parent.getRight(): return 1 + countRightNodes(parent.getLeft()) + countRightNodes(parent.getRight()) else: return countRightNodes(parent.getLeft()) 

add two methods to return your left (getLeft()) and right (getRight()) nodes

pseudocode for countSubNodes

int countSubNodes(Node parent): if parent: return 1 + countSubNodes(parent.getLeft()) + countSubNodes(parent.getRight()) else: return 0 

if you want to count all the nodes from root

countSubNodes(root)

int countRightNodes(Node parent): if !parent: return 0 if has right node: return 1 + (count right nodes of left subtree) + (count right nodes of right subtree) else: return # of right nodes of left subtree 
Post Undeleted by dting
added 285 characters in body
Source Link
dting
  • 39.4k
  • 10
  • 98
  • 117
Loading
deleted 90 characters in body
Source Link
dting
  • 39.4k
  • 10
  • 98
  • 117
Loading
Post Deleted by dting
Source Link
dting
  • 39.4k
  • 10
  • 98
  • 117
Loading