1.23k likes | 1.37k Views
Data Structures and Algorithms Introduction to Binary and Binary Search Trees Prepared by: S. Kondakci. Tree.
E N D
Data Structures and Algorithms Introduction to Binary and Binary Search Trees Prepared by: S. Kondakci
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.
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
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.
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
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; };
a b c d e g h i l f j k Picture of a binary tree
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
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.
Inorder Traversal (LVR) • The node’s left subtree is traversed. • The node’s data is processed. • The node’s right subtree is traversed.
Preorder Traversal (VLR) • The node’s data is processed. • The node’s left subtree is traversed. • The node’s right subtree is traversed.
Postorder Traversal (LRV) • The node’s left subtree is traversed. • The node’s right subtree is traversed. • The node’s data is processed.
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);}
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);}
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); • }
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”
Inorder Search Through Binary Tree(LVR) 12 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 14 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 14 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 14 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 14 75 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 14 75 23 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 14 75 23 12 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 14 75 23 12 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 14 75 23 12 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 14 75 23 12 35 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 14 75 23 12 35 34 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 14 75 23 12 35 34 34 14 33 23 35 55 89 75
Inorder Search Through Binary Tree 12 89 33 14 75 23 12 35 34 55 34 14 33 23 35 55 89 75
Preorder Search Through Binary Tree(VLR) 12 12 34 14 33 23 35 55 89 75
Preorder Search Through Binary Tree 12 12 14 34 14 33 23 35 55 89 75
Preorder Search Through Binary Tree 12 12 14 33 34 14 33 23 35 55 89 75
Preorder Search Through Binary Tree 12 12 14 33 89 34 14 33 23 35 55 89 75
Preorder Search Through Binary Tree 12 12 14 33 89 23 34 14 33 23 35 55 89 75
Preorder Search Through Binary Tree 12 12 14 33 89 23 75 34 14 33 23 35 55 89 75
Preorder Search Through Binary Tree 12 12 14 33 89 23 75 34 35 34 14 33 23 35 55 89 75
Preorder Search Through Binary Tree 12 12 14 33 89 23 75 34 35 55 34 14 33 23 35 55 89 75
Postorder Search Through Binary Tree(LRV) 12 34 14 33 23 35 55 89 75
Postorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75
Postorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75
Postorder Search Through Binary Tree 12 34 14 33 23 35 55 89 75
Postorder Search Through Binary Tree 12 89 34 14 33 23 35 55 89 75
Postorder Search Through Binary Tree 12 89 33 34 14 33 23 35 55 89 75
Postorder Search Through Binary Tree 12 89 33 34 14 33 23 35 55 89 75