PHP में एल्गोरिदम और डेटा संरचनाएँ: मूल बातों से लेकर कार्यान्वयन तक
आधुनिक वेब डेवलपमेंट में, एल्गोरिदम और डेटा संरचनाओं का ज्ञान केवल एक शैक्षणिक कौशल नहीं है, बल्कि तेज़, स्केलेबल और विश्वसनीय एप्लिकेशन बनाने के लिए एक व्यावहारिक उपकरण है। PHP, "सरल साइटों के लिए भाषा" के रूप में अपनी प्रतिष्ठा के बावजूद, जटिल कम्प्यूटेशनल कार्यों को लागू करने के लिए सभी आवश्यक साधन प्रदान करता है। इस लेख में, हम प्रमुख डेटा संरचनाओं और एल्गोरिदम पर चर्चा करेंगे, और सबसे महत्वपूर्ण बात, कोड उदाहरणों के साथ शुद्ध PHP में उन्हें कैसे लागू करें, यह दिखाएंगे।
1. PHP पर मौलिक डेटा संरचनाएँ
PHP में अंतर्निहित प्रकार (ऐरे, ऑब्जेक्ट) पहले से ही कई आवश्यकताओं को पूरा करते हैं, लेकिन गहन समझ और ऑप्टिमाइज़ेशन के लिए, शास्त्रीय संरचनाओं को मैन्युअल रूप से लागू करना उचित है।
1.1. स्टैक (Stack)
स्टैक LIFO (Last In, First Out) सिद्धांत पर काम करता है। इसका उपयोग क्रियाओं को पूर्ववत करने (undo), कोष्ठकों को पार्स करने, ग्राफ़ को गहराई में ट्रैवर्स करने के लिए किया जाता है।
<?phpclass Stack { private array $items = [];
public function push($item): void { array_push($this->items, $item); }
public function pop() { if ($this->isEmpty()) { throw new UnderflowException('Stack is empty'); } return array_pop($this->items); }
public function peek() { return end($this->items); }
public function isEmpty(): bool { return empty($this->items); }
public function size(): int { return count($this->items); }}
// उदाहरण: कोष्ठक संतुलन की जाँचfunction isBalanced(string $expression): bool { $stack = new Stack(); $pairs = ['(' => ')', '{' => '}', '[' => ']'];
for ($i = 0; $i < strlen($expression); $i++) { $char = $expression[$i]; if (isset($pairs[$char])) { $stack->push($char); } elseif (in_array($char, $pairs)) { if ($stack->isEmpty() || $pairs[$stack->pop()] !== $char) { return false; } } } return $stack->isEmpty();}
echo isBalanced("({[]})") ? "संतुलित" : "असंतुलित"; // संतुलित?>1.2. क्यू (Queue)
क्यू FIFO (First In, First Out) सिद्धांत पर काम करता है। इसका उपयोग कार्य कतार प्रणालियों, BFS (चौड़ाई में ट्रैवर्सल), डेटा बफ़रिंग में किया जाता है।
<?phpclass Queue { private array $items = [];
public function enqueue($item): void { $this->items[] = $item; }
public function dequeue() { if ($this->isEmpty()) { throw new UnderflowException('Queue is empty'); } return array_shift($this->items); }
public function peek() { return $this->items[0] ?? null; }
public function isEmpty(): bool { return empty($this->items); }
public function size(): int { return count($this->items); }}
// उदाहरण: प्रिंट क्यू का सिमुलेशन$printQueue = new Queue();$printQueue->enqueue("document1.pdf");$printQueue->enqueue("photo.jpg");$printQueue->enqueue("report.docx");
while (!$printQueue->isEmpty()) { echo "प्रिंट हो रहा है: " . $printQueue->dequeue() . "";}?>1.3. बाइनरी सर्च ट्री (BST)
ट्री तेज़ खोज, सम्मिलन और विलोपन (औसतन O(log n)) प्रदान करता है।
<?phpclass TreeNode { public $value; public ?TreeNode $left = null; public ?TreeNode $right = null;
public function __construct($value) { $this->value = $value; }}
class BinarySearchTree { public ?TreeNode $root = null;
public function insert($value): void { $this->root = $this->insertNode($this->root, $value); }
private function insertNode(?TreeNode $node, $value): TreeNode { if ($node === null)