java - Adding a path length to each node in a Binary Search Tree -


I have a binary search tree class, and I was thinking, how can I calculate it using the height variable Yes the length of the root, of each individual node, by the root?

For example:

  S / \ EX / \ AH \ \ CM   

Path size of "E" (height 1) The path size of "A" is 2, the path size of "M" is 3, the path size of "X" is 1. How do I do this method when inserting a node ()?

  Private key; / / Sorted by important private value Vals; // Related data private node left, right; // Left and right subtitles Private int N; // number of nodes in the sub-private private int height; Put a private node (node ​​x, key key, value val) {if (x == null) {new node (key, wall, 1); } X.height = -1; Int cmp = key.compareTo (x.key); If (CMP & lt; 0) {x.left = put (x.left, key, val); } And if (CMP> 0) {x.right = put (x.right, key, val); } And {x.val = val; } X.N = 1 + size (x.left) + size (x.right); Return x; By definition, the path of a leaf is the path to its original + 1.    

You can easily get it by adding it to your method: < / P>

  Put a private node (node ​​x, key key, value, value, height) {if (x == zero) {new node return (key, val, height); }. . }   

The first call should be kept (empty, key, wal, 0)

and increase the height with each call. For:

  x.left = put (x.left, key, val, ++ height);   

Of course you have to add height to the node constructor.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -