1 / 32

Computer Science II

Computer Science II. University of Palestine Faculty of Engineering and Urban planning Software Engineering Department. Lecture 13 of. Mohammad Amin Kuhail M.Sc. (York, UK). Trees. Saturday, 17 November 2007. Trees. Outline. Binary Trees Data structures for representing Binary Trees.

alana-ochoa
Download Presentation

Computer Science II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science II University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Lecture 13 of Mohammad Amin KuhailM.Sc. (York, UK) Trees Saturday, 17 November 2007

  2. Trees Outline • Binary Trees • Data structures for representing Binary Trees

  3. Binary Trees Properties In a binary tree T, the number of external nodes is 1 more than the number of internal nodes 1

  4. Binary Trees Properties v w z

  5. Binary Trees Properties z

  6. Binary Trees Properties z

  7. Binary Trees Properties The number of external nodes in T is at least h+1 and at most 2^h 2

  8. Binary Trees Properties

  9. Binary Trees Properties • Level 0: At its simplest form, a binary tree contains one node, h=0, external nodes=h+1=1=2^0 • Level 1: Adding children to the root, will make h=1, external nodes=2, which is h+1=2^1 2

  10. Binary Trees Properties • Level 2: max number of nodes means every node of the previous level have two children, and this will mathematically lead to 2*2 at this level, which is 2^2=2^h. 2

  11. Binary Trees Properties • Level 2: min number of nodes means one node of the previous level has two children, so this only add one external nodes to the tree, and add one parent to it which increases the height by one, and increases the external nodes by one thus =h+1 2

  12. Binary Trees Properties The number of internal nodes is at least h and at most 2^h-1 3

  13. Binary Trees Properties The total number of nodes in T is at least 2h+1 and at most 2^(h+1) -1 4

  14. Binary Trees Properties The height of T is at least log(n+1)-1 and at most (n-1)/2 5

  15. Binary Trees ADT publicinterface BinaryTree extends SimpleTree{ public Position leftChild(Position v); public Position rightChild(Position v); publicvoid expandExternal(Position v); publicvoid removeAboveExternal(Position v); } All methods O(1)

  16. Traversals of a Binary Tree Preorder Traversal

  17. Traversals of a Binary Tree Preorder Traversal Algorithm binaryPreorder(T,v): perform the “visit” action for node v if v is an internal node then binaryPreorder(T,T.leftChild(v)) {recursively traverse left subtree} binaryPreorder(T,T.rightChild(v)) {recursively traverse right subtree}

  18. Traversals of a Binary Tree Preorder Traversal Example Algorithm clone(T,T`,v,v`): Input: A binary Tree T containing a node v and a binary tree T` containing an external node v` Output: An augmentation of T` so that the subtree rootes at v` is an exact copy of the subtree of T rooted at v if v is an internal node then expandExternal(v) v`v.element() clone(T,T`.T.leftChild(v),T`.leftChild(v`)) clone(T,T`,T.rightChild(v),T`.rightChild(v`))

  19. Traversals of a Binary Tree Postorder Traversal

  20. Traversals of a Binary Tree Postorder Traversal Algorithm binaryPostorder(T,v): if v is an internal node then binaryPostorder(T,T.leftChild(v)) {recursively traverse left subtree} binaryPostrder(T,T.rightChild(v)) {recursively traverse right subtree} perform the “visit” action for node v

  21. Traversals of a Binary Tree Postorder Traversal Example Algorithm evlauteExpression(T,v): if v is an internal node storing operator o then x  evaluateExpression(T,T.leftChild(v)) y  evaluateExpression(T,T.rightChild(v)) return x o y else return the value of the variable stored at v

  22. Traversals of a Binary Tree Postorder Traversal 3x4 + 6/7

  23. Traversals of a Binary Tree Inorder Traversal

  24. Traversals of a Binary Tree Inorder Traversal Algorithm binaryInorder(T,v): if v is an internal node then binaryInorder(T,T.leftChild(v)) {recursively traverse left subtree} perform the “visit” action for node v if v is an internal node then binaryPostrder(T,T.rightChild(v)) {recursively traverse right subtree}

  25. Euler Tour of a Binary Tree Properties Generic Traversal of Binary Tree. Includes the preorder, postorder, and inorder traversals. Walk around the tree and visit each node three times: • On the left (preorder) • On the right (postorder) • On the bellow (Inorder)

  26. Euler Tour of a Binary Tree Algorithm Algorithm eulorTour(T,v): perform the “visit” action for node v on the left if v is an internal node then eulorTour(T,T.leftChild(v)) {recursively traverse left subtree} perform the “visit” action for node v from below if v is an internal node then eulorTour(T,T.rightChild(v)) {recursively traverse right subtree} perform the “visit” action for node v

  27. Euler Tour of a Binary Tree Applications • Can be used to implement preorder, postorder, and inorder traversals.

  28. Euler Tour of a Binary Tree Applications public void printExpression(BinaryTree T, Position v){ if(T.isExternal(v)) System.out.print(v.element()); else{ System.out.print(“(“); printExpression(T,T.leftChild(v)); //recursive call System.out.print(v.element()); printExpression(T,T.rightChild(v)); //recursive call System.out.print(“)”); } }

  29. Euler Tour of a Binary Tree Applications ( (3x4) + (6/7) )

  30. Euler Tour of a Binary Tree Applications ( (3x4) + (6/7) )

  31. Data structures for representing Trees A Sequence based structure for Binary Trees Let p(v) be the integer defined as follows: • If v is the root of T, the p(v)=1 • If v is the left child of node u, then p(v)=2p(u) • If v is the right child of node u, then p(v)=2p(u)+1 0 1 2 3 …..

  32. Data structures for representing Trees A Sequence based structure for Binary Trees

More Related