آخر الأخبار

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



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 

عرضاخفاءالتعليقات
الغاء

شركنا الاراء ،،

ad