Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Trees and Binary Search Trees

Similar presentations


Presentation on theme: "Binary Trees and Binary Search Trees"— Presentation transcript:

1 Binary Trees and Binary Search Trees

2 Binary Trees A binary tree is a tree.
Each node has one or two children. Examples are Arithmetic expression (7-8)* (3+9) Logical expressions (T and T) or (F and not T)

3 Binary Tree (node, left, right)

4 + We will make the tree / \ We will visit in * - (e.g. 3+4 as tree)
* - /\ /\ We will make the tree We will visit in (e.g. 3+4 as tree) Pre order In order Post order

5 A node class We will have a node class (for binary trees)
It will store some data It will have all the associated get/set methods

6 data attributes private String data;
// the data we are storing at the node private Node left; // object reference to node at left. private Node right; // object reference to node at right.

7 methods public Node(String data) { // constructor
public String getNode() { // getter methods public void setNode(String data) { // setter method public Node getLeft() { // get the left child public void setLeft(Node left) { // set the left child public Node getRight() { // get the right child public void setRight(Node right) { // set the right child public boolean hasLeft() { // does the node have a left child public boolean hasRight() { // does the node have a right child public String toString() { // returns String containing data in node.

8 toString The purpose of a toString method is to represent the object as a string E.g. in a person class with name and age public String toString() { return “name ”+name+” age ”+ age } //this will give the “class attributes” as a single string

9 constructor public TreeBinaryNode(T data) { this.data = data; }

10 Get/set node public String getNode() { // getter methods return this.data; } public void setNode(String data) { // setter method this.data = data;

11 Get/set left public Node getLeft() { return this.left; }
public void setLeft(Node left) { this.left = left;

12 Get/set right public Node getRight() { return this.right; }
public void setRight(Node right) { this.right = right;

13 Has left/right public boolean hasLeft() { return left != null; }
public boolean hasRight() { return right != null;

14 toString() //returns a String containing the data in the node.
public String toString() { return this.getNode().toString(); }

15 preorder When displaying, 1/ display node first 2/ then the left tree
3/ then the right tree E.g

16 Pre-order public static void preorder(Node node) { if (node != null) {
System.out.print(node.getNode() + " "); preorder(node.getLeft()); preorder(node.getRight()); }

17 In order When displaying, 1/ then the left tree 2/ display node first
3/ then the right tree E.g

18 In-order public static void inorder(Node node) { if (node != null) {
inorder(node.getLeft()); System.out.print(node.getNode() + " "); inorder(node.getRight()); }

19 Post order When displaying, 1/ then the left tree
2/ then the right tree 3/ display node first E.g

20 Post-order public static void postorder(Node node) {
if (node != null) { postorder(node.getLeft()); postorder(node.getRight()); System.out.print(node.getNode() + " "); }

21 Test class We will make the tree + / \ * - /\ /\ 1 2 3 4
* - /\ /\ Two steps 1/ make nodes 2/ connect together

22 make nodes // set up some numbers - as STRINGS
TreeBinaryNode<String> one = new TreeBinaryNode<String>("1"); TreeBinaryNode<String> two = new TreeBinaryNode<String>("2"); TreeBinaryNode<String> three = new TreeBinaryNode<String>("3"); TreeBinaryNode<String> four = new TreeBinaryNode<String>("4");

23 make some more nodes // set up some arithmetic operators - as STRINGS
TreeBinaryNode<String> add = new TreeBinaryNode<String>("+"); TreeBinaryNode<String> mul = new TreeBinaryNode<String>("*"); TreeBinaryNode<String> sub = new TreeBinaryNode<String>("-");

24 Connect together We use setleft/right to connect add.setLeft(mul); add.setRight(sub); mul.setLeft(one); mul.setRight(two); sub.setLeft(three); sub.setRight(four);

25 printing We will now display the trees Pre order In order Post order
Level order

26 Mul pre/in/post order * 1 2 1 * 2 1 2 *

27 Add pre/in/post order + * 1 2 - 3 4 1 * 2 + 3 - 4 1 2 * 3 4 - +
+ *

28 Sub pre/in/post order - 3 4 3 - 4 3 4 -

29 Summary A binary tree has nodes with 0 1 or 2 children.
We can print in at least 3 ways – there are more. Use to model arithmetic and logic expressions. where a hierarchy exists We will now look at binary search trees.

30 Concept of Binary Search Tree

31 CompareTo method A compare to method public int compareTo(Node node)
Returns int Negative if before in ordering Zero – if equal order Positive if after in ordering Therefore using this we can order a set of item.

32 Additional method in Node class
//THIS IS A NEW METHOD public int compareTo(Node node) { return this.data.toString().compareTo(node.getNode().toString()); }

33 attributes private Node root; // The root Node of the tree private int size = 0; // A count of the Nodes in the tree

34 Size/clear/isEmpty // Returns the number of Nodes in the tree. public int size() { return size;} // Empties the tree public void clear() { this.root = null; size = 0;} // Determines if the tree is empty or not. public boolean isEmpty() { return (root == null);}

35 add a node public void addNode(Node nodeToInsert) { // If tree is empty, make our new Node the root if (root == null) { root = nodeToInsert; size = 1; } else { // recursive calls to add the descendants of Node addNode(nodeToInsert, root);// PRIVATE METHOD size = size + 1; }

36 private void addNode(Node
// private adds a node private void addNode(Node nodeToInsert, Node currentNode) { if (nodeToInsert.compareTo(currentNode) < 0) { if (currentNode.hasLeft()) addNode(nodeToInsert, currentNode.getLeft()); else { currentNode.setLeft(nodeToInsert); return;} } else { if (currentNode.hasRight()) addNode(nodeToInsert, currentNode.getRight()); currentNode.setRight(nodeToInsert); return; }}}

37 // preOrder public String preOrder() {
StringBuffer stringBuffer = new StringBuffer(); if (root == null) return "Empty Tree!"; else preOrder(root, stringBuffer); return stringBuffer.toString(); }

38 // private preOrder private void preOrder(Node Node, StringBuffer stringBuffer) { //visit the Nodes and add contents to our String stringBuffer.append(node.getNode().toString() + ", "); // 1 Visit Node if (node.hasLeft()) preOrder(node.getLeft(), stringBuffer); // 2 Visit left if (node.hasRight()) preOrder(node.getRight(), stringBuffer); // 3 Visit right }

39 find public String find(String name) { return find(name, root); }
//find(name, root) is a private method – and looks for name from the current node.

40 3 choices The node we are looking for is either
1/ at the current node in the tree we are looking at OR It is to the left it is to the right. We can decide which branch to go down using compareTo

41 private String find(String name, Node node)
private String find(String name, Node node) { if (node == null) return null; int order = name.compareTo(node.getNode().toString()); if (order == 0) return node.getNode(); if (node.hasLeft() && order < 0) return find(name, node.getLeft()); if (node.hasRight() && order > 0) return find(name, node.getRight()); }

42 Example We will Make some nodes (containing letters)
Add the nodes to the tree. Print out the tree as we add the node to see where it adds them (i.e. does it maintain order)

43 make a tree to store some Nodes
TreeSearchBinary letters = new TreeSearchBinary(); // make some nodes Node a = new Node("a"); Node b = new Node("b"); Node c = new Node("c"); Node x = new Node("x"); Node y = new Node("y"); Node z = new Node("z");

44 connect nodes and print trees
letters.addNode(b); letters.addNode(c); letters.addNode(a); letters.addNode(z); letters.addNode(x); letters.addNode(y); //try this now

45 // connect nodes and print the trees
letters.addNode(b); letters.printTree(); b

46 letters.addNode(c); letters.printTree(); c b

47 letters.addNode(a); letters.printTree(); c b a

48 letters.addNode(z); letters.printTree(); z c b a

49 letters.addNode(x); letters.printTree(); z x c b a

50 letters.addNode(y); letters.printTree(); z y x c b a

51 System. out. println(letters. find("a")); System. out. println(letters
System.out.println(letters.find("a")); System.out.println(letters.find("q"));

52 System.out.println(letters.find("a"));
System.out.println(letters.find("q")); a null

53 Pre/in/post order System.out.println(letters.preOrder());
System.out.println(letters.inOrder()); System.out.println(letters.postOrder());

54 Pre/in/post order System.out.println(letters.preOrder());
System.out.println(letters.inOrder()); System.out.println(letters.postOrder()); b, a, c, z, x, y, a, b, c, x, y, z, a, y, x, z, c, b,

55 Summary Binary trees – one or two branches.
E.g. logical or arithmetic expression. Get/set methods data/left/right. Binary Search tree is a special case of a binary tree. Given the current node (according to compareTo) - all the left nodes are less than current node all the right nodes are greater than current node

56 Concept of Binary Search Tree


Download ppt "Binary Trees and Binary Search Trees"

Similar presentations


Ads by Google