जावा प्रोग्राम एक पेड़ में पत्ती नोड्स की संख्या की गणना करने के लिए

इस उदाहरण में, हम जावा का उपयोग करके एक पेड़ में लीफ नोड्स की संख्या गिनना सीखेंगे।

इस उदाहरण को समझने के लिए, आपको निम्नलिखित जावा प्रोग्रामिंग विषयों का ज्ञान होना चाहिए:

  • जावा क्लास और ऑब्जेक्ट्स
  • जावा के तरीके

उदाहरण: एक पेड़ में लीफ नोड्स की संख्या की गणना करने के लिए जावा प्रोग्राम

 class Node ( int item; Node left, right; public Node(int key) ( item = key; left = right = null; ) ) class Main ( // root of Tree Node root; Main() ( root = null; ) // method to count leaf nodes public static int countLeaf(Node node) ( if(node == null) ( return 0; ) // if left and right of the node is null // it is leaf node if (node.left == null && node.right == null) ( return 1; ) else ( return countLeaf(node.left) + countLeaf(node.right); ) ) public static void main(String() args) ( // create an object of Tree Main tree = new Main(); // create nodes of tree tree.root = new Node(5); tree.root.left = new Node(3); tree.root.right = new Node(8); // create child nodes of left child tree.root.left.left = new Node(2); tree.root.left.right = new Node(4); // create child nodes of right child tree.root.right.left = new Node(7); tree.root.right.right = new Node(9); // call method to count leaf nodes int leafNodes = countLeaf(tree.root); System.out.println("Total Leaf Nodes = " + leafNodes); ) )

आउटपुट

 कुल पत्ती नोड्स = 4
लीफ नॉड्स की गिनती

उपरोक्त उदाहरण में, हमने जावा में ट्री डेटा संरचना को लागू किया है। यहां, हम पुनरावर्तन का उपयोग पेड़ में पत्ती नोड्स की संख्या की गणना करने के लिए कर रहे हैं।

अनुशंसित पढ़ना :

  • ट्री डेटा संरचना
  • बाइनरी ट्री कार्यान्वयन जावा में

दिलचस्प लेख...