1 / 123

Data Structures and Algorithms Introduction to Binary and Binary Search Trees

Data Structures and Algorithms Introduction to Binary and Binary Search Trees Prepared by: S. Kondakci. Tree.

Download Presentation

Data Structures and Algorithms Introduction to Binary and Binary Search Trees

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. Data Structures and Algorithms Introduction to Binary and Binary Search Trees Prepared by: S. Kondakci

  2. Tree A tree is a recursively structured set of branches, where each branchconsists of a collection of N nodes, one of which is the root and N-1 edges. The collection can be empty; otherwise, a tree consists of a distinguished node r, called the root, and zero or more non-empty (sub) trees T1, T2, …, Tk each of whose roots are connected by a directed edge from r.

  3. Tree terminology • The root of each subtree is said to be a child of r and r is said to be the parent of each subtree root. • Leaves: nodes with no children (also known as external nodes) • Internal Nodes: nodes with children • Siblings: nodes with the same parent

  4. Tree terminology (continued) • A path from node n1 to nk is defined as a sequence of nodes n1, n2, …, nk such that ni is the parent of ni+1 for 1<= i <= k. • The length of this path is the number of edges on the path namely k-1. • The length of the path from a node to itself is 0. • There is exactly one path from the root to each node.

  5. Tree terminology (continued) • Depth: the length of the unique path from the root to a node. • The depth of a tree is equal to the depth of its deepest leaf. • Height: the length of the longest path from a node to a leaf. • All leaves have a height of 0

  6. Implementation of Binary trees • A binary tree is a tree in which no node can have more than two children. • Each node has an element, a reference to a left child and a reference to a right child. struct TreeNode { intelement; TreeNode *left_child; TreeNode *right_child; };

  7. a b c d e g h i l f j k Picture of a binary tree

  8. A binary tree is defined recursively: it consists of a root, a left subtree, and a right subtree To traverse (or walk) the binary tree is to visit each node in the binary tree exactly once Tree traversals are naturally recursive Since a binary tree has three “parts,” there are six possible ways to traverse the binary tree: root, left, right left, root, right left, right, root root, right, left right, root, left right, left, root General Tree traversals

  9. Traversing the Tree • There are three common methods for traversing a binary tree and processing the value of each node: • inorder • preorder • postorder • Each of these methods is best implemented as a recursive function.

  10. Inorder Traversal (LVR) • The node’s left subtree is traversed. • The node’s data is processed. • The node’s right subtree is traversed.

  11. Preorder Traversal (VLR) • The node’s data is processed. • The node’s left subtree is traversed. • The node’s right subtree is traversed.

  12. Postorder Traversal (LRV) • The node’s left subtree is traversed. • The node’s right subtree is traversed. • The node’s data is processed.

  13. Inorder traversal (LVR) • In inorder, the root is visited in the middle • Here’s an inorder traversal to print out all the elements in the binary tree: • void inorderPrint(BinaryTree *bt) { if (bt == null) return; inorderPrint(bt -> leftChild); printf(“%d \n”,bt -> element);inorderPrint(bt -> rightChild);}

  14. Preorder traversal (VLR) • In preorder, the root is visited first • Here’s a C code implementing preorder traversal to print out all the elements in a binary tree: • void preorderPrint(BinaryTree *bt) { if (bt == null) return;printf(“%d \n”,bt -> element);preorderPrint(bt -> leftChild);preorderPrint(bt -> rightChild);}

  15. Postorder traversal (LRV) • In postorder, the root is visited last • Here’s a postorder traversal to print out all the elements in the binary tree: • void postorderPrint(BinaryTree bt) { if (bt == null) return; postorderPrint(bt.leftChild);postorderPrint(bt.rightChild); printf(“%d \n”,bt -> element); • }

  16. The order in which the nodes are visited during a tree traversal can be easily determined by imagining there is a “flag” attached to each node, as follows: To traverse the tree, collect the flags: Preorder (VLR) Inorder(LVR) Postorder (LRV) A A A B C B C B C D E F G D E F G D E F G A B D E C F G D B E A F C G D E B F G C A Tree traversals Illustration using “flags”

  17. Inorder Search Through Binary Tree(LVR) 12 34 14 33 23 35 55 89 75

  18. Inorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75

  19. Inorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75

  20. Inorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75

  21. Inorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75

  22. Inorder Search Through Binary Tree 12 89 34 14 33 23 35 55 89 75

  23. Inorder Search Through Binary Tree 12 89 33 34 14 33 23 35 55 89 75

  24. Inorder Search Through Binary Tree 12 89 33 14 34 14 33 23 35 55 89 75

  25. Inorder Search Through Binary Tree 12 89 33 14 34 14 33 23 35 55 89 75

  26. Inorder Search Through Binary Tree 12 89 33 14 34 14 33 23 35 55 89 75

  27. Inorder Search Through Binary Tree 12 89 33 14 75 34 14 33 23 35 55 89 75

  28. Inorder Search Through Binary Tree 12 89 33 14 75 23 34 14 33 23 35 55 89 75

  29. Inorder Search Through Binary Tree 12 89 33 14 75 23 12 34 14 33 23 35 55 89 75

  30. Inorder Search Through Binary Tree 12 89 33 14 75 23 12 34 14 33 23 35 55 89 75

  31. Inorder Search Through Binary Tree 12 89 33 14 75 23 12 34 14 33 23 35 55 89 75

  32. Inorder Search Through Binary Tree 12 89 33 14 75 23 12 35 34 14 33 23 35 55 89 75

  33. Inorder Search Through Binary Tree 12 89 33 14 75 23 12 35 34 34 14 33 23 35 55 89 75

  34. Inorder Search Through Binary Tree 12 89 33 14 75 23 12 35 34 34 14 33 23 35 55 89 75

  35. Inorder Search Through Binary Tree 12 89 33 14 75 23 12 35 34 55 34 14 33 23 35 55 89 75

  36. Preorder Search Through Binary Tree(VLR) 12 12 34 14 33 23 35 55 89 75

  37. Preorder Search Through Binary Tree 12 12 14 34 14 33 23 35 55 89 75

  38. Preorder Search Through Binary Tree 12 12 14 33 34 14 33 23 35 55 89 75

  39. Preorder Search Through Binary Tree 12 12 14 33 89 34 14 33 23 35 55 89 75

  40. Preorder Search Through Binary Tree 12 12 14 33 89 23 34 14 33 23 35 55 89 75

  41. Preorder Search Through Binary Tree 12 12 14 33 89 23 75 34 14 33 23 35 55 89 75

  42. Preorder Search Through Binary Tree 12 12 14 33 89 23 75 34 35 34 14 33 23 35 55 89 75

  43. Preorder Search Through Binary Tree 12 12 14 33 89 23 75 34 35 55 34 14 33 23 35 55 89 75

  44. Postorder Search Through Binary Tree(LRV) 12 34 14 33 23 35 55 89 75

  45. Postorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75

  46. Postorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75

  47. Postorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75

  48. Postorder Search Through Binary Tree 12 89 34 14 33 23 35 55 89 75

  49. Postorder Search Through Binary Tree 12 89 33 34 14 33 23 35 55 89 75

  50. Postorder Search Through Binary Tree 12 89 33 34 14 33 23 35 55 89 75

More Related