Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures – CSC212 (1) Dr Muhammad Hussain Lecture - Binary Tree ADT Binary Tree ADT It is an ordered tree with the following properties 1.Each node.

Similar presentations


Presentation on theme: "Data Structures – CSC212 (1) Dr Muhammad Hussain Lecture - Binary Tree ADT Binary Tree ADT It is an ordered tree with the following properties 1.Each node."— Presentation transcript:

1 Data Structures – CSC212 (1) Dr Muhammad Hussain Lecture - Binary Tree ADT Binary Tree ADT It is an ordered tree with the following properties 1.Each node can have at most two subtrees 2.Each subtree is identified as being either left subtree or the right subtree of its parent 3.It may be empty Note:  Property 1 says that each node can have maximum two children  The order between the children of a node is specified by labeling its children as left child and right child

2 Data Structures – CSC212 (2) Dr Muhammad Hussain Lecture - Binary Tree ADT Why binary Trees? 1.Expression Tree - A central data structure in compiler design - A central data structure in compiler design - Interior nodes contain operators and the leaf nodes have operands - An expression is evaluated by applying the operator at root to the values obtained by recursively evaluating the left and right subtrees - The following tree corresponds the expression: (a+((b-c)*d) + a * - d bc  Binary trees naturally arise in many different applications

3 Data Structures – CSC212 (3) Dr Muhammad Hussain Lecture - Binary Tree ADT 2.Huffman Coding Tree - Its is used in a simple but effective data compression algorithm - Its is used in a simple but effective data compression algorithm - In this tree, each symbol is stored at a leaf node - To generate the code of a symbol traverse the tree from the root to the leaf node that contains the symbol such that each left link corresponds to 0 and each right link corresponds to 1 - The following tree encodes the symbols a, b, c, d a d bc 10 1 0 10 The code of a is 0, and that of b is 100 Why binary Trees?

4 Data Structures – CSC212 (4) Dr Muhammad Hussain Lecture - Binary Tree ADT Recursive definition of Binary Tree A binary tree is - empty OR - a node, called the root node, together with two binary trees, which are disjoint from each other and the root node. These are called left and right subtrees of the root  Many routines can be efficiently implemented using recursive nature of the binary tree

5 Data Structures – CSC212 (5) Dr Muhammad Hussain Lecture - Binary Tree ADT Types of Binary Tree Full - Every node has exactly two children in all levels, except the last level. Nodes in last level have 0 children Complete - Full up to second last level and last level is filled from left to right Other - not full or complete

6 Data Structures – CSC212 (6) Dr Muhammad Hussain Lecture - Binary Tree ADT Full, Complete or Other? A CD FIE B RSP GH T ML O KJ Q N not binary

7 Data Structures – CSC212 (7) Dr Muhammad Hussain Lecture - Binary Tree ADT Full, Complete or other? A D FI B RSP GH T ML O K N

8 Data Structures – CSC212 (8) Dr Muhammad Hussain Lecture - Binary Tree ADT Full, Complete or other? A D F I B R S P G H T M L O K N

9 Data Structures – CSC212 (9) Dr Muhammad Hussain Lecture - Binary Tree ADT Full, Complete or other? A D F I B R S P G H T M L O K N

10 Data Structures – CSC212 (10) Dr Muhammad Hussain Lecture - Binary Tree ADT Full, Complete or other? A D F I B R S P G H T M L O K N

11 Data Structures – CSC212 (11) Dr Muhammad Hussain Lecture - Binary Tree ADT Full, Complete or Other? A D F I B Q S R G H T M L P K N O

12 Data Structures – CSC212 (12) Dr Muhammad Hussain Lecture - Binary Tree ADT Full, Complete or other? A D F I B Q S R G H T M L P K N O

13 Data Structures – CSC212 (13) Dr Muhammad Hussain Lecture - Binary Tree ADT Full, Complete or Other? A D F I B QR G H M L P K N O

14 Data Structures – CSC212 (14) Dr Muhammad Hussain Lecture - Binary Tree ADT Process To process a node means to perform some simple operation like printing the contents of the node or updating the contents of the node Traversal To traverse a finite collection of objects means to process each object in the collection exactly once Examples 1. List traversal – to process each element of list exactly once 2. Tree traversal – to process each node of tree exactly once Binary Tree Traversal

15 Data Structures – CSC212 (15) Dr Muhammad Hussain Lecture - Binary Tree ADT Traversal Of Binary Tree There are four methods for the traversal of a binary tree 1.Preorder Traversal Each node is processed before any node in either of its subtrees 2.Inorder Traversal Each node is processed after all nodes in its left subtree and before any node in its right subtree 3.Postorder Traversal Each node is processed after all nodes in both of its subtrees 4.Level order Traversal Each node at level l is processed before any node at level l+1

16 Data Structures – CSC212 (16) Dr Muhammad Hussain Lecture - Binary Tree ADT A D F I B R S P G H T M L O K N Preorder Traversals 1.Visit the root 2.Visit Left subtree 3.Visit Right subtree 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

17 Data Structures – CSC212 (17) Dr Muhammad Hussain Lecture - Binary Tree ADT Algorithm TraversePreorder(n) Process node n if n is an internal node then TraversePreorder( n -> leftChild) TraversePreorder( n -> rightChild)

18 Data Structures – CSC212 (18) Dr Muhammad Hussain Lecture - Binary Tree ADT A D F I B R S P G H T M L O K N 1 Inorder Traversals 1.Visit Left subtree 2.Visit the root 3.Visit Right subtree 1 3 4 5 7 8 9 10 11 12 13 14 15 16 1 1 6 2

19 Data Structures – CSC212 (19) Dr Muhammad Hussain Lecture - Binary Tree ADT Algorithm TraverseInorder(n) if n is an internal node then TraverseInorder( n -> leftChild) Process node n if n is an internal node then TraverseInorder( n -> rightChild)

20 Data Structures – CSC212 (20) Dr Muhammad Hussain Lecture - Binary Tree ADT A D F I B R S P G H T M L O K N Postorder Traversals 1.Visit Left subtree 2.Visit Right subtree 3.Visit the root 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2 2 3

21 Data Structures – CSC212 (21) Dr Muhammad Hussain Lecture - Binary Tree ADT Algorithm TraversePostorder(n) if n is an internal node then TraversePostorder( n -> leftChild) TraversePostorder( n -> rightChild) Process node n

22 Data Structures – CSC212 (22) Dr Muhammad Hussain Lecture - Binary Tree ADT B H M N A D F I B R S P G H T M L O K N Level Order Traversals 1.Visit the root 2.Visit root of Left subtree 3.Visit root of Right subtree 4.??? 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2 1718 19 20

23 Data Structures – CSC212 (23) Dr Muhammad Hussain Lecture - Binary Tree ADT Specification of Binary Tree ADT Elements: Any data type Structure: A binary tree either is empty OR a node, called the root node, together with two binary trees, which are disjoint from each other and the root node. These are called left and right subtrees of the root Domain: Number of elements is bounded Operations: OperationSpecification void empty() Precondition/Requires: none. Processing/Results: returns true if the binary tree (BT) has no nodes. void traverse(Order ord) Precondition/Requires: BT is not empty. Processing/Results: Traverses the binary tree according to the value of ord (1) ord = preOrder: traverses the tree using preorder traversal (2) ord = inOrder: traverses the tree using inorder traversal (3) ord = postOrder: traverses the tree using postorder traversal

24 Data Structures – CSC212 (24) Dr Muhammad Hussain Lecture - Binary Tree ADT OperationSpecification void find (Relative rel) Precondition/Requires: BT is not empty. Processing/Results: the current node of BT is determined by Relative and the current node prior to the operation as follows (always return true unless indicated so): (1) rel = Root: current = root (2) rel = Parent: if the current node has a parent then parent is the current node; otherwise returns false (3) rel = LeftChild: if the current node has a leftchild then it will be the current node; otherwise returns false (4) rel = RightChild: same as above but for rightchild. void insert(Relative rel, Type val) Precondition/Requires: either (1) BT is empty and rel = Root; or (2) BT not empty and rel  Root. Processing/Results: as follows: (1) rel = Root: create a root node with data = val. (2) rel = Parent: nonsense case. (3) rel = LeftChild: if current node does not have a leftchild then make one with data = val. (4) rel = RightChild: same as above but for rightchild. In all the above cases if the insertion was successful then it will be designated as current node and returns true, otherwise current remains unchanged and returns false.

25 Data Structures – CSC212 (25) Dr Muhammad Hussain Lecture - Binary Tree ADT OperationSpecification void update(Type)Precondition/Requires: BT is not empty. Processing/Results: update the value of data of the current node. Type retrieve()Precondition/Requires: BT is not empty. Processing/Results: returns data of the current node. void delete_sub()Precondition: BT is not empty. Process: the subtree whose root node was the current node before this operation is deleted from the tree. In case the resulting tree is not empty then current = root. Note : Relative is enumerated type and is confined to the values {Root, Parent, LeftChild, RightChild}

26 Data Structures – CSC212 (26) Dr Muhammad Hussain Lecture - Binary Tree ADT Represent ion of Binary Tree ADT A binary tree can represented using - Linked List - Array Note : Array is suitable only for full and complete binary trees

27 Data Structures – CSC212 (27) Dr Muhammad Hussain Lecture - Binary Tree ADT Linked List based Implementation public class BTNode public class BTNode { public T data; public T data; public BTNode left, right; public BTNode left, right; public BTNode(T val) public BTNode(T val) { data = val; data = val; left = right = null; left = right = null; } public BTNode(T val, BTNode l, BTNode r) public BTNode(T val, BTNode l, BTNode r) { data = val; data = val; left = l; left = l; right = r; right = r; }} Order public enum Order {preOrder, inOrder, postOrder};

28 Data Structures – CSC212 (28) Dr Muhammad Hussain Lecture - Binary Tree ADT public class BT { //Data Members BTNode root, current; BTNode root, current; public BT() public BT() // Private Methods private void preorder(BTNode p) private void preorder(BTNode p) private void inorder(BTNode p) private void postorder(BTNode p) private void postorder(BTNode p) private BTNode findparent (BTNode p) // non-recursive private BTNode findparent (BTNode p, BTNode t) // Operations public void traverse(Order ord) public boolean empty() public boolean find (Relative rel) public boolean find (Relative rel) public boolean insert (Relative rel, T val) public T retrieve () public void update (T val) public void delete_subtree() public void delete_subtree()} public enum Relative {Root, Parent, LeftChild, RightChild};

29 Data Structures – CSC212 (29) Dr Muhammad Hussain Lecture - Binary Tree ADT preorder private void preorder(BTNode p) { if (p != null){ System.out.println(p.data); preorder(p.left); preorder(p.right); } inorder private void inorder(BTNode p) { if (p != null){ inorder(p.left); System.out.println(p.data); inorder(p.right); } Implementation of Private Methods

30 Data Structures – CSC212 (30) Dr Muhammad Hussain Lecture - Binary Tree ADT private void postorder(BTNode p) { if (p != null){ postorder(p.left); postorder(p.right); System.out.println(p.data); } Implementation of Private Methods

31 Data Structures – CSC212 (31) Dr Muhammad Hussain Lecture - Binary Tree ADT Implementation of Private Methods /* Non-recursive version of findparent */ findparent private BTNode findparent (BTNode p) { LinkStack > stack = new LinkStack >(); BTNode q = root; while (q.right != p && q.left != p){ if (q.right != null) stack.push(q.right); if (q.left != null) q = q.left; else q = stack.pop(); } return q; }

32 Data Structures – CSC212 (32) Dr Muhammad Hussain Lecture - Binary Tree ADT Implementation of Private Methods /* Recursive version of findparent - uses pre-order traversal */ findparent private BTNode findparent (BTNode p, BTNode t) { if (t == null) return null; /* empty tree */ if (t.right == null && t.left == null) return null; else if (t.right == p || t.left == p) return t; /* parent is t */ else { BTNode q = findparent(p, t.left); if (q != null) return q; else return findparent(p, t.right); }

33 Data Structures – CSC212 (33) Dr Muhammad Hussain Lecture - Binary Tree ADT Implementation of Operations public void traverse(Order ord) { switch (ord) { case preOrder: preorder(root); break; case inOrder: inorder(root); break; case postOrder: postorder(root); break; default: break; } return; } public BT() { root = current = null; }

34 Data Structures – CSC212 (34) Dr Muhammad Hussain Lecture - Binary Tree ADT Implementation of Operations find public boolean find (Relative rel){ switch (rel) { case Root: current = root; return true; case Parent: if (current == root) return false; current = findparent(current, root); return true; case LeftChild: if (current.left == null) return false; current = current.left; return true; case RightChild: if (current.right == null) return false; current = current.right; return true; default: return false; }

35 Data Structures – CSC212 (35) Dr Muhammad Hussain Lecture - Binary Tree ADT Implementation of Operations insert public boolean insert (Relative rel, T val) { switch (rel) { case Root: if (! empty()) return false; current = root = new BTNode (val); return true; case Parent: return false; case LeftChild: if (current.left != null) return false; current.left = new BTNode (val); current = current.left; return true; case RightChild: if (current.right != null) return false; current.right = new BTNode (val); current = current.right; return true; default: return false; }

36 Data Structures – CSC212 (36) Dr Muhammad Hussain Lecture - Binary Tree ADT Implementation of Operations public T retrieve () { return current.data; } public void update (T val) { current.data = val; }

37 Data Structures – CSC212 (37) Dr Muhammad Hussain Lecture - Binary Tree ADT Implementation of Operations public void delete_subtree() { if (current == root){ current = root = null; } else { BTNode p = current; find(Relative.Parent); if (current.left == p) current.left = null; else current.right = null; current = root; }

38 Data Structures – CSC212 (38) Dr Muhammad Hussain Lecture - Binary Tree ADT Height and the number of nodes in a Binary Tree  If the height of a binary tree is h then the maximum number of nodes in the tree is 2 h -1  If the number of nodes in a complete binary tree is n, then 2 h - 1 = n 2 h = n + 1 O(logn) h = log(n+1)  O(logn)


Download ppt "Data Structures – CSC212 (1) Dr Muhammad Hussain Lecture - Binary Tree ADT Binary Tree ADT It is an ordered tree with the following properties 1.Each node."

Similar presentations


Ads by Google