Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.

Similar presentations


Presentation on theme: "Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node."— Presentation transcript:

1 Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node {char symb; Node *l, *r; }; typedef Node *Tree; Example. Arithmetic expression with binary operators. (2*b) + (y-z/w) + * - / 2by zw l r symb

2 Terminology and Properties root parent right child left child internal nodes leaf or external node subtree An edge is a parent-child pair. The size of a tree is the number of nodes. A path is a sequence of contiguous edges. The length of a path is the number of edges (1) Every node has 1 parent, except the root, which has no parent (2) The number of edges is 1 less than the size of the tree. (3) For every node  there is a unique path from the root to . The length of that path is the depth of .

3 Traversal There is no unique best sequence to visit the nodes in a tree. PreOrder: 1. root, 2. left subtree, 3. right subtree. InOrder: 1. left subtree, 2. root, 3. right subtree. PostOrder: 1. left subtree, 2. right subtree, 3. root. Example: PreOrder: + * 2 b - y / z w (Polish or prefix notation) InOrder: 2 * b + y - z / w (symmetric or infix notation) PostOrder: 2 b * y z w / - + (Reverse polish or postfix) void PreOrder(Tree t) { if (t != NULL) { printf(“%c”, t->symb); PreOrder(t->l); PreOrder(t->r); } + * - / 2by zw

4 void InOrder(Tree t) { if (t != NULL) { InOrder(t->l); printf(“%c”, t->symb); InOrder(t->r); } void PostOrder(Tree t) { if (t != NULL) { PostOrder(t->l); PostOrder(t->r); printf(“%c”, t->symb); }

5 Measuring Trees Properties of trees can often be computed in a traversal. int Size (Tree t) { if (t == NULL) return 0; else return (Size(t->l)+Size(t->r)+1); } The height of a tree is the maximum depth d(  ) of any node . int Height(Tree t) { if (t == NULL) return -1; else return Max(Height(t->l),Height(t->r))+1; }

6 The (internal) path length is the sum of all depths. int PathLength(Tree t) { if (t == NULL) return 0; else return PathLength(t->l) + PathLength(t->r) + Size(t) - 1; } Print a tree sideways: void PrintTree(Tree t, int indent) { if (t != NULL) { PrintTree(t->r, indent + 1); for(i=0; i<indent; i++) printf(“ “); printf(“%c\n”, t->symb); PrintTree(t->l, indent + 1); }

7 Binary Search Trees Sorted Trees A Binary tree is sorted if its in-order sequence is sorted. A binary search tree is a sorted binary tree. Example. m k s x b l r y b k l m r s x y

8 Searching We can search by tracing a path down from the root. Node *Search (Tree t, char x) { if (t != NULL) { if (x symb) return Search(t->l, x); else if (x > t->symb) return Search(t->r, x); else { assert(x == t->symb); return t; } else { assert (t == NULL); return NULL; }

9 Running Time In the worst case, the tree is a linked list: A search is like linear search that takes time O(n), n=size(t). In the best case the tree is perfectly balanced: height = log 2 n. Search in tree is like binary search in a sorted array: takes time O(log 2 n) m s x y height=n-1 m g s w c k p adilnrtz

10 Insertion Idea is simple: follow path from root and add new node to last node on the path. Path is stored on recursion stack. Example. (animated) m k s x b l r y Insert v: 1. Follow path 2. Create new node storing v. v 3. Make node child of node x. Done.

11 Deletion. The main idea is same: Follow path to node that contains key to be deleted. The details are more complicated because there are several cases: Case 1: x Node x has no children, delete it. (1 step animation). Case 2: x Node x has no right child. Make child of node x child of parent. (1 step) Case 3: Symmetric to case 2. (1 step) x continued on next slide

12 Deletion ( continued). Case 4. Node x has 2 children.(animated) x Find rightmost node in left subtree of x. xx u Replace x by u and delete u. (u has at most 1 child) x u u Done

13 /* Insert ******************************************************/ /* adds x to the binary search tree t, unless x is already in */ /* t in which case x is not added. */ /***************************************************************/ Tree Insert (Tree t, char x) { Node *nu; if (t != NULL) { if (x symb) t->l = Insert (t->l, x); else if (x > t->symb) t->r = Insert (t->r, x); else { assert (x == t->symb); printf ("%c already in tree.", x); } return t; } else { assert (t == NULL); nu = Fetch (); nu->symb = x; nu->l = nu->r = NULL; printf ("%c added in new node %d.", x, nu); return nu; }

14 /* Delete ***************************************************************/ /* removes character x from binary search tree t, if x is stored there. */ Tree Delete (Tree t, char x) { Node *mu; if (t == NULL) { printf ("%c not in tree.", x); return NULL; } else { assert (t != NULL); if (x symb) t->l = Delete (t->l, x); else if (x > t->symb) t->r = Delete (t->r, x); else { assert (x == t->symb); printf ("%c in node %d. ", x, t); if (t->l == NULL) { mu = t->r; Free (t); return mu; } else if (t->r == NULL) { mu = t->l; Free (t); return mu; } else { assert (t->l != NULL && t->r != NULL); mu = t->l; while (mu->r != NULL) mu = mu->r; t->symb = mu-> symb; t->l = Delete (t->l, t->symb); } return t; }


Download ppt "Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node."

Similar presentations


Ads by Google