Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Trees. Node structure Data A data field and two pointers, left and right.

Similar presentations


Presentation on theme: "Binary Trees. Node structure Data A data field and two pointers, left and right."— Presentation transcript:

1 Binary Trees

2 Node structure Data A data field and two pointers, left and right

3 Binary tree structure Binary tree: A tree in which each node has at most two children. Tree: A non-linear data structure (representing a strictly hierarchical system of data) where each data item is thought of as a node. Strictly binary tree: A tree in which each node has precisely two children.

4 Recursive! All the nodes together form a binary tree. The nodes within the red triangle also form a binary tree. The nodes within the green triangle also form a binary tree. How may binary trees are there in total?

5 Binary search tree 50 7625 12 37 65 89 5972 83 95 617 32 41 Binary search tree: A binary tree that has the following properties: The left subtree of a node contains only nodes with data less than the node's data. The right subtree of a node contains only nodes with data greater than the node's data. Both the left and right subtrees are also binary search trees. Binary search tree: A binary tree that has the following properties: The left subtree of a node contains only nodes with data less than the node's data. The right subtree of a node contains only nodes with data greater than the node's data. Both the left and right subtrees are also binary search trees.

6 Terminology 50 7625 12 37 65 89 5972 83 95 617 32 41 root node Parent of the node that contains 12 Left child of the node that contains 25 Leaf nodes Right subtree of the tree of which the 76 node is the root

7 Binary trees don’t have to be symmetrical

8 Insertion operation 50 7625 12 37 65 89 5972 83 95 617 32 41 We want to insert a new node into the tree. Where should we put it? 63

9 Insertion operation 50 7625 12 37 65 89 5972 83 95 617 32 41 It is bigger than 50, so go down the right subtree… 63

10 Insertion operation 50 7625 12 37 65 89 5972 83 95 617 32 41 It is smaller than 76, so go down the left subtree… 63

11 Insertion operation 50 7625 12 37 65 89 5972 83 95 617 32 41 It is smaller than 65, so go down the left subtree… 63

12 Insertion operation 50 7625 12 37 65 89 5972 83 95 617 32 41 It is bigger than 59, and 59 has no right children, so that’s where it goes. 63

13 Task 50 7625 12 37 65 89 5972 83 95 617 32 41 Think about how you would code the insert operation… 63

14 The iterative way Don’t think about this too long because there is a beautifully elegant alternative… 50 7625 12 37 65 89 5972 83 95 617 32 41 63 private Node root; public void insert(int data) { if (root == null) { root = new Node(data); return; } Node tmp = root; while (true) { if (data == root.getData()) { // Data is already in the tree return; } else if (data < root.getData()) { // insert left if (root.getLeft() == null) { root.setLeft(new Node(data)); return; } else { tmp = root.getLeft(); } } else { // insert right if (root.getRight() == null) { root.setRight(new Node(data)); return; } else { tmp = root.getRight(); }

15 The recursive way Recursion is sometimes tough to get your head round. But can be very simple and very elegant. 50 7625 12 37 65 89 5972 83 95 617 32 41 63 void insertNode(Node root, int data) { if (root == NULL) root = new Node(data); else if (data < root.getData()) insertNode(root.getLeft(), data); else insertNode(root.getRight(), data); } You will be expected to be able to code this type of method from scratch for your exam

16 Binary Tree Traversals "Traversing" a binary tree means visiting every node in turn. 50 7625 12 37 65 89 5972 83 95 617 32 41 There are three types of traversal: Preorder Inorder Postorder There are three types of traversal: Preorder Inorder Postorder

17 Binary Tree Traversals Let's say that you want to print out your binary tree. Here are the three different traversals. Preorder: When you get to a node: Print the node's data. Then traverse its left subtree. Then traverse its right subtree. Preorder: When you get to a node: Print the node's data. Then traverse its left subtree. Then traverse its right subtree. Inorder: When you get to a node: Traverse its left subtree. Then print the node's data. Then traverse its right subtree. Inorder: When you get to a node: Traverse its left subtree. Then print the node's data. Then traverse its right subtree. Postorder: When you get to a node: Traverse its left subtree. Then traverse its right subtree. Then print the node's data. Postorder: When you get to a node: Traverse its left subtree. Then traverse its right subtree. Then print the node's data. void preOrder(Node n){ if(n == null) return; print(n); preOrder(n.left); preOrder(n.right); } void inOrder(Node n){ if(n == null) return; inOrder(n.left); print(n); inOrder(n.right); } void inOrder(Node n){ if(n == null) return; inOrder(n.left); print(n); inOrder(n.right); } void postOrder(Node n){ if(n == null) return; postOrder(n.left); postOrder(n.right); print(n); } void postOrder(Node n){ if(n == null) return; postOrder(n.left); postOrder(n.right); print(n); } Which of these would you want to use to print your binary search tree in ascending order?

18 Task Write down the order of the numbers as printed by each of the traversals. 50 7625 12 37 65 89 5972 83 95 617 32 41 void preOrder(Node n){ if(n == null) return; print(n); preOrder(n.left); preOrder(n.right); } void inOrder(Node n){ if(n == null) return; inOrder(n.left); print(n); inOrder(n.right); } void postOrder(Node n){ if(n == null) return; postOrder(n.left); postOrder(n.right); print(n); }

19 Infix, prefix and postfix notation Consider the following mathematical expression: 4  (3 + 8) This is written in what is called infix notation, in which the operators (+, -, x, /) are written between their operands. It is the usual notation that you are familiar with from mathematics. Consider the following mathematical expression: 4  (3 + 8) This is written in what is called infix notation, in which the operators (+, -, x, /) are written between their operands. It is the usual notation that you are familiar with from mathematics. However, there are two other ways of writing expressions like these, and both of them are used in computer science. In prefix notation, also known as Polish notation, the operators come before their operands. In postfix notation, also known as Reverse Polish notation, the operators come after their operands. (All operators are assumed to be binary.) However, there are two other ways of writing expressions like these, and both of them are used in computer science. In prefix notation, also known as Polish notation, the operators come before their operands. In postfix notation, also known as Reverse Polish notation, the operators come after their operands. (All operators are assumed to be binary.)

20 Examples Infix: 4  (3 + 8) Convert the following expressions to prefix and postfix: 1.4 + 5 2.4 / 3 + 7 3.(5 + 2)  (3 + 1) 4.4 / (3 + 6) + 5  9 Convert the following expressions to prefix and postfix: 1.4 + 5 2.4 / 3 + 7 3.(5 + 2)  (3 + 1) 4.4 / (3 + 6) + 5  9 Prefix:  4 + 3 8 Postfix: 4 3 8 + 

21 Why use different notations? Computers normally convert all infix expressions to postfix. Reasons: Brackets are not necessary in postfix expressions There are no rules about precedence in postfix expressions, as there are in infix expressions Postfix expressions are easy to evaluate using stacks Stacks are fast and easy to program Computers normally convert all infix expressions to postfix. Reasons: Brackets are not necessary in postfix expressions There are no rules about precedence in postfix expressions, as there are in infix expressions Postfix expressions are easy to evaluate using stacks Stacks are fast and easy to program

22 What has this got to do with binary trees? What happens when the following tree is printed: preorder inorder postorder What happens when the following tree is printed: preorder inorder postorder

23 Using stacks to evaluate postfix expressions When we see an operand, push it onto the stack When we see an operator, pop two operands from the stack, do the operation, and push the answer onto the stack Loop If you follow this algorithm, you will be left with one value on the stack, which is the answer to the expression. When we see an operand, push it onto the stack When we see an operator, pop two operands from the stack, do the operation, and push the answer onto the stack Loop If you follow this algorithm, you will be left with one value on the stack, which is the answer to the expression.

24 Task Create annotated notes in Word, using drawing objects. Show how 5 x (4 + 3) is converted to postfix notation. Show how the postfix expression is evaluated using a stack. Create annotated notes in Word, using drawing objects. Show how 5 x (4 + 3) is converted to postfix notation. Show how the postfix expression is evaluated using a stack.

25 Task answer Parse expression 5  (4 + 3) 3 Convert to postfix: 543+  Push 5 Push 4 Push 3 (diagram 1) 4 5 Pop 3 Pop 4 Push 3 + 4 = 7 (diagram 2) Pop 7 Pop 5 7 5 Push 7  5 = 35(diagram 3) 35 1 2 3

26 Past exam questions

27 Past exam questions (cont.)


Download ppt "Binary Trees. Node structure Data A data field and two pointers, left and right."

Similar presentations


Ads by Google