Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 21 Trees. “ A useful data structure for many applications”

Similar presentations


Presentation on theme: "Lecture 21 Trees. “ A useful data structure for many applications”"— Presentation transcript:

1 Lecture 21 Trees

2 “ A useful data structure for many applications”

3 Applications Compiler design text processing (dictionary) searching algorithms DOM trees (browsers) cider like red I apple andwine

4 A tree is a set of nodes and a set of directed edges that connects pairs of nodes. A tree is a a Directed, Acyclic Graph (DAG) with the following properties - one vertex is distinguished as the root; no edges enter this vertex - every other vertex has exactly one entering edge Trees Ctd…

5 thus, there exists a unique path from the root to any vertex Every node can have up to two(in a binary tree) other nodes connected to it, which we call the children. – Left and right child Trees

6 Terminology parent, child, sibling - – immediate predecessor of a node is its parent; – immediate successor is a child ancestor, descendant - – if path from root to node a goes through node b, b is an ancestor of a; – conversely, a is a descendant of b

7 Path length – the number of edges that must be followed leaf node – a node that has no children Depth of a node – length of the path from root to the node height of a tree – number of nodes in the longest path from root to a leaf node Definitions

8 internal node - – not a leaf subtree at v - – treat v as if it were a root height of vertex v - – length of longest path from v to a leaf height of tree - – height of root depth of v - – length of path from root to v ordered tree - – children of each vertex are distinguished (ordered left to right) More Definitions

9 Binary Search Trees Binary Search Tree – An ordered tree in which the nodes can have 0, 1, 2 children Complete - – bottom row missing nodes only on right Full, complete - – all rows full, no missing nodes on any level (max nodes for height) – # nodes in full, complete tree of height k? – # leaves? – Conversely, height of full, complete tree with n nodes?

10 Consider the following definition of a node class Node { type data; Node left; Node right; Node(type n){data=n; left=NULL;right=NULL;} } data left right Implementation

11 Node root; root = new Node(10); Node nextnode = new Node(5); // make this node the left child of the root Root.left = nextnode; // create another node and make it the right node nextnode = new Node(20); Root.right = nextnode; Now let us build a tree according to the following criteria. Make the first number the root of the tree For every subsequent number, use the following criteria to determine its place in the tree – if the node is bigger, go right – if the node is smaller, go left Implementation

12 Open a file read a num Node root = new Node(num); while (!infile.eof()) { int num = infile.getInt(); Node nextnode = new Node(num); Node ptr = root; Node prev; while (ptr != null) { prev = ptr; if (num >= ptr.data) ptr= ptr.right; if (num < ptr.data) ptr= ptr.left; } if (num >= prev.data) pre.right = nextnode; else prev.left = nextnode; } Implementation

13 Recursive Insert void Insert(Node t, string val) // Note we pass our tree ptr in by ref { // since we need to modify our left right ptrs if (t == null) // within each node { t=new Node( val); return; } if (t.data == val) { cout << "Ignoring Dupe\n"; return; } if ( val < t.data ) Insert(t.left, val ); else Insert(t.right, val ); }

14 Tree Traversal Three methods of tree traversal – inorder left, root, right – preorder root, left, right – postorder left, right, root

15 Inorder Traversal void print(Node ptr) { if (ptr != NULL) { print(ptr.left); System.out.print(ptr.data); print(ptr.right); } How does this work? 8 6 12 4 14 7 10


Download ppt "Lecture 21 Trees. “ A useful data structure for many applications”"

Similar presentations


Ads by Google