Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree.

Similar presentations


Presentation on theme: "Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree."— Presentation transcript:

1 Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree

2 What is a Binary Tree A binary tree is a a collection of nodes that consists of the root and other subsets to the root points, which are called the left and right subtrees. Every parent node on a binary tree can have up to two child nodes (roots of the two subtrees); any more children and it becomes a general tree. A node that has no children is called a leaf node.

3 A Few Terms Regarding Binary Trees A EF C B G D A is the root B and C are A’s children A is B’s parent B & C are the root of two subtrees D, E, and G are leaves

4 This is NOT A Binary Tree A EF C B GDH This is a general tree because C has three child nodes

5 This is NOT A Binary Tree A EF C B GD This is a graph because A, B, E, H, F and C form a circuit H

6 Tree Traversal There are three common ways to traverse a tree: –Preorder: Visit the root, traverse the left subtree (preorder) and then traverse the right subtree (preorder) –Postorder: Traverse the left subtree (postorder), traverse the right subtree (postorder) and then visit the root. –Inorder: Traverse the left subtree (in order), visit the root and the traverse the right subtree (in order).

7 Tree Traversals: An Example A EF C B G D Preorder: ABDECFG Postorder: DEBGFCA In Order: DBEAFGC

8 Tree Traversals: An Example A EF C B G D Preorder: A (visit each node as your reach it)

9 Tree Traversals: An Example A EF C B G D Preorder: AB (visit each node as your reach it)

10 Tree Traversals: An Example A EF C B G D Preorder: ABD (visit each node as your reach it)

11 Tree Traversals: An Example A EF C B G D Preorder: ABDE (visit each node as your reach it)

12 Tree Traversals: An Example A EF C B G D Preorder: ABDEC (visit each node as your reach it)

13 Tree Traversals: An Example A EF C B G D Preorder: ABDECF (visit each node as your reach it)

14 Tree Traversals: An Example A EF C B G D Preorder: ABDECFG (visit each node as your reach it)

15 Tree Traversals: An Example A EF C B G D Postorder:

16 Tree Traversals: An Example A EF C B G D Postorder:

17 Tree Traversals: An Example A EF C B G D Postorder: D

18 Tree Traversals: An Example A EF C B G D Postorder: DE

19 Tree Traversals: An Example A EF C B G D Postorder: DEB

20 Tree Traversals: An Example A EF C B G D Postorder: DEB

21 Tree Traversals: An Example A EF C B G D Postorder: DEB

22 Tree Traversals: An Example A EF C B G D Postorder: DEBG

23 Tree Traversals: An Example A EF C B G D Postorder: DEBGF

24 Tree Traversals: An Example A EF C B G D Postorder: DEBGFC

25 Tree Traversals: An Example A EF C B G D Postorder: DEBGFCA

26 Tree Traversals: An Example A EF C B G D In Order:

27 Tree Traversals: An Example A EF C B G D In Order:

28 Tree Traversals: An Example A EF C B G D In Order: D

29 Tree Traversals: An Example A EF C B G D In Order: DB

30 Tree Traversals: An Example A EF C B G D In Order: DBE

31 Tree Traversals: An Example A EF C B G D In Order: DBEA

32 Tree Traversals: An Example A EF C B G D In Order: DBEA

33 Tree Traversals: An Example A EF C B G D In Order: DBEAF

34 Tree Traversals: An Example A EF C B G D In Order: DBEAFG

35 Tree Traversals: An Example A EF C B G D In Order: DBEAFGC

36 Tree Traversals: An Example A EF C B G D Preorder: ABDECFG Postorder: DEBGFCA In Order: DBEAFGC

37 Tree Traversals: Another Example A E F C B I D Preorder: ABDFHIECG Postorder: HIFDEBGCA In Order: DHFIBEACG G H

38 Tree Traversals: Another Example A E F C B I D Preorder: A G H

39 Tree Traversals: Another Example A E F C B I D Preorder: AB G H

40 Tree Traversals: Another Example A E F C B I D Preorder: ABD G H

41 Tree Traversals: Another Example A E F C B I D Preorder: ABDF G H

42 Tree Traversals: Another Example A E F C B I D Preorder: ABDFH G H

43 Tree Traversals: Another Example A E F C B I D Preorder: ABDFHI G H

44 Tree Traversals: Another Example A E F C B I D Preorder: ABDFHIE G H

45 Tree Traversals: Another Example A E F C B I D Preorder: ABDFHIEC G H

46 Tree Traversals: Another Example A E F C B I D Preorder: ABDFHIECG G H

47 Tree Traversals: Another Example A E F C B I D Postorder: G H

48 Tree Traversals: Another Example A E F C B I D Postorder: G H

49 Tree Traversals: Another Example A E F C B I D Postorder: G H

50 Tree Traversals: Another Example A E F C B I D Postorder: G H

51 Tree Traversals: Another Example A E F C B I D Postorder: H G H

52 Tree Traversals: Another Example A E F C B I D Postorder: HI G H

53 Tree Traversals: Another Example A E F C B I D Postorder: HIF G H

54 Tree Traversals: Another Example A E F C B I D Postorder: HIFD G H

55 Tree Traversals: Another Example A E F C B I D Postorder: HIFDE G H

56 Tree Traversals: Another Example A E F C B I D Postorder: HIFDEB G H

57 Tree Traversals: Another Example A E F C B I D Postorder: HIFDEB G H

58 Tree Traversals: Another Example A E F C B I D Postorder: HIFDEBG G H

59 Tree Traversals: Another Example A E F C B I D Postorder: HIFDEBGC G H

60 Tree Traversals: Another Example A E F C B I D Postorder: HIFDEBGCA G H

61 Tree Traversals: Another Example A E F C B I D In Order: G H

62 Tree Traversals: Another Example A E F C B I D In Order: G H

63 Tree Traversals: Another Example A E F C B I D In Order: D G H

64 Tree Traversals: Another Example A E F C B I D In Order: D G H

65 Tree Traversals: Another Example A E F C B I D In Order: DH G H

66 Tree Traversals: Another Example A E F C B I D In Order: DHF G H

67 Tree Traversals: Another Example A E F C B I D In Order: DHFI G H

68 Tree Traversals: Another Example A E F C B I D In Order: DHFIB G H

69 Tree Traversals: Another Example A E F C B I D In Order: DHFIBE G H

70 Tree Traversals: Another Example A E F C B I D In Order: DHFIBEA G H

71 Tree Traversals: Another Example A E F C B I D In Order: DHFIBEAC G H

72 Tree Traversals: Another Example A E F C B I D In Order: DHFIBEACG G H

73 Tree Traversals: Another Example A E F C B I D Preorder: ABDFHIECG Postorder: HIFDEBGCA In Order: DHFIBEACG G H

74 Basic Implementation of a Binary Tree We can implement a binary in essentially the same way as a linked list, except that there are two nodes that comes next: public class Node { private int data; private Node left; private Node right;

75 public int getData() { return data; } public Node getLeft() { return left; } public Node getRight() { return right; } public void setData(int x) { data = x; }

76 public void setLeft(Node p) { left = p; } public void setRight(Node p) { right = p; }

77 The tree Class public class Tree { private Node root; // tree() - The default constructor – Starts // the tree as empty public Tree() { root = null; } // Tree() - An initializing constructor that // creates a node and places in it // the initial value

78 public Tree(int x) { root = new Node(); root.setData(x); root.setLeft(null); root.setRight(null); } public Node getRoot() { return root; }

79 // newNode() - Creates a new node with a // zero as data by default public Node newNode() { Node p = new Node(); p.setData(0); p.setLeft(null); p.setRight(null); return(p); }

80 // newNode() - Creates a new node with the // parameter x as its value public Node newNode(int x) { Node p = new Node(); p.setData(x); p.setLeft(null); p.setRight(null); return(p); }

81 //travTree() – initializes recursive //traversal of tree public void travTree() { if (root != null) travSubtree(root); System.out.println(); } //travSubtree() – recursive method used to //traverse a binary tree (inorder) public void travSubtree(Node p) { if (p != null) { travSubtree(p.getLeft()); System.out.print(p.getData() + "\t"); travSubtree(p.getRight()); }

82 // addLeft() - Inserts a new node containing // x as the left child of p public void addLeft(Node p, int x) { Node q = newNode(x); p.setLeft(q); } // addRight() - Inserts a new node containing // x as the right child of p public void addRight(Node p, int x) { Node q = newNode(x); p.setRight(q); }

83 A Basic Search Tree We can construct a simple search tree if we add new nodes with value x on the tree using this strategy: –Every time x is less than the value in the node we move down to the left. –Every time x is greater than the value in the node we move down to the right.

84 A Sample Search Tree 15 1218 22 8 20 523 16107

85 // insert() -Insert value x in a new node to //be insertedafter p public voidinsert(int x) { Node p, q; booleanfound = false; p = root; q = null; while (p != null && !found){ q = p; if (p.getData() == x) found = true; else if (p.getData() > x) p = p.getLeft(); else p = p.getRight(); }

86 if (found) error("Duplicate entry"); if (q.getData() > x) addLeft(q, x); else addRight(q, x); //q = newNode(x); }

87 // isXThere() -Is there a node in the // tree containing x? public booleanisXThere(int x) { Node p; booleanfound = false; p = root; while (p != null && !found){ if (p.getData() == x) found = true; else if (p.getData() > x) p = p.getLeft(); else p = p.getRight(); } return(found); }

88 public voiderror(String message) { System.out.println(message); System.exit(0); }

89 // getNode() -Get the pointer for the // node containing x public NodegetNode(int x) { Node p, q; booleanfound = false; p = root; q = null; while (p != null && !found){ q = p; if (p.getData() == x) found = true; else if (p.getData() > x) p = p.getLeft(); else p = p.getRight(); }

90 if (found) return(q); else return(null); }

91 public class TestTree { public static void main(String[] args) { Treemytree = new Tree(8); mytree.addLeft(mytree.getRoot(), 6); mytree.addRight(mytree.getRoot(), 9); mytree.insert(4); mytree.insert(1); mytree.insert(12); if (mytree.isXThere(13)) System.out.println("great"); else System.out.println("not great, Bob"); mytree.travTree(); }

92 Tracing TestTree 8

93 8 6

94 8 9 6

95 8 9 6 4

96 8 9 6 4 1

97 8 9 6 4 12 1


Download ppt "Introduction to Algorithms and Data Structures Lecture 12 - “I think that I shall never see.. a data structure lovely as a” Binary Tree."

Similar presentations


Ads by Google