تمرين: الأشجار الثنائية المتكافئة
تمرين: الأشجار الثنائية المتكافئة
قد توجد أشجار ثنائية عديدة ومختلفة تخزّن التسلسل نفسه من القيم. على سبيل المثال، إليك شجرتين ثنائيتين تخزّنان التسلسل 1، 1، 2، 3، 5، 8، 13.
تكون الدالة التي تتحقق مما إذا كانت شجرتان ثنائيتان تخزّنان التسلسل نفسه معقدةً إلى حد كبير في معظم اللغات. سنستخدم التزامن والقنوات في Go لكتابة حل بسيط.
يستخدم هذا المثال حزمة tree التي تعرّف النوع:
type Tree struct {
Left *Tree
Value int
Right *Tree
}
تابع الوصف في الصفحة التالية.
النص الأصلي بالإنجليزية
Exercise: Equivalent Binary Trees
There can be many different binary trees with the same sequence of values stored in it. For example, here are two binary trees storing the sequence 1, 1, 2, 3, 5, 8, 13.
A function to check whether two binary trees store the same sequence is quite complex in most languages. We'll use Go's concurrency and channels to write a simple solution.
This example uses the tree package, which defines the type:
type Tree struct {
Left *Tree
Value int
Right *Tree
}
Continue description on next page.