فعل الوندوز الان بارخص الاسعار

فعل الوندوز الان بارخص الاسعار
فقط ب30 ريال مدى الحياة
‏إظهار الرسائل ذات التسميات java. إظهار كافة الرسائل
‏إظهار الرسائل ذات التسميات java. إظهار كافة الرسائل

برنامج جافا لحذف node من شجرة بحث ثنائية.

0



 import java.util.*;

class BinaryTree {

int data;

BinaryTree left, right;

BinaryTree(int x) {

this.left=this.right=null; data=x;

}

public void insert( int i ) {

if (i <= data) {

if (left != null) left.insert(i);

else left = new BinaryTree( i );

}

else if (i >= data) {

if (right != null) right.insert(i);

else right = new BinaryTree( i );

}

}

public void inOrder(BinaryTree tree) {

if(tree!=null) {

inOrder(tree.left);

System.out.print(tree.data+" ");

inOrder(tree.right); }

}

public static int min(BinaryTree node) {

if (node.left == null) {

return node.data;

}

return min(node.left);

}

public static BinaryTree deleteNodeInBST(BinaryTree node, int data) {

if (null == node) {

System.out.println("Element is not in binary search tree");

return null;

}

if (data < node.data) {

node.left = deleteNodeInBST(node.left, data);

} else if (data > node.data) {

node.right = deleteNodeInBST(node.right, data);

} else { // case for equality

// Now we see that whether we can directly delete the node

if (node.left != null && node.right != null) {

int minInRightSubTree = min(node.right);

node.data = minInRightSubTree;

node.right = deleteNodeInBST(node.right, minInRightSubTree);

} else { // either one child or leaf node

if (node.left == null && node.right == null) {

node = null;

} else {// one child case

BinaryTree deleteNode = node;

node = (node.left != null) ? (node.left) : (node.right);

deleteNode = null;

}

}

}

return node;

}

public static void main (String args[]) {

char choice='y'; int n, d;

Scanner sc = new Scanner(System.in);

System.out.print("Enter Root node value = ");

n=sc.nextInt();

System.out.print("Enter ur Choice(y/n) = ");

choice=sc.next().charAt(0); BinaryTree ob=new BinaryTree(n);

while (choice == 'y') {

System.out.print("Enter node value = ");

n=sc.nextInt(); ob.insert(n);

System.out.print("Enter ur Choice(y/n) = ");

choice=sc.next().charAt(0);

}

System.out.print("\nInorder Traversal = "); ob.inOrder(ob);

System.out.print("\nEnter the Node to delete = ");

d = sc.nextInt(); ob.deleteNodeInBST(ob, d);

System.out.print("\n After deleting the Node "+d+" = "); ob.inOrder(ob);

}

}



Output:

Inorder Traversal = 2 10 16 50 110

Enter the Node to delete = 16

After deleting the Node 16 = 2 10 50 110

برنامج جافا لتنفيذ اجتياز INORDER و PREORDER و POSTORDER لشجرة بحث ثنائية.

0


class Node

{

int key;

Node left, right;

public Node(int item)

{

key = item;

left = right = null;

}

}

public class BinaryTree1

{

Node root;

BinaryTree1()

{

root = null;

}

void printPostorder(Node node)

{

if (node == null)

return;

printPostorder(node.left);

printPostorder(node.right);

System.out.print(node.key + " ");

}

void printInorder(Node node)

{

if (node == null)

return;

printInorder(node.left);

System.out.print(node.key + " ");

printInorder(node.right);

}

public void printPreorder(Node node)

{

if (node == null)

return;

System.out.print(node.key + " ");

printPreorder(node.left);

printPreorder(node.right);

}

void printPostorder() { printPostorder(root); }

void printInorder() { printInorder(root); }

void printPreorder() { printPreorder(root); }

public static void main(String[] args)

{

BinaryTree1 tree = new BinaryTree1();

tree.root = new Node(1);

tree.root.left = new Node(2);

tree.root.right = new Node(3);

tree.root.left.left = new Node(4);

tree.root.left.right = new Node(5);

System.out.println("Preorder traversal of binary tree is ");

tree.printPreorder();

System.out.println("\nInorder traversal of binary tree is ");

tree.printInorder();

System.out.println("\nPostorder traversal of binary tree is ");

tree.printPostorder();

}

}




Output

Preorder traversal of binary tree is

1 2 4 5 3

Inorder traversal of binary tree is

4 2 5 1 3

Postorder traversal of binary tree is

4 5 2 3 1 

برنامج جافا لحساب عاملي عدد معين باستخدام الركيرشن

0




java Program to demonstrate the use of recursion to find the factorial of a given number.


import java.util.Scanner;

class Factorial

{

public static void main(String args[])

{

int n, c, f = 1;

System.out.println("Enter number");

Scanner in = new Scanner(System.in);

n = in.nextInt();

if (n < 0)

System.out.println("Number should be non-negative.");

else

{

for (c = 1; c <= n; c++)

f = f*c;

System.out.println("Factorial of "+n+" is = "+f);

}

}



output:

Enter number

9

Factorial of 9 is = 362880

مقالات شائعة

جميع الحقوق محفوظه © مدونة فـاب

تصميم الورشه