Click here to load reader

فصل پنجم : درخت

  • View
    68

  • Download
    6

Embed Size (px)

DESCRIPTION

فصل پنجم : درخت. آشنايي با درخت درخت هاي دودويي پيمايش درختان هرم جنگل. اهداف. فصل پنجم : درختان. درخت. ساختار درختي يعني مجموعه داده هاي سازماندهي شده اي که عناصر اطلاعاتي شان به وسيله انشعاباتي با هم رابطه داشته باشند. مفهوم درخت. - PowerPoint PPT Presentation

Text of فصل پنجم : درخت

  • *

  • .*

  • :

    .

    n 0 . .*

  • . . () ( )

    *

  • A B C D . B C D . A = 31-5*

  • : .

    : . .

    : .

    *

  • . 1-5 :

    (A(B(E(K L)F)C(G)D(H(M)IJ)))*

  • ( ) : . .*

  • k k .*

  • T k n n(k-1)+1 nk ( ) 0 (NULL) .

    : n n-1 . n k nk . : NULL : nk-(n-1)=n(k-1)+1 *

  • .) - ) 2.*

  • .

  • *

  • *

  • * . . . . :

  • structure Binary_tree (abbreviated BinTree ) is objects : a finite set of nodes either empty or consisting of a root node left Binary_tree and right Binary_tree. functions :for all bt bt1 bt2 BinTree item elementBinTree Create()Boolean IsEmpty (bt)BinTree MakeBT(bt1 item bt 2)BinTree Lchild(bt)element Data(bt)BinTree Rchild(bt)*

  • .

    .*

  • i 2i-1 .

    k 2k-1 . *

  • 2*

  • k 2k-1 . n k k . n

  • :

    *

  • . 1 n .

  • *

  • n ( ) i n i 1 :(1) i1 i [i/2] . i=1 i .(2) 2in i 2i . 2i>n i .(3) 2i+1n i 2i+1 . 2i+1>n i

  • k k . *

  • ( ) . . .*

  • :left_child data right_child C :typedef struct node *tree_pointer ;typedef struct node {int data ;tree_pointer left_child right_child ;};*

  • class Tree;class TreeNode{friend class Tree;private:char data; TreeNode *LeftChild; TreeNode *RightChild;};class Tree{public: Tree(); Tree(const Tree& t);private: TreeNode *root;};*

  • . R V L ( ) : RLV RVL VRL VLR LRV LVR *

  • VLR LRV LVR . V L R preordcr postorder inorder .*

  • postorder .

    preorder .*

  • : A/B*C*D*+E *

  • . infix .*

  • void inorder (tree_pointer ptr )/* inorder tree traversal */{if (ptr) {inorder ( ptr -> left_child );printf ( % d ptr -> data );inorder (ptr -> right_child);}}*

  • preorder . . . .*

  • preorder :+ * * / A B C D E prefix .*

  • Vide preorder (tree_pointer ptr )/* preorder tree traversal */{if (ptr) {printf ( % d ptr -> data );preorder ( ptr -> left_child );preorder (ptr -> right_child);}}*

  • . .*

  • postorder :A B/ C* D* E + postfix .*

  • Void iter_pointer (tree_pointer node ){int top = -1 ; /* initialize stack */tree_pointer stack [MAX_STACK_SIZE] ;for ( ; ; ) {for (; node ; node = ->left_child)add ( &top node ); /* add to stack */node = delete (&top); /*delete from stack */if (! Node) break ; /* empty stack*/printf ( % d node-> data ) ;node = node -> right_child;}}*

  • 3-5 inorder inorder2 : n iter_inorder . n O(n) . O(n) .*

  • inorder preorder postorder . . . .*

  • 3-5 : + *E *D / C A B*

  • 1- 2- 3- Satisfiability*

  • . n + 1 2n . . .*

  • :

    ptr-> left_child inorder ptr . ptr-> right_child inorder ptr .

    *

  • . right_thread left_thread .*

  • *

  • ptr ptr->right_thread = TRUE ptr inorder ptr->right_child . ptr ptr ptr left_thread = TRUE .*

  • insucc inorder . inorder insucc .*

  • threaded_pointer insucc (threaded_pointer tree){/* find the inorder sucassor of tree in a threaded binary tree */threaded_pointer temp ;temp = tree -> right_child ;if (! Tree -> right_thread)while (! temp -> left_thread)temp = temp -> left_child ;return temp ;} insucc : inorder*

  • Void tinorder (threaded_pointer tree){/* traverse the threaded binary tree inorder */threaded_pointer temp = tree ;for ( ; ; ) {temp = insucc (temp) ;if (temp = tree ) break ;printf ( % 3c temp -> data ) ;}}*

  • . power point . .*

  • max tree .max heap max tree .min tree .min heap min tree .*

  • max heap min heap*

  • (heap)

    (heap)

    (heap) *

  • . ( ) .

    .*

  • *

  • - heap - heap .*

  • *

  • heap n log2(n) . while log2(n) . log2(n) .*

  • max heap heap . max heap*

  • *

  • .

    heap n O(n) . *

  • . : . . . .*

  • *

  • class BSTNode{friend class BST;private:BSTNode *left;int key;BSTNode *right;public:int GetKey();};

    class BST{public:BST();bool Insert(int key);BSTNode* Search(int key);void DeleteNode(BSTNode* pNode,bool bRight);private:BSTNode *root;}; . *

  • key . (root) . key : key key . key .*

  • h O(h) .*

  • BSTNode* BST::Search(int key){ return Search(root , key);}BSTNode* BST::Search(BSTNode* p, int key){if (!p)return 0; if(key ==pkey)return p;if(key
  • BSTNode* BST::Search(int key){BSTNode *p=root;while(p!=NULL){if(key < pkey)p = pleft;else if(key > pkey)p = pright;else return p;}return 0;}*

  • key . .*

  • 80 35*

  • num O(h) h . (1) . insert_node (h) . *

  • bool BST::Insert(int key){BSTNode *p=root;BSTNode *q;while(p!=NULL){q = p;if(key < pkey)p = pleft;else if(key > pkey)p = pright;else return false; //Error for duplicate key}p = new BSTNode (key);if (!root){root = p;return true;}if(key < qkey)qleft = p;elseqright = p;return true;}*

  • 35 NULL :*

  • .

    O(h) h .*

  • void DeleteNode (BSTNode* pNode,bool bRight){ BSTNode *CurNode = pNodeleft; if (bRight) CurNode = pNoderight; if( !CurNoderight && !CurNode left) { delete CurNode; return; } if(CurNoderight && !CurNode left) { if (bRight) pNoderight = CurNoderight; else pNodeleft = CurNoderight; delete CurNode; return; } if(!CurNoderight && CurNode left) { if (bRight) pNoderight = CurNodeleft; else pNodeleft = CurNodeleft; delete CurNode; return; } if(CurNoderight && CurNode left) { BSTNode *tmp = CurNodeleft; pNode = CurNode; bRight = false; while (tmpright) {pNode = tmp; bRight = true;tmp = tmpright; } CurNodekey = tmpkey; DeleteNode(pNode , bRight); }}*

  • *

  • k . key . (run) . n k . *

  • k k (k-runs) . k (k-runs) k-1 . k>2 .*

  • .*

  • run1run 2run 3run 4run 5run 8run 7run 6

  • .

    n .

    k (run) .

    *

  • n 0 . .*

  • : .*

  • *

  • B: n=0 . . *

  • postorder inorder preorder T F F .*

  • preorder T F preorder : F . F . preorder . F preorder .*

  • inorder T F inorder :

    F . inorder . . inorder . *

  • postorder . postorder F :

    1) F .2) F postorder .3) F postorder . 4) F *

  • 0 9 :

    *

  • .*

  • 2) find(i)( i ) : i *

  • *

  • Weighting Union(i,j ) : i j j i i j .*

  • Weighting . count . i count[i] . Count parent . Weighting union2 .*

  • ( ) : j i j .*

  • : n 1 n n+1 .*

  • n : . :*

    ******************************************************************************************************