Presentation is loading. Please wait.

Presentation is loading. Please wait.

  In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and.

Similar presentations


Presentation on theme: "  In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and."— Presentation transcript:

1

2   In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.  A recursive definition using just set theory notions is that a (non-empty) binary tree is a triple ( L, S, R ), where L and R are binary trees or the empty set and S is a singleton set. Definition

3   A rooted binary tree has a root node and every node has at most two children.treeroot node  A full binary tree (sometimes referred to as a proper or plane binary tree) is a tree in which every node in the tree has either 0 or 2 children.  A perfect binary tree is a binary tree in which all interior nodes have two children and all leaves have the same depth or same level. (This is ambiguously also called a complete binary tree.) An example of a perfect binary tree is the ancestry chart of a person to a given depth, as each person has exactly two biological parents (one mother and one father).ancestry chart Types of binary trees

4   In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2 h nodes at the last level h. An alternative definition is a perfect tree whose rightmost leaves (perhaps all) have been removed. Some authors use the term complete to refer instead to a perfect binary tree as defined above, in which case they call this type of tree an almost complete binary tree or nearly complete binary tree. A complete binary tree can be efficiently represented using an array. Types of binary trees

5   In the infinite complete binary tree, every node has two children (and so the set of levels is countably infinite). The set of all nodes is countably infinite, but the set of all infinite paths from the root is uncountable, having the cardinality of the continuum. These paths correspond by an order- preserving bijection to the points of the Cantor set, or (using the example of a Stern–Brocot tree) to the set of positive irrational numbers.countably infinitecardinality of the continuumbijectionCantor setStern–Brocot treeirrational numbers Types of binary trees

6   A balanced binary tree has the minimum possible maximum height for the leaf nodes, because for any given number of leaf nodes the leaf nodes are placed at the greatest height possible.maximum height  One common balanced tree structure is a binary tree structure in which the left and right subtrees of every node differ in height by no more than 1. One may also consider binary trees where no leaf is much farther away from the root than any other leaf. (Different balancing schemes allow different definitions of "much farther".) Types of binary trees

7   A degenerate (or pathological ) tree is where each parent node has only one associated child node. This means that performance-wise, the tree will behave like a linked list data structure.linked list Types of binary trees

8  Inserting a node 1. Declare and initialize necessary variables 2. Read the data item to be inserted in the tree say x. 3. Create a new node with its left and right pointers to null. 4. Assign data x to info field of new node. 5. If (tree == NULL) then tree = address of new node else if (x info) if(tree->left == NULL) then tree->left = new node else tree = tree->left repeat step 5. else if(x>tree->info) if(tree->right == NULL) then tree->right = new node else tree = tree->right repeat step 5 else if(x == tree->info) print "Duplicated data" and exit 6. For next insertion, goto step 5.

9  Insertion Code struct TreeNode { int value; TreeNode* left; TreeNode* right; }; void insert (TreeNode* tree, int number) { if (tree == NULL) { tree = new TreeNode; tree->left = NULL; tree->right = NULL; tree->value = number; } else if (number value) { insert(tree->left, number); } else { insert(tree->right, number); }

10  Deletion of node 1. Declare and initialize necessary variable 2. Read data to be deleted say x. 3. Find the node where data is exist. if node is not found print "node not found" and exit 4. If the node has no subtree * then assign the father node's pointer with null that points to the node * delete the node that contains data item. 5. If the node has to be deleted has only one subtree * move its son up, to take its place * delete the node that contains data item 6. If the node has to be deleted has two subtrees * set the information of node to be deleted by the information of leftmost node of rightmost subtree of the node to be deleted 7. For next deletion, goto step 2

11  Deletion Code void TreeType::Delete(int key) { tree *temp = root,*parent = root, *marker; if(temp==NULL) cout<<"The tree is empty"<<endl; else{ while(temp!=NULL && temp- >info!=key) { parent=temp; if(temp->info<key){ temp=temp->Right; }else{ temp=temp->Left; } marker=temp; if(temp==NULL) cout<<"No node present"; else if(temp==root){ if(temp->Right==NULL && temp- >Left==NULL){ root=NULL; } else if(temp->Left==NULL){ root=temp->Right; } else if(temp->Right==NULL){ root=temp->Left; } else{ tree *temp1; temp1 = temp->Right; while(temp1->Left!=NULL){ temp=temp1; temp1=temp1->Left; }

12  Deletion Code if(temp1!=temp->Right){ temp->Left=temp1->Right; temp1->Right=root->Right; } temp1->Left=root->Left; root=temp1; } else{ if(temp->Right==NULL && temp- >Left==NULL){ if(parent->Right==temp) parent->Right=NULL; else parent->Left=NULL; } else if(temp->Left==NULL){ if(parent->Right==temp) parent->Right=temp->Right; else parent->Left=temp->Right; } else if(temp->Right==NULL){ if(parent->Right==temp) parent->Right=temp->Left; else parent->Left=temp->Left; }else{ tree *temp1; parent=temp; temp1=temp->Right; while(temp1->Left!=NULL){ parent=temp1; temp1=temp1->Left; } if(temp1!=temp->Right){ temp->Left=temp1->Right; temp1->Right=parent->Right; } temp1->Left=parent->Left; parent=temp1; } delete marker;


Download ppt "  In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and."

Similar presentations


Ads by Google