Binary tree and linked list review

Source: Internet
Author: User

Review the list and the two-fork tree today based on the Stanford online course

The recursive definition for the binary tree above is very concise:

A binary tree is either empty (represented by a null pointer), or was made of a single node, where the left and RI ght pointers (recursive definition ahead) each point to a binary tree.

A branch of a binary tree: a two-fork search tree (TREE,BST), which is the value of all left nodes <= the middle node, the value of the right node > the middle node.

This is a binary search tree.

The advantage of BST is that it is easy to insert and find, and the algorithm complexity of inserting and finding in a number of n BST is O (LgN), similar to binary lookup.

The following is a two-fork tree described in Java, including the basic methods for inserting and finding. In addition, the object-oriented advantage is that the external shielding does not need to display the details, encapsulation.

//Binarytree.java Public classBinaryTree {//Root node pointer. 'll is null for the empty tree.   PrivateNode Root; /*--node--The binary tree is a built using this nested Node class.    Each node stores the one data element, and has a left and a right sub-tree pointer which could be null. The node is a "dumb" nested class – we just use it for storage;   It does not has any methods. */   Private Static classnode {node left;     Node right; intdata; Node (intNewData) { Left=NULL; Right=NULL; Data=NewData; }   }  /**creates an empty binary tree--a null root pointer. */    Public voidBinaryTree () {root=NULL; }    /**Returns True if the given target is in the binary tree.   Uses a recursive helper. */    Public BooleanLookupintdata) {     return(lookup (root, data)); }    /**Recursive Lookup--given a node, recur down searching for the given data. */   Private BooleanLookup (Node node,intdata) {     if(node==NULL) {       return(false); }    if(data==node.data) {return(true); }     Else if(data<node.data) {return(Lookup (node.left, data)); }     Else {       return(Lookup (node.right, data)); }   }    /**inserts the given data into the binary tree.   Uses a recursive helper. */    Public voidInsertintdata) {Root=Insert (root, data); }    /**Recursive Insert--Given a node pointer, recur down and inserts the given data into the tree.   Returns The new node pointer (the standard-communicate a changed pointer back-to-the-caller). */   PrivateNode Insert (node node,intdata) {     if(node==NULL) {node=NewNode (data); }     Else {       if(Data <=node.data) {node.left=Insert (node.left, data); }       Else{node.right=Insert (node.right, data); }     }    return(node);//in any case, return the new pointer to the caller}

There's also a way to get the height of the tree and the minimum value.

//Get the height of the tree

/**Returns the max root-to-leaf depth of the tree. Uses a recursive helper that recurs down to find the max depth. */ Public intmaxDepth () {return(maxDepth (root));}Private intmaxDepth (node node) {if(node==NULL) { return(0); } Else { intLdepth =maxDepth (Node.left); intRdepth =maxDepth (node.right); //Use the larger + 1 return(Math.max (ldepth, rdepth) + 1); }}//Get minimum value/**Returns the Min value in a non-empty binary search tree. Uses A helper method that iterates to the left to find the Min value. */ Public intMinValue () {return(MinValue (root));} /**Finds the Min value in a non-empty binary search tree.*/ Private intMinValue (node node) {Node current=node; while(Current.left! =NULL) { current=Current.left; } return(Current.data);}

About linked lists

I remember a job interview when College didn't graduate, and the interviewer asked me how many pointers I need to put a list upside down? At that time did not answer out, the impression is particularly profound, so this time this knowledge point to review.

It's written in C code, but the idea is the same.

/*iterative list reverse. Iterate through the list left-right. Move/insert each node to the front of the result list – like a Push of the node.*/Static voidReverse (structnode**headref) {        structnode*result=NULL; structnode*current=*Headref; structnode*Next;  while(current!=NULL) {Next=current->next;//Tricky:note the next nodecurrent->next=result;//move the node onto the resultresult=Current ; Current=Next; }                *headref=result;}

Requires three pointers, one pointing to the list that needs to be inverted, one to save the next node, and one to return to after the inverted list.

Binary tree and linked list review

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.