Lecture 11

  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Lecture 11 as PDF for free.

More details

  • Words: 557
  • Pages: 5
‫‪Data Structures‬‬ ‫‪Notes for Lecture 11‬‬ ‫‪Non Linear Data Structures‬‬ ‫‪By‬‬ ‫‪Samaher Hussein Ali‬‬ ‫‪2007-2008‬‬

‫‪Representation of Arithmetic Expression Using Binary Tree‬‬ ‫ﺗﻤﺜﻴﻞ اﻟﺘﻌﺎﺑﻴﺮ اﻟﺤﺴﺎﺑﻴﺔ ﺑﻮاﺳﻄﺔ اﻷﺷﺠﺎر اﻟﺜﻨﺎﺋﻴﺔ‬ ‫ﻣﻦ اﻟﺘﻄﺒﻴﻘﺎت اﻟﻤﻬﻤﺔ ﻟﻸﺷﺠﺎر اﻟﺜﻨﺎﺋﻴﺔ هﻲ اﺳﺘﺨﺪاﻣﻬﺎ ﻓﻲ ﺗﻤﺜﻴﻞ اﻟﺘﻌﺎﺑﻴﺮ اﻟﺤﺴﺎﺑﻴﺔ ﺣﻴﺚ إن‪-:‬‬ ‫ اﻟﻌﻤﻠﻴﺎت اﻟﺤﺴﺎﺑﻴﺔ ﺗﻤﺜﻞ ﺑﻌﻘﺪ ﻣﺘﻔﺮﻋﺔ‬‫ اﻟﻌﻮاﻣﻞ ﺗﻤﺜﻞ ﺑﻮاﺳﻄﺔ اﻷوراق‬‫ ﻣﺴﺘﻮﻳﺎت اﻟﺸﺠﺮة ﺗﻌﻜﺲ أﺳﺒﻘﻴﺎت ﺗﻨﻔﻴﺬ اﻟﻌﻤﻠﻴﺎت اﻟﺤﺴﺎﺑﻴﺔ‬‫ﻣﺜﺎل‪ -:‬اﺳﺘﺨﺪم اﻟﺸﺠﺮة اﻟﺜﻨﺎﺋﻴﺔ ﻟﺘﻤﺜﻴﻞ اﻟﺘﻌﺒﻴﺮ اﻟﺤﺴﺎﺑﻲ اﻟﺘﺎﻟﻲ ﺛﻢ ﻗﻢ ﺑﺰﻳﺎرﺗﻬﺎ‪-:‬‬ ‫)‪A=B*C+(8+D*E)/(F*2‬‬ ‫=‬

‫‪+‬‬

‫‪/‬‬

‫‪A‬‬

‫*‬

‫‪C‬‬

‫*‬

‫‪B‬‬

‫‪+‬‬

‫‪2‬‬ ‫‪C‬‬ ‫*‬

‫‪F‬‬

‫‪E‬‬

‫‪8‬‬

‫‪D‬‬

‫•‬

‫ﻋﻨﺪ زﻳﺎرﺗﻬﺎ ﺑﻄﺮﻳﻘﺔ اﻟﻤﺮور اﻟﺴﺎﺑﻖ ﻧﺤﺼﻞ ﻋﻠﻰ اﻟﺘﻌﺒﻴﺮ اﻟﺘﺎﻟﻲ‪-:‬‬

‫‪=A+*BC/+8*DE*F2‬‬

‫•‬

‫ﻋﻨﺪ زﻳﺎرﺗﻬﺎ ﺑﻄﺮﻳﻘﺔ اﻟﻤﺮور اﻟﺒﻴﻨﻲ ﻧﺤﺼﻞ ﻋﻠﻰ اﻟﺘﻌﺒﻴﺮ اﻟﺘﺎﻟﻲ‪-:‬‬

‫)‪A=B*C+(8+D*E)/(F*2‬‬

‫•‬

‫ﻋﻨﺪ زﻳﺎرﺗﻬﺎ ﺑﻄﺮﻳﻘﺔ اﻟﻤﺮور اﻟﻼﺣﻖ ﻧﺤﺼﻞ ﻋﻠﻰ اﻟﺘﻌﺒﻴﺮ اﻟﺘﺎﻟﻲ‪-:‬‬

‫‪1‬‬

‫=‪ABC*8DE*+F2*/‬‬

‫‪Binary Search Tree‬‬ ‫ﺷﺠﺮة اﻟﺒﺤﺚ اﻟﺜﻨﺎﺋﻴﺔ‬ ‫هﻲ ﺷﺠﺮة ﺛﻨﺎﺋﻴﺔ ﻣﺮﺗﺒﺔ ﺗﻜﻮن اﻟﻘﻴﻤﺔ اﻟﺒﻴﺎﻧﻴﺔ ﻷﻳﺔ ﻋﻘﺪة ﻓﻴﻬ ﺎ ه ﻲ اآﺒ ﺮ ﻣ ﻦ اﻟﻘﻴﻤ ﺔ اﻟﺒﻴﺎﻧﻴ ﺔ ﻟﻠﻔ ﺮع اﻷﻳﺴ ﺮ واﺻ ﻐﺮ ﻣ ﻦ اﻟﻘﻴﻤ ﺔ اﻟﺒﻴﺎﻧﻴ ﺔ ﻟﻠﻔ ﺮع‬ ‫اﻷﻳﻤﻦ‪.‬‬ ‫ﻣﺜﺎل‪-:‬‬ ‫‪50‬‬

‫‪20‬‬

‫‪90‬‬

‫‪10‬‬ ‫‪80‬‬

‫‪70‬‬

‫‪88‬‬

‫ﻣﻼﺣﻈﺔ‪ -:‬ﻳﺠﺐ ﻣﻌﺮﻓﺔ ﺗﺴﻠﺴﻞ اﻷﺣﺮف ﺑﻠﻐﺔ اﻹﻧﻜﻠﻴﺰﻳﺔ ﺣﻴﺚ هﻲ‪-:‬‬ ‫‪ABCDEFGHIJKLMNOPQRSTUVWXYZ‬‬ ‫ﻣﺜﺎل‪ -:‬ارﺳﻢ ﺷﺠﺮة اﻟﺒﺤﺚ اﻟﺜﻨﺎﺋﻴﺔ ﻟﻠﻌﻨﺎﺻﺮ اﻟﺘﺎﻟﻴﺔ‬ ‫]‪[G,E,C,A,F,B,D‬‬ ‫اﻟﺠﻮاب‪-:‬‬ ‫أو ً‬ ‫ﻻ‪ -:‬ﻧﺄﺧﺬ اﻟﻌﻨﺼﺮ وهﻮ ‪D‬‬

‫وﻧﻌﺘﺒﺮﻩ اﻟﺠﺬر‬ ‫‪D‬‬

‫ﺛﺎﻧﻴًﺎ‪ -:‬ﻧﺄﺧﺬ اﻟﻌﻨﺼﺮ اﻟﺜﺎﻧﻲ وهﻮ ‪ B‬وﻟﻜﻮﻧﻪ اﺻﻐﺮ ﻣﻦ اﻟﺠﺬر ﻓﻨﻌﺘﺒﺮﻩ اﺑﻦ أﻳﺴﺮ‬ ‫‪D‬‬

‫‪B‬‬

‫ﺛﺎﻟﺜًﺎ‪ -:‬ﻧﺄﺧﺬ اﻟﻌﻨﺼﺮ اﻟﺜﺎﻟﺚ وهﻮ ‪ F‬وﻟﻜﻮﻧﻪ اآﺒﺮ ﻣﻦ اﻟﺠﺬر ﻓﻨﻌﺘﺒﺮﻩ اﺑﻦ أﻳﻤﻦ‬ ‫‪D‬‬

‫‪B‬‬

‫‪F‬‬

‫‪2‬‬

‫راﺑﻌ ًﺎ‪ -:‬ﻧﺄﺧﺬ اﻟﻌﻨﺼﺮ اﻟﺮاﺑﻊ وهﻮ‬

‫ﻓﻨﻌﺘﺒﺮﻩ اﺑﻦ أﻳﺴﺮ ﻟﻪ‬

‫‪ A‬وﻟﻜﻮﻧﻪ اﺻﻐﺮ ﻣﻦ ‪B‬‬

‫‪D‬‬

‫‪B‬‬

‫‪F‬‬

‫‪A‬‬

‫ﺧﺎﻣﺴ ًﺎ‪ -:‬ﻧﺄﺧﺬ اﻟﻌﻨﺼﺮ اﻟﺨﺎﻣﺲ وهﻮ ‪C‬‬

‫وﻟﻜﻮﻧﻪ اآﺒﺮ‬

‫‪ B‬ﻣﻦ ﻓﻨﻌﺘﺒﺮﻩ اﺑﻦ أﻳﻤﻦ ﻟﻬﺎ‬ ‫‪D‬‬

‫‪B‬‬

‫‪F‬‬

‫‪A‬‬

‫‪C‬‬

‫ﺳﺎدﺳًﺎ‪ -:‬ﻧﺄﺧﺬ اﻟﻌﻨﺼﺮ اﻟﺴﺎدس وهﻮ ‪ E‬وﻧﻘﺎرﻧﻪ ﻣﻊ ﻋﻘﺪ اﻟﺸﺠﺮة ﻧﺠﺪﻩ اﺻﻐﺮ ﻣﻦ اﻟﻌﻘﺪة ‪F‬‬

‫وﻟﻜﻮﻧﻪ اﺻﻐﺮ ﻟﺬﻟﻚ ﻓﻨﻌﺘﺒﺮﻩ اﺑﻦ أﻳﺴﺮ ﻟﻬﺎ‬

‫‪D‬‬

‫‪B‬‬

‫‪F‬‬

‫‪A‬‬

‫‪C‬‬ ‫‪E‬‬

‫ﺳﺎﺑﻌ ًﺎ‪ -:‬ﻧﺄﺧﺬ اﻟﻌﻨﺼﺮ اﻟﺴﺎﺑﻊ وهﻮ ‪G‬‬

‫وﻧﻘﺎرﻧﻪ ﻣﻊ ﻋﻘﺪ اﻟﺸﺠﺮة ﻧﺠﺪﻩ اآﺒﺮ ﻣﻦ اﻟﻌﻘﺪة ‪ F‬ﻟﺬﻟﻚ ﻓﻨﻌﺘﺒﺮﻩ اﺑﻦ أﻳﻤﻦ ﻟﻬﺎ‬ ‫‪D‬‬

‫‪B‬‬

‫‪F‬‬

‫‪G‬‬

‫‪C‬‬

‫‪E‬‬

‫ﺧﻄﻮات ﺣﺬف ﻋﻘﺪة ﻣﻦ ﺷﺠﺮة اﻟﺒﺤﺚ اﻟﺜﻨﺎﺋﻴﺔ‬ ‫* ﺣﺬف ﻋﻘﺪة ﻧﻬﺎﺋﻴﺔ‬ ‫وﺗﺘﻀﻤﻦ ﺧﻄﻮﺗﻴﻦ‪-:‬‬ ‫أ‪ .‬ﻧﺄﺧﺬ اﻟﻌﻘﺪة وﻧﺠﻌﻞ ﻗﻴﻤﺔ ﻣﺆﺷﺮ اﻷب إﻟﻴﻬﺎ‬ ‫ب‪ .‬ﻧﻠﻐﻲ ﺗﻠﻚ اﻟﻌﻘﺪة‬

‫‪null‬‬

‫‪Dispose‬‬

‫‪3‬‬

‫‪A‬‬

‫ﻣﺜﺎل‪ -:‬اﺣﺬف اﻟﻌﻘﺪة‬

‫‪A‬‬

‫ﻣﻦ ﺷﺠﺮة اﻟﺒﺤﺚ اﻟﺜﻨﺎﺋﻴﺔ اﻟﺘﺎﻟﻴﺔ‬

‫‪P‬‬

‫‪P‬‬

‫اﻟﺸﺠﺮة ﺑﻌﺪ اﻟﺤﺬف‬ ‫‪Z‬‬

‫‪Z‬‬

‫‪J‬‬

‫‪J‬‬

‫‪L‬‬

‫‪L‬‬

‫‪A‬‬

‫* ﺣﺬف ﻋﻘﺪة ﻟﻬﺎ ﻓﺮع واﺣﺪ‬ ‫وﺗﺘﻀﻤﻦ ﺧﻄﻮﺗﻴﻦ‪-:‬‬ ‫أ‪ .‬ﻧﺠﻌﻞ ﻣﺆﺷﺮ اﻟﻌﻘﺪة اﻷب ﻳﺸﻴﺮ إﻟﻰ اﻟﻌﻘﺪة اﻻﺑﻦ‬ ‫ب‪ .‬ﻧﻠﻐﻲ ﺗﻠﻚ اﻟﻌﻘﺪة‬ ‫ﻣﺜﺎل‪ -:‬اﺣﺬف اﻟﻌﻘﺪة‬

‫‪Dispose‬‬ ‫‪C‬‬

‫ﻣﻦ ﺷﺠﺮة اﻟﺒﺤﺚ اﻟﺜﻨﺎﺋﻴﺔ اﻟﺘﺎﻟﻴﺔ‬

‫‪L‬‬

‫‪L‬‬

‫اﻟﺸﺠﺮة ﺑﻌﺪ اﻟﺤﺬف‬ ‫‪P‬‬

‫‪P‬‬

‫‪D‬‬

‫‪A‬‬

‫‪H‬‬

‫‪F‬‬

‫‪D‬‬

‫‪C‬‬

‫‪H‬‬

‫‪F‬‬

‫* ﺣﺬف ﻋﻘﺪة ﻟﻬﺎ ﻓﺮﻋﺎن‬ ‫وﺗﺘﻀﻤﻦ ﻋﺪة ﺧﻄﻮات‪-:‬‬ ‫أ‪ .‬ﻧﺴﺘﺒﺪل اﻟﻌﻘﺪة اﻟﻤﻄﻠﻮب ﺣﺬﻓﻬﺎ ﺑﺎﻟﻌﻘﺪة اﻟﺘﺎﻟﻴﺔ ﻟﻬﺎ ﺑﺎﻟﻘﻴﻤﺔ ﻣﻦ اﻟﺸﺠﺮة اﻟﻔﺮﻋﻴﺔ اﻟﻴﺴﺮى او اﻟﻴﻤﻨﻰ ﺑﺎﻟﻨﺴﺒﺔ ﻟﻠﺸﺠﺮة‬ ‫ب‪ .‬ﻧﺄﺧﺬ اﻟﺸﺠﺮة اﻟﻔﺮﻋﻴﺔ اﻟﻴﺴﺮى ﻟﻠﻌﻘﺪة )أي اﻟﻌﻘﺪة اﻟﺘﻲ ﻓﻲ ﻳﺴﺎر اﻟﻌﻘﺪة اﻟﻤﻄﻠﻮب ﺣﺬﻓﻬﺎ(‬ ‫* إذا ﻟﻢ ﻳﻜﻦ ﻟﻬﺎ ﻓﺮع اﻳﻤﻦ ﻓﺄﻧﻬﺎ ﺗﺼﺒﺢ اﻟﺒﺪﻳﻞ‬ ‫* إذا آﺎن ﻟﻬﺎ ﻓﺮع اﻳﻤﻦ ﻓﺈﻧﻨﺎ ﻧﺄﺧﺬ اﻟﻌﻘﺪة ﻓﻲ أﻗﺼﻰ اﻟﻴﻤﻴﻦ ﻟﺘﺼﺒﺢ اﻟﺒﺪﻳﻞ‬

‫‪4‬‬

‫‪A‬‬

‫ﻣﺜﺎل‪ -:‬اﺣﺬف اﻟﻌﻘﺪة ‪q‬‬

‫ﻣﻦ ﺷﺠﺮة اﻟﺒﺤﺚ اﻟﺜﻨﺎﺋﻴﺔ اﻟﺘﺎﻟﻴﺔ‬

‫‪J‬‬

‫‪q‬‬

‫‪B‬‬

‫‪R‬‬

‫‪Z‬‬

‫‪L‬‬

‫‪K‬‬

‫‪N‬‬

‫‪p‬‬

‫‪M‬‬

‫اﻟﺸﺠﺮة ﺑﻌﺪ اﻟﺤﺬف‬

‫‪J‬‬

‫‪B‬‬

‫‪P‬‬

‫‪R‬‬

‫‪Z‬‬

‫‪L‬‬

‫‪K‬‬

‫‪N‬‬

‫‪M‬‬

‫ﻟﺤﺬف اﻟﻌﻘﺪة ‪q‬‬

‫‪-:‬‬

‫ﻻ‪ -:‬ﻧﺠﺪ ان ﻟﻬﺎ ﻓﺮﻋﺎن‬ ‫او ً‬

‫‪L‬‬

‫ﺛﺎﻧﻴًﺎ‪ -:‬ﻧﺎﺧﺬ اﻟﻔﺮع اﻻﻳﺴﺮ وهﻮ‬ ‫اﻟﺠﺪﻳﺪة وهﻲ‬

‫و‬

‫‪R‬‬ ‫‪L‬‬

‫وﻧﺠﺪ آﺬﻟﻚ ان ﻟﻪ اﺑﻦ اﻳﻤﻦ وهﻮ‬

‫‪P‬‬

‫‪5‬‬

‫‪N‬‬

‫ﻟﺬﻟﻚ ﻧﺄﺧﺬ اﻟﻌﻘﺪة اﻟﻤﻮﺟﻮدة ﻓﻲ أﻗﺼﻰ اﻟﻴﻤﻴﻦ ﻟﺘﺼﺒﺢ اﻟﻌﻘﺪة‬

Related Documents

Lecture 11
April 2020 10
Lecture 11
August 2019 28
Lecture 11
October 2019 27
Lecture 11
November 2019 22
Lecture 11
December 2019 26
Lecture 11
November 2019 25