???(binary Tree) - PowerPoint PPT Presentation

About This Presentation
Title:

???(binary Tree)

Description:

Title: PowerPoint Presentation Last modified by: ypb Created Date: 1/1/1601 12:00:00 AM Document presentation format: Other titles – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 49
Provided by: educ5490
Category:
Tags: binary | tree

less

Transcript and Presenter's Notes

Title: ???(binary Tree)


1
??? ?????
  • 6.1??????
  • 6.2???
  • 6.3?????
  • 6.4?????
  • 6.5????
  • 6.6????(????????)

2
6.1??????
6.1.1?????6.5
3
6.2???
  • 6.2.1???????????
  • ???(binary Tree)
  • n???????,????,?????????,???????????????,??????????
    ?????????????????,???????????
  • ??????????
  • ???,???,??,??,???,??,??
  • ???????????
  • ????????????????
  • ???????1,???k?????????k1
  • ????????????????????

4
  • ????(full binary tree)??????2,?????????
  • ?????(complete binary tree)??h,h-1?????,h????????
    ??
  • ????????
  • InitBiTree(T)
  • DestroyBiTree(T)
  • CreateBiTree(T,definition)
  • BiTreeEmpty(T)
  • BiTreeDepth(T)
  • Parent(T,e)
  • LeftChild(T,e)
  • RightChild(T,e)
  • LeftSibling(T,e)
  • RightSibling(T,e)
  • InsertChild(T,p,LR,C)
  • DeleteChild(T,p,LR)
  • Traverse(T)

5
6.2.2??????????
  • ??1 ??????i???????? 2 (i-1)
  • ??2 ???k ???????????2k-1
  • ??3 ?????T,?????????n0, ??2?????n2,?n0n21
    n0n1n2n2n2n11
  • ??4 ??n????????????logn1

6
  • ??5 ??????n?????????T??????(??????logn1?,?????
    ?)?1????,?????????i(1ltiltn)????
  • ??i1,??????????,?????igt1,,??????Parent(i)????i/
    2
  • ??2igtn,????i ????????,???????????LChild(i)????2i
  • ??2i1gtn,????i ??????????????RChild(i)????2i1

7
6.2.3????????
  • ??????
  • Const MAXSIZE100
  • typedef struct
  • ElemType data
  • int nodenum
  • SqBiTree

8
  • ??????
  • ???? ??data,lchild,rchild???,?????????
  • ???? ??data,lchild,rchild,parent???,?????
  •      
  • typedef BiTNode
  • ElemType data
  • struct BiTNode lchild,rchild,parent
  • BiTNode,BiTree

9
6.3?????
  • 6.3.1?????
  • ?????
  • ?????????,???????
  • LDR?LRD?DLR?DRL?RLD?RDL????
  • ??(?)DLR???(?)LDR???(?)LRD
  • ??????????
  • ????ABDEC
  • ????DBEAC
  • ????DEBCA

10
6.3.2???????
  • ?? ????
  • void preorder(BiTree T,void ( visit)(BiTree))
  • ?? ?????
  • typedef enum Travel1,Visit0 TaskType
  • typedef struct
  • BiTree ptr
  • TaskType task
  • SElemType
  • void InOrder_iter (BiTree BT,void (
    visit)(BiTree))
  • ??????

11
?????
  • Void InOrder(BiTree BT,void (visit)(BiTree T))
  • InitStack(S)
  • e.ptrBTe.tasktravel
  • if(BT)push(S,e)
  • while(!StackEmpty(S))
  • Pop(S,e)
  • if(e.taskVisit) visit(e.ptr)
  • else if(e.ptr)
  • pe.ptr
  • e.ptrp-gtrchilde.taskTravelPush(s,e)
  • e.ptrpe.taskVisitPush(s,e)
  • e.ptrp-gtlchilde.taskTravelPush(s,e)
  • //if
  • //while
  • //InOrder

12
??6.4??????????
  • typedef strunct BiTree T int flag SElemType
  • Void postorder(BiTree)
  • InitStack(S)
  • while(T!StackEmpty(S))
  • while(T) //???
  • e(T,0)Push(S,e)TT-gtlchild
  • if(!StackEmpty(S))pop(S,e) //??
  • if(e.flag0)e.flag1Push(S,e)
    Te.T-gtrchild
  • if(e.flag1)visit(T-gtdata)TNULL

13
6.3.3???????
  • ? ??????????(?????????)
  • void CreateBiTree(BiTree T) ?? ??
  • cingtgtch
  • if(ch)TNULL
  • else
  • Tnew BiTNode
  • T-gtdatach
  • CreateBiTree(T-gtlchild)
  • CreateBiTree(T-gtrchild)

14
  • ? ????? ???? ??
  • Bitree CopyTree(BiTree T)
  • if(!T)return NTLL
  • newlpCopyTree(T-gtlchild)
  • newrpCopyTree(T-gtrchild)
  • newnodenew BiTNode
  • newnode-gtdataT-gtdata
  • newnode-gtlchildnewlp
  • newnode-gtrchildnewrp
  • return newnode

15
  • ? ?????????????? (??)
  • void Count(BiTree T,int C1, int C2)
  • if(!T)return
  • C1
  • if(T-gtlchildNULL T-gtrchildNULL)C2
  • count(T-gtlchild,C1,C2)
  • count(T-gtrchild,C1,C2)

16
  • ?6.2?????????????
  • void level(BiTree,int lev) //lev1??
  • if(!T) return
  • coutltltT-gtdataltltltltlevltltendl
  • level(T-gtlchild,lev1)
  • level(T-gtrchild,lev1)
  • void numb(BiTree,int lev) //?? ?????????
  • if(!T) return
  • lev
  • coutltltT-gtdataltltltltlevltltendl
  • numb (T-gtlchild,lev)
  • numb (T-gtrchild,lev)

17
  • ?6.3??????? ????
  • int Depth(BiTree T) ??
  • void Depth2(BiTree T,int h,int depth)??
  • if(!T) return
  • if(hgtdepth)depthh
  • Depth2(T-gtlchild,h1,depth)
  • Depth2(T-gtrchild,h1,depth)
  • ?6.5???????????????
  • ??6.10 double Calculate(BiTree )
  • ?????????? -ab(c-d)-(-f)

18
6.4?????
  • ????????????????,?????????,???????????????????
    ???
  • typedef BiTHrNode
  • ElemType data
  • struct BiTHrNode lchild,rchild
  • struct BiTHrNode pred,succ
  • BiTHrNode,BiThrTree

19
????????
  • Void InOrder(BiThrTree H,void ( visit)(BiTree))
  • //H???????????????
  • pH-gtsucc
  • while(p!H)
  • visit(p)
  • pp-gtnext

20
??????????
  • Void InOrderThreading(BiThrTree H, BiThrTree T)
  • Hnew BiThrNode
  • H-gtlchildTH-gtrchildNULL
  • if(!T)H-gtpredH-gtsuccH
  • else
  • preH
  • InThreading(T,pre)
  • pre-gtsuccHH-gtpredpre
  • //InOrderThreading

21
  • Void InThreading(BiThrTree p, BiThrTree pre)
  • //p?????,pre?????,?p?????
  • if(!p) return
  • Inthreading(p-gtlchild,pre)
  • pre-gtsuccp
  • p-gtpredpre
  • prep
  • Inthreading(p-gtrchild,pre)
  • //InThreading

22
6.5????
  • 6.5.1???????
  • ???? (???)
  • n(ngt0)?????(??)????D,?D???,???????
  • ?D??????????????
  • ?ngt1?,???????m(mgt0)??????????T1,T2,......,Tm,?????
    ????????,???????
  • ?????
  • ?m(mgt0)??????????

23
  • ??????
  • InitTree(T)
  • DestroyTree(T)
  • CreateTree(T,definition)
  • TreeEmpty(T)
  • TreeDepth(T)
  • Parent(T, e)
  • LeftChild(T,e)
  • Rightsibling(T,e)
  • InsertChild(T,p,i,C)
  • DeleteChild(T,p,i)
  • Traverse(T)

24
6.5.2 ?????????
  • 1??????
  • Const MAX_TREE_SIZE100
  • typedef struct PTNode
  • Elem data
  • int parent
  • PTNode
  • typedef struct
  • PTNode nodesMAX_TREE_SIZE
  • int r,n
  • PTree
  • ??????????? r0 n11

25
data parent
0 A -1
1 B 0
2 C 0
3 D 0
4 E 1
5 F 1
6 G 1
7 H 3
8 I 3
9 J 5
10 K 5
26
  • 2??????
  • 1)??????
  • typedef struct CTNode
  • int child
  • struct CTNode next
  • CTNode,Childptr
  • typedef struct
  • Elem data
  • Childptr firstchild
  • CTBox
  • typedef struct
  • CTBox nodesMAX_TREE_SIZE
  • int n,r
  • CTree
  • ????????,r0,n11

27
data pare
0 A
1 B
2 C
3 D
4 E
5 F
6 G
7 H
8 I
9 J
10 K




28
  • 2)?????????
  • typedef struct TNode
  • Elem data
  • struct TNode childMAX_NODE
  • TNode,Tree
  • 3????????
  • ???????????????
  • 4???????(??-??)???
  • typedef struct CSNode
  • Elem data
  • struct CSNode firstchild,nextsibling
  • CSNode,CSTree

29
A
B
C
E
D
F
G
J
H
I
K
30
  • ????????
  • ???????????
  • ?????????-??????-????,??????????-???
  • ???????(?????)??????????????-???????
  • ?????????????????

31
  • ?????????F(T1,T2.....Tn)-gt B
  • ???F?,????B??
  • ?????????????ROOT(T1)???????,??????????????
  • ????????????T2,T3......Tn????????B????
  • ?????????B -gt F(T1,T2......Tn)
  • ????B?,???F??
  • ??????????????????ROOT(T1),??????????T1???
  • ????????????T2,T3......Tn

32
6.5.3 ???????
  • ??????????(??)
  • ?????? ABCEHDFG
  • ?????? BHECFGDA
  • ?????? ABCDEFGH
  • ??????????(??)
  • ????(ABCDEFGHIJ) ?????
  • ?????????????
  • ?????????????????
  • ????????????????????
  • ????(BCDAFEHJIG) ?????
  • ?????????????????
  • ?????????????
  • ????????????????????

33
  • ?????????????????????????????
  • ???????????????????????????

34
  • ???????
  • int TreeDepth(CsTree T) ??
  • if(!T)return 0
  • h1TreeDepth(T-gtfirstchild)
  • h2TreeDepth(T-gtnextsibling)
  • return (h11gth2)?h11h2

35
  • ? ??????????????????
  • void OutPath(CSTree T,Stack S)
  • while(T)
  • Push(S,T-gtdata)
  • if(!T-gtfirstchild)StackTraverse(S)
  • else OutPath(T-gtfirstchild,S)
  • Pop(S,e)
  • TT-gtnextsibling
  • ?????????,??????
  • while????????????
  • ??????????

36
  • ? ????????????-????
  • void CreateTree(CSTree T)
  • TNULL
  • for(cingtgtfagtgtchch!cingtgtfagtgtch)
  • pnew CSNode
  • p-gtdatachp-gtfirstchildp-gtnextsiblingN
    ULL
  • EnQueue(Q,p)
  • if(fa)Tp
  • else
  • while(GetHead(Q,s) s-gtdata!fa)DeQueue(Q,s)
  • if(!s-gtfirstchild)s-gtfirstchildprp
  • elser-gtnextsiblingprp
  • //else
  • //for
  • //CreateTree

37
6.6????
  • 6.6.1??????
  • ?????
  • ???? ,???n/2?1????
  • ????,???????,????
  • typedef SqTable HeapType
  • void HeapAdjust(HeapType H,int s,int m)
  • ??H.rs..m????,???H.rs????????
  • void HeapSort(HeapType H)
  • ???n/2?1????,?H????
  • ?????????,???HeapAdjust??????
  • ???????,????????

38
  • void HeapAdjust(HeapType H,int s,int m)
  • //??H.rs..m????,???H.rs?????
  • rcH.rs
  • for(j2sjltmj2)
  • if(jltm H.rj.keyltH.rj1.key)j
  • if(rcgtH.rj.key)break
  • H.rsH.rjsj
  • //for
  • H.rsrc

39
???
  • void HeapSort(HeapType H)
  • for(iH.length/2igt0--i)
  • HeapAdjust(H,i,H.length)
  • wH.r1H.r1H.rH.lengthH.rH.lengthw
  • for(iH.length-1igt1--i) //i?2???????
  • HeapAdjust(H,1,i)
  • wH.r1H.r1H.riH.riw
  • //for
  • //HeapSort

40
6.6.2?????
  • ???????
  • ???,???????
  • ??????,??????????????????
  • ??????,?????????????????????
  • ?????????????
  • typedef TelemType RcdType
  • typedef KeyType int
  • void Insert_BST(BiTree T, TElemType e)
  • ?????????????

41
  • void Insert_BST(BiTree T, TElemType e)
  • snew BiTNode
  • s-gtdataes-gtlchilds-gtrchildNULL
  • if(!T)Ts //?????
  • else
  • pT
  • while(p)
  • if(e.keyltp-gtdata.key)fppp-gtlchild
  • else fppp-gtrchild
  • if(e.keyltf-gtdata.key)f-gtlchilds
  • else f-gtrchilds
  • //else

42
?????????
  • void BSTSort(SqTable L)
  • BiTreeNULL
  • for(i1iltL.lengthi)
  • Insert_BST(T,L.ri)
  • i1
  • InOrder(T,Output(T,L,i))
  • //BSTSort
  • Void Output(BiTree T,SqTable L,int i)
  • L.riT-gtdata

43
  • ?????????????
  • keytype getmax(BiTree T)
  • if(!T)
  • errormessage(NULL Tree,Cant get value!)
  • return -1
  • pT
  • while(p-gtrchild)pp-gtrchild
  • return p-gtdata.key

44
6.6.3???(huffman)?????
  • ??????????????????
  • ????????????
  • ????????????????????
  • ??????????????????????????????????
  • ????????WPLS?klk (????)
  • ?????(????)
  • n????????????,??WPL????????????

45
? ??????????
?? 059 6069 7079 8089 90100
?? 0.05 0.15 0.40 0.30 0.10
lt60
lt80
lt70
??
lt70
lt90
lt80
??
lt60
?
?
?
lt90
?
??
??
?
?
  • 10000?????,??????31500?,???22000?

46
  • ??????
  • 1)?????n???w1,w2......wn,??n???????FT1,T2.....
    .Tn,???????????Wi?????
  • 2)?F?????????????????????,????????,???????????????
    ???????
  • 3)?F???????,???F??????
  • 4)??2)?3)??F???????

47
  • ????????
  • typedef struct
  • int weight
  • int parent,lchild,rchild
  • HTNode
  • typedef HTNode HuffmanTree
  • ??????
  • void CreateHuffman(HuffmanTree HT, int w,int n)

48
  • ????
  • ???????????????????
  • ?????
  • ?n????????(??)??,????????,????????????
  • ? ?8???a?b?c?d?e?f?g?h,?????5?29?7?8?14?23?3
    ?11,???????
  • ????
  • void HuffmanCoding(HuffmanTree HT,HuffmanCode
    HC, int n)
Write a Comment
User Comments (0)
About PowerShow.com